LONGBOW_TEST_CASE(Object, parcRandomAccessFile_Read) { char *fname = "tmpfile"; PARCFile *file = parcFile_Create(fname); parcFile_CreateNewFile(file); FILE *fp = fopen(fname, "w"); fseek(fp, 0, SEEK_SET); uint8_t data[128]; for (int i = 0; i < 128; i++) { data[i] = i; } fwrite(data, 1, 128, fp); fclose(fp); PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); parcFile_Release(&file); PARCBuffer *buffer = parcBuffer_Allocate(128); size_t numBytes = parcRandomAccessFile_Read(instance, buffer); assertTrue(numBytes == 128, "Expected 128 bytes to be read, but got %zu", numBytes); parcBuffer_Flip(buffer); uint8_t *bytes = parcBuffer_Overlay(buffer, parcBuffer_Remaining(buffer)); assertTrue(memcmp(data, bytes, 128) == 0, "Expected buffers to be equal"); parcBuffer_Release(&buffer); parcRandomAccessFile_Close(instance); parcRandomAccessFile_Release(&instance); }
LONGBOW_TEST_CASE(Object, parcRandomAccessFile_Write) { char *fname = "tmpfile"; PARCFile *file = parcFile_Create(fname); parcFile_CreateNewFile(file); uint8_t data[128]; for (int i = 0; i < 128; i++) { data[i] = i; } PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); PARCBuffer *buffer = parcBuffer_Allocate(128); parcBuffer_PutArray(buffer, 128, data); parcBuffer_Flip(buffer); size_t numBytes = parcRandomAccessFile_Write(instance, buffer); assertTrue(numBytes == 128, "Expected 128 bytes to be read, but got %zu", numBytes); parcBuffer_Release(&buffer); parcRandomAccessFile_Close(instance); parcRandomAccessFile_Release(&instance); uint8_t bytes[128]; FILE *fp = fopen(fname, "r"); numBytes = fread(bytes, 1, 128, fp); assertTrue(numBytes == 128, "Expected 128 bytes to be read, but got %zu", numBytes); fclose(fp); assertTrue(memcmp(data, bytes, 128) == 0, "Expected buffers to be equal"); parcFile_Release(&file); }
LONGBOW_TEST_CASE(Object, parcRandomAccessFile_Display) { PARCFile *file = parcFile_Create("/tmp/tmpfile"); PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); parcFile_Release(&file); parcRandomAccessFile_Display(instance, 0); parcRandomAccessFile_Release(&instance); }
LONGBOW_TEST_CASE(Object, parcRandomAccessFile_IsValid) { PARCFile *file = parcFile_Create("/tmp/tmpfile"); parcFile_CreateNewFile(file); PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); parcFile_Release(&file); assertTrue(parcRandomAccessFile_IsValid(instance), "Expected parcRandomAccessFile_Create to result in a valid instance."); parcRandomAccessFile_Release(&instance); assertFalse(parcRandomAccessFile_IsValid(instance), "Expected parcRandomAccessFile_Release to result in an invalid instance."); }
LONGBOW_TEST_CASE(CreateAcquireRelease, CreateRelease) { PARCFile *file = parcFile_Create("/tmp/tmpfile"); PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); assertNotNull(instance, "Expected non-null result from parcRandomAccessFile_Open();"); parcObjectTesting_AssertAcquireReleaseContract(parcRandomAccessFile_Acquire, instance); parcRandomAccessFile_Release(&instance); assertNull(instance, "Expected null result from parcRandomAccessFile_Release();"); parcFile_Release(&file); }
LONGBOW_TEST_CASE(Object, parcRandomAccessFile_ToString) { PARCFile *file = parcFile_Create("/tmp/tmpfile"); PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); parcFile_Release(&file); char *string = parcRandomAccessFile_ToString(instance); assertNotNull(string, "Expected non-NULL result from parcRandomAccessFile_ToString"); parcMemory_Deallocate((void **) &string); parcRandomAccessFile_Release(&instance); }
LONGBOW_TEST_CASE(Object, parcRandomAccessFile_Equals) { PARCFile *fileX = parcFile_Create("/tmp/tmpfileX"); PARCFile *fileY = parcFile_Create("/tmp/tmpfileX"); PARCFile *fileZ = parcFile_Create("/tmp/tmpfileX"); PARCRandomAccessFile *x = parcRandomAccessFile_Open(fileX); PARCRandomAccessFile *y = parcRandomAccessFile_Open(fileY); PARCRandomAccessFile *z = parcRandomAccessFile_Open(fileZ); parcFile_Release(&fileX); parcFile_Release(&fileY); parcFile_Release(&fileZ); parcObjectTesting_AssertEquals(x, y, z, NULL); parcRandomAccessFile_Close(x); parcRandomAccessFile_Close(y); parcRandomAccessFile_Close(z); parcRandomAccessFile_Release(&x); parcRandomAccessFile_Release(&y); parcRandomAccessFile_Release(&z); }
/** * Write the input buffer to the specified file. * * @param [in] outFile Name of the file to which the buffer will be written. * @param [in] data A `PARCBuffer` instance which stores the data to be written. */ static void _ccnxFileRepoClient_AppendBufferToFile(char *outFile, PARCBuffer *data, size_t offset) { PARCFile *out = parcFile_Create(outFile); parcFile_CreateNewFile(out); PARCRandomAccessFile *fout = parcRandomAccessFile_Open(out); parcRandomAccessFile_Seek(fout, offset, PARCRandomAccessFilePosition_Start); parcRandomAccessFile_Write(fout, data); parcRandomAccessFile_Close(fout); parcRandomAccessFile_Release(&fout); parcFile_Release(&out); }
static void _destroy(PARCFileChunker **chunkerP) { if ((*chunkerP)->fhandle != NULL) { parcRandomAccessFile_Release(&(*chunkerP)->fhandle); } if ((*chunkerP)->file != NULL) { parcFile_Release(&(*chunkerP)->file); } if ((*chunkerP)->currentElement != NULL) { parcBuffer_Release(&(*chunkerP)->currentElement); } }
LONGBOW_TEST_CASE(Object, parcRandomAccessFile_ToJSON) { PARCFile *file = parcFile_Create("/tmp/tmpfile"); PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); parcFile_Release(&file); PARCJSON *json = parcRandomAccessFile_ToJSON(instance); PARCJSONPair *pair = parcJSON_GetPairByName(json, "fname"); PARCJSONValue *value = parcJSONPair_GetValue(pair); PARCBuffer *buffer = parcJSONValue_GetString(value); char *string = parcBuffer_ToString(buffer); assertTrue(strcmp("/tmp/tmpfile", string) == 0, "The file was stored correctly"); parcMemory_Deallocate(&string); parcJSON_Release(&json); parcRandomAccessFile_Release(&instance); }
LONGBOW_TEST_CASE(Object, parcRandomAccessFile_Seek) { char *fname = "tmpfile"; PARCFile *file = parcFile_Create(fname); parcFile_CreateNewFile(file); FILE *fp = fopen(fname, "w"); fseek(fp, 0, SEEK_SET); uint8_t data[128]; for (int i = 0; i < 128; i++) { data[i] = i; } fwrite(data, 1, 128, fp); fclose(fp); PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); PARCBuffer *buffer = parcBuffer_Allocate(128); parcRandomAccessFile_Seek(instance, 64, PARCRandomAccessFilePosition_Start); size_t numBytes = parcRandomAccessFile_Read(instance, buffer); assertTrue(numBytes == 64, "Expected 64 bytes to be read, but got %zu", numBytes); parcRandomAccessFile_Seek(instance, 0, PARCRandomAccessFilePosition_End); parcBuffer_Flip(buffer); numBytes = parcRandomAccessFile_Read(instance, buffer); assertTrue(numBytes == 0, "Expected 0 bytes to be read, but got %zu", numBytes); parcRandomAccessFile_Seek(instance, 0, PARCRandomAccessFilePosition_Start); parcBuffer_Flip(buffer); numBytes = parcRandomAccessFile_Read(instance, buffer); assertTrue(numBytes == 128, "Expected 128 bytes to be read, but got %zu", numBytes); parcBuffer_Release(&buffer); parcRandomAccessFile_Close(instance); parcRandomAccessFile_Release(&instance); parcFile_Release(&file); }