void StackInfo::write(U_8* bytes) { U_8* data = bytes; StackInfo* serializedInfo = (StackInfo*)data; *serializedInfo = *this; serializedInfo->byteSize = getByteSize(); data+=sizeof(StackInfo); MemoryManager mm("DepthInfo"); EntryPtr * entries = new(mm) EntryPtr[hashTableSize]; for(U_32 i = 0; i< hashTableSize; i++) entries[i] = NULL; for(DepthMap::iterator dmit = stackDepthInfo->begin(); dmit != stackDepthInfo->end(); dmit++) { hashSet(entries, dmit->first, hashTableSize, dmit->second, mm); } U_8* next = data + hashTableSize * sizeof(POINTER_SIZE_INT); for(U_32 i = 0; i< hashTableSize; i++) { DepthEntry * e = entries[i]; POINTER_SIZE_INT serializedEntryAddr = 0; if(entries[i]) { serializedEntryAddr = (POINTER_SIZE_INT)next; for(; e != NULL; e = e->next) { DepthEntry* serialized = (DepthEntry*)next; *serialized = *e; next+=sizeof(DepthEntry); serialized->next = e->next ? (DepthEntry*)next : NULL; } } *((POINTER_SIZE_INT*)data)= serializedEntryAddr; data+=sizeof(POINTER_SIZE_INT); } assert(getByteSize() == (POINTER_SIZE_INT) (((U_8*)next) - bytes)); }
void Preferences_set(Preferences * self, const char * key, DataValue value) { struct PreferencesEvent event; DataValue * previousValue; event.key = key; event.value = value; previousValue = hashGet(self->private_ivar(hashTable), key); if (previousValue == NULL) { memset(&event.previousValue, 0, sizeof(event.previousValue)); event.previousValue.type = value.type; } else { event.previousValue = *previousValue; } EventDispatcher_dispatchEvent(self->eventDispatcher, ATOM(PREFERENCES_EVENT_VALUE_CHANGED), &event); hashSet(self->private_ivar(hashTable), key, valueCopy(&event.value)); }
void handleQuery(char *commandName, unsigned int key, unsigned int value, unsigned long responseAddress, unsigned int responsePort) { //, struct hashnode *table if(isSetQuery(commandName)) { // -- process set cmd if(hashSet(table, key, value) == -1){ strcpy(commandName, "err"); // key = value = 0; }else{ strcpy(commandName, "ok!"); } } else if(isGetQuery(commandName)) { // -- process get cmd int val = hashGet(table, key); if(val == -1){ strcpy(commandName, "nof"); // key = value = 0; }else{ strcpy(commandName, "val"); value = val; } } else if(isDelQuery(commandName)) { // -- process del cmd if(hashDel(table, key) == -1){ strcpy(commandName, "nof"); // key = value = 0; }else{ strcpy(commandName, "ok!"); } } else { strcpy(commandName, "err"); // key = value = 0; } printf("Table modification complete.\n"); forwardQuery(commandName, key, value, responseAddress, responsePort); return; }
/** * A client has requested the given url using the given method * (#MHD_HTTP_METHOD_GET, #MHD_HTTP_METHOD_PUT, * #MHD_HTTP_METHOD_DELETE, #MHD_HTTP_METHOD_POST, etc). The callback * must call MHD callbacks to provide content to give back to the * client and return an HTTP status code (i.e. #MHD_HTTP_OK, * #MHD_HTTP_NOT_FOUND, etc.). * * @param cls argument given together with the function * pointer when the handler was registered with MHD * @param url the requested url * @param method the HTTP method used (#MHD_HTTP_METHOD_GET, * #MHD_HTTP_METHOD_PUT, etc.) * @param version the HTTP version string (i.e. * #MHD_HTTP_VERSION_1_1) * @param upload_data the data being uploaded (excluding HEADERS, * for a POST that fits into memory and that is encoded * with a supported encoding, the POST data will NOT be * given in upload_data and is instead available as * part of #MHD_get_connection_values; very large POST * data *will* be made available incrementally in * @a upload_data) * @param upload_data_size set initially to the size of the * @a upload_data provided; the method must update this * value to the number of bytes NOT processed; * @param con_cls pointer that the callback can set to some * address and that will be preserved by MHD for future * calls for this request; since the access handler may * be called many times (i.e., for a PUT/POST operation * with plenty of upload data) this allows the application * to easily associate some request-specific state. * If necessary, this state can be cleaned up in the * global #MHD_RequestCompletedCallback (which * can be set with the #MHD_OPTION_NOTIFY_COMPLETED). * Initially, `*con_cls` will be NULL. * @return #MHD_YES if the connection was handled successfully, * #MHD_NO if the socket must be closed due to a serios * error while handling the request */ static int answer_to_connection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t * upload_data_size, void **con_cls) { if (*con_cls == NULL) { printf("CON_CLS is NULL. Request-type: %s\n", method); struct connection_info_struct *con_info; con_info = malloc(sizeof(struct connection_info_struct)); if (con_info == NULL) { return MHD_NO; } con_info->answerstring = NULL; if (strcmp(method, "POST") == 0) { con_info->postprocessor = MHD_create_post_processor(connection, POSTBUFFERSIZE, iterate_post, (void *) con_info); if (con_info->postprocessor == NULL) { free(con_info); return MHD_NO; } con_info->connectiontype = POST; } else if (strcmp(method, "DELETE") == 0) { con_info->connectiontype = DELETE; } else { con_info->connectiontype = GET; } *con_cls = (void *) con_info; return MHD_YES; } printf("CON_CLS is NOT NULL.\n"); if (0 == strcmp(method, "GET")) { char modifiableUrl [strlen(url) + 1]; strncpy(modifiableUrl, url, sizeof modifiableUrl); modifiableUrl[strlen(url)] = '\0'; char * argument = strtok(modifiableUrl, "/"); // remove api name argument = strtok(NULL, "/"); int parsedArgument = atoi(argument); int responseValue = hashGet(parsedArgument); if (responseValue >= 0) { char buffer[100]; // totally arbitrary but pretty big sprintf(buffer, "200 - OK\nValue: %d\n", responseValue); return send_page(connection, MHD_HTTP_OK, buffer); } else if (responseValue == NOT_FOUND) { return send_page(connection, 404, "404 - Not found\nThere are no entries matching your key.\n"); } else { return send_page(connection, 500, "500 - Internal server error\n"); } } if (0 == strcmp(method, "DELETE")) { printf("DELETE REQUEST to %s >>>\n", url); char modifiableUrl [strlen(url) + 1]; strncpy(modifiableUrl, url, sizeof modifiableUrl); modifiableUrl[strlen(url)] = '\0'; char * argument = strtok(modifiableUrl, "/"); // remove api name argument = strtok(NULL, "/"); int parsedArgument = atoi(argument); printf("Parsed as %d from %s\n", parsedArgument, argument); int responseValue = hashDel(parsedArgument); printf("Result: %d\n", responseValue); if (responseValue >= 0) { return send_page(connection, 200, "200 - OK\n"); } else if (responseValue == NOT_FOUND) { return send_page(connection, 404, "404 - Not found\nThere are no entries matching your key.\n"); } else { return send_page(connection, 500, "500 - Internal server error\n"); } } if (0 == strcmp(method, "POST")) { struct connection_info_struct *con_info = *con_cls; if (*upload_data_size != 0) { MHD_post_process(con_info->postprocessor, upload_data, *upload_data_size); *upload_data_size = 0; return MHD_YES; } else if (keySet && valueSet) { keySet = 0; valueSet = 0; printf("Set %d:%d!\n", key, value); if (hashSet(key, value)) { const char *responseText = "201 - Created\n"; struct MHD_Response *response = MHD_create_response_from_buffer(strlen(responseText), (void*) responseText, MHD_RESPMEM_PERSISTENT); char buffer[100]; // totally arbitrary but pretty big sprintf(buffer, "http://localhost:%d%s/%d", port, url, key); MHD_add_response_header (response, "Location", buffer); int ret = MHD_queue_response (connection, 201, response); MHD_destroy_response(response); return MHD_YES; } else { return send_page(connection, 500, "500 - Internal server error\n"); } } else { return send_page(connection, MHD_HTTP_BAD_REQUEST, "400 - Malformed request\n"); } } return send_page(connection, MHD_HTTP_NOT_FOUND, "404 - Not found\n"); }
static void deserializeContainer(DataValue * container, DeserializationContext * context) { size_t index, count; const char * key = NULL; enum DataValueType type; DataValue value; const void * blob; size_t length = 0; count = context->beginArray(context, NULL); if (context->status != SERIALIZATION_ERROR_OK) { return; } for (index = 0; index < count; index++) { if (container->type == DATA_TYPE_HASH_TABLE || container->type == DATA_TYPE_ASSOCIATIVE_ARRAY) { key = context->readString(context, NULL); if (context->status != SERIALIZATION_ERROR_OK) { return; } index++; } type = context->readEnumeration(context, NULL, ALL_DATA_TYPE_ENUM_KEYS_AND_VALUES, NULL); if (context->status != SERIALIZATION_ERROR_OK) { return; } index++; switch (type) { case DATA_TYPE_BOOLEAN: value = valueCreateBoolean(context->readBoolean(context, NULL)); break; case DATA_TYPE_INT8: value = valueCreateInt8(context->readInt8(context, NULL)); break; case DATA_TYPE_UINT8: value = valueCreateUInt8(context->readUInt8(context, NULL)); break; case DATA_TYPE_INT16: value = valueCreateInt16(context->readInt16(context, NULL)); break; case DATA_TYPE_UINT16: value = valueCreateUInt16(context->readUInt16(context, NULL)); break; case DATA_TYPE_INT32: value = valueCreateInt32(context->readInt32(context, NULL)); break; case DATA_TYPE_UINT32: value = valueCreateUInt32(context->readUInt32(context, NULL)); break; case DATA_TYPE_INT64: value = valueCreateInt64(context->readInt64(context, NULL)); break; case DATA_TYPE_UINT64: value = valueCreateUInt64(context->readUInt64(context, NULL)); break; case DATA_TYPE_FLOAT: value = valueCreateFloat(context->readFloat(context, NULL)); break; case DATA_TYPE_DOUBLE: value = valueCreateDouble(context->readDouble(context, NULL)); break; case DATA_TYPE_FIXED_16_16: value = valueCreateFixed16_16(context->readFixed16_16(context, NULL)); break; case DATA_TYPE_STRING: value = valueCreateString(context->readString(context, NULL), DATA_USE_STRLEN, true, true); break; case DATA_TYPE_BLOB: blob = context->readBlob(context, NULL, &length); if (context->status != SERIALIZATION_ERROR_OK) { return; } value = valueCreateBlob(blob, length, true, true); break; case DATA_TYPE_ARRAY: value.type = DATA_TYPE_ARRAY; value.value.array = arrayCreate(); deserializeContainer(&value, context); break; case DATA_TYPE_HASH_TABLE: value.type = DATA_TYPE_HASH_TABLE; value.value.hashTable = hashCreate(); deserializeContainer(&value, context); break; case DATA_TYPE_ASSOCIATIVE_ARRAY: value.type = DATA_TYPE_ASSOCIATIVE_ARRAY; value.value.associativeArray = associativeArrayCreate(); deserializeContainer(&value, context); break; case DATA_TYPE_POINTER: break; } if (context->status != SERIALIZATION_ERROR_OK) { return; } switch (container->type) { case DATA_TYPE_ARRAY: arrayAppend(container->value.array, value); break; case DATA_TYPE_HASH_TABLE: hashSet(container->value.hashTable, key, value); break; case DATA_TYPE_ASSOCIATIVE_ARRAY: associativeArrayAppend(container->value.associativeArray, key, value); break; default: break; } } context->endArray(context); }
int main(int argc, char const *argv[]) { // default to a table of 1,000 elements. size_t size = 1000; if (argc > 1) { size = atoi(argv[1]); if (!size) { // couldn't convert argv[1] to a number fprintf(stderr, USAGE); exit(EXIT_FAILURE); } } // create a new hash table Hash *h = hashCreate(size); if (!h) { // failed to create hash table fprintf(stderr, "failed to create hash table\n"); exit(EXIT_FAILURE); } else { printf("Successfully created a hash table of size %zu.\n",size); FILE *opt; if ((opt = fopen(OPTIONS_FILE,"r"))) { char c; while((c = fgetc(opt)) != EOF) { putchar(c); } fclose(opt); } else { fprintf(stderr, "failed to open %s\n", OPTIONS_FILE); exit(EXIT_FAILURE); } } // create some variables char *command, *key, *obj, *res; bool hasSet,hasGet,onOwn; hasSet = hasGet = onOwn = false; bool keepGoing = true; while(keepGoing) { // initialize the variables and print the prompt command = key = obj = res = NULL; // encourage the user to try out the different commands if (!hasSet) { printf("%% (type 'set') "); } else if (!hasGet) { printf("%% (type 'get') "); } else if (!onOwn) { onOwn = true; printf("You're on your own now... available commands: set, get, delete, load\n"); printf("%% "); } else { printf("%% "); } // read the command command = readString(); // process a set command if (!strcasecmp(command,"set")) { hasSet = true; printf("\tkey: "); key = readString(); printf("\tvalue: "); obj = readString(); printf("\tresult: "); printf(hashSet(h,key,obj) ? "SUCCESS\n" : "FAILURE\n"); // process a get command } else if (!strcasecmp(command,"get")) { hasGet = true; printf("\tkey: "); key = readString(); if ((res = (char *) hashGet(h,key))){ printf("\tresult: %s\n", res); } else { printf("\tresult: NULL\n"); } // process a delete command } else if (!strcasecmp(command,"delete")) { printf("\tkey: "); key = readString(); if ((res = (char *) hashDelete(h,key))){ printf("\tresult: %s\n", res); free(res); } else { printf("\tresult: NULL\n"); } // process a load command } else if (!strcasecmp(command,"load")) { printf("\tresult: %f\n", hashLoad(h)); // let the user exit } else if (!strcasecmp(command,"exit") || !strcasecmp(command,"quit")) { keepGoing = false; // invalid command } else if (strlen(command) > 0) { printf("invalid command\n"); } // free the command and key, as necessary if (command) { free(command); } if (key) { free(key); } } hashDestroy(h,destroy); return EXIT_SUCCESS; }
static void testCreate() { DataValue value; char * string1 = "abc", * string2 = "foo"; char blob1[] = {0x00, 0x01}, blob2[] = {0x03, 0x05, 0x07}; HashTable * hashTable1, * hashTable2; DataArray * array1, * array2; AssociativeArray * assArray1, * assArray2; value = valueCreateBoolean(false); TestCase_assert(value.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, value.type); TestCase_assert(!value.value.boolean, "Expected false but got true"); valueDispose(&value); value = valueCreateBoolean(true); TestCase_assert(value.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, value.type); TestCase_assert(value.value.boolean, "Expected true but got false"); valueDispose(&value); value = valueCreateInt8(0); TestCase_assert(value.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, value.type); TestCase_assert(value.value.int8 == 0, "Expected 0 but got %d", value.value.int8); valueDispose(&value); value = valueCreateInt8(1); TestCase_assert(value.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, value.type); TestCase_assert(value.value.int8 == 1, "Expected 1 but got %d", value.value.int8); valueDispose(&value); value = valueCreateUInt8(0); TestCase_assert(value.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, value.type); TestCase_assert(value.value.uint8 == 0, "Expected 0 but got %d", value.value.uint8); valueDispose(&value); value = valueCreateUInt8(1); TestCase_assert(value.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, value.type); TestCase_assert(value.value.uint8 == 1, "Expected 1 but got %d", value.value.uint8); valueDispose(&value); value = valueCreateInt16(0); TestCase_assert(value.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, value.type); TestCase_assert(value.value.int16 == 0, "Expected 0 but got %d", value.value.int16); valueDispose(&value); value = valueCreateInt16(1); TestCase_assert(value.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, value.type); TestCase_assert(value.value.int16 == 1, "Expected 1 but got %d", value.value.int16); valueDispose(&value); value = valueCreateUInt16(0); TestCase_assert(value.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, value.type); TestCase_assert(value.value.uint16 == 0, "Expected 0 but got %d", value.value.uint16); valueDispose(&value); value = valueCreateUInt16(1); TestCase_assert(value.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, value.type); TestCase_assert(value.value.uint16 == 1, "Expected 1 but got %d", value.value.uint16); valueDispose(&value); value = valueCreateInt32(0); TestCase_assert(value.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, value.type); TestCase_assert(value.value.int32 == 0, "Expected 0 but got %d", value.value.int32); valueDispose(&value); value = valueCreateInt32(1); TestCase_assert(value.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, value.type); TestCase_assert(value.value.int32 == 1, "Expected 1 but got %d", value.value.int32); valueDispose(&value); value = valueCreateUInt32(0); TestCase_assert(value.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, value.type); TestCase_assert(value.value.uint32 == 0, "Expected 0 but got %d", value.value.uint32); valueDispose(&value); value = valueCreateUInt32(1); TestCase_assert(value.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, value.type); TestCase_assert(value.value.uint32 == 1, "Expected 1 but got %d", value.value.uint32); valueDispose(&value); value = valueCreateInt64(0); TestCase_assert(value.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, value.type); TestCase_assert(value.value.int64 == 0, "Expected 0 but got " INT64_FORMAT, value.value.int64); valueDispose(&value); value = valueCreateInt64(1); TestCase_assert(value.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, value.type); TestCase_assert(value.value.int64 == 1, "Expected 1 but got " INT64_FORMAT, value.value.int64); valueDispose(&value); value = valueCreateUInt64(0); TestCase_assert(value.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, value.type); TestCase_assert(value.value.uint64 == 0, "Expected 0 but got " UINT64_FORMAT, value.value.uint64); valueDispose(&value); value = valueCreateUInt64(1); TestCase_assert(value.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, value.type); TestCase_assert(value.value.uint64 == 1, "Expected 1 but got " UINT64_FORMAT, value.value.uint64); valueDispose(&value); value = valueCreateFloat(0.0f); TestCase_assert(value.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, value.type); TestCase_assert(value.value.float32 == 0.0f, "Expected 0.0 but got %f", value.value.float32); valueDispose(&value); value = valueCreateFloat(1.0f); TestCase_assert(value.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, value.type); TestCase_assert(value.value.float32 == 1.0f, "Expected 1.0 but got %f", value.value.float32); valueDispose(&value); value = valueCreateDouble(0.0); TestCase_assert(value.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, value.type); TestCase_assert(value.value.float64 == 0.0, "Expected 0.0 but got %f", value.value.float64); valueDispose(&value); value = valueCreateDouble(1.0); TestCase_assert(value.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, value.type); TestCase_assert(value.value.float64 == 1.0, "Expected 1.0 but got %f", value.value.float64); valueDispose(&value); value = valueCreateFixed16_16(0x00000); TestCase_assert(value.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, value.type); TestCase_assert(value.value.fixed == 0x00000, "Expected 0x00000 but got 0x%05X", value.value.fixed); valueDispose(&value); value = valueCreateFixed16_16(0x10000); TestCase_assert(value.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, value.type); TestCase_assert(value.value.fixed == 0x10000, "Expected 0x10000 but got 0x%05X", value.value.fixed); valueDispose(&value); value = valueCreatePointer((void *) 0x0); TestCase_assert(value.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, value.type); TestCase_assert(value.value.pointer == (void *) 0x0, "Expected 0x0 but got %p", value.value.pointer); valueDispose(&value); value = valueCreatePointer((void *) 0x1); TestCase_assert(value.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, value.type); TestCase_assert(value.value.pointer == (void *) 0x1, "Expected 0x1 but got %p", value.value.pointer); valueDispose(&value); value = valueCreateString(string1, 2, true, true); TestCase_assert(value.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, value.type); TestCase_assert(!strcmp(value.value.string, "ab"), "Expected \"ab\" but got \"%s\"", value.value.string); TestCase_assert(value.value.string != string1, "Expected differing pointers, but both are %p", string1); valueDispose(&value); value = valueCreateString(string2, DATA_USE_STRLEN, false, false); TestCase_assert(value.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, value.type); TestCase_assert(value.value.string == string2, "Expected %p but got %p", string2, value.value.string); valueDispose(&value); value = valueCreateBlob(blob1, sizeof(blob1), true, true); TestCase_assert(value.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, value.type); TestCase_assert(value.value.blob.length == 2, "Expected 2 but got " SIZE_T_FORMAT, value.value.blob.length); TestCase_assert(!memcmp(value.value.blob.bytes, blob1, sizeof(blob1)), "Expected {0x%02X, 0x%02X} but got {0x%02X, 0x%02X}", blob1[0], blob1[1], ((char *) value.value.blob.bytes)[0], ((char *) value.value.blob.bytes)[1]); TestCase_assert(value.value.blob.bytes != blob1, "Expected differing pointers, but both are %p", blob1); valueDispose(&value); value = valueCreateBlob(blob2, sizeof(blob2), false, false); TestCase_assert(value.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, value.type); TestCase_assert(value.value.blob.length == 3, "Expected 3 but got " SIZE_T_FORMAT, value.value.blob.length); TestCase_assert(value.value.blob.bytes == blob2, "Expected %p but got %p", blob2, value.value.blob.bytes); valueDispose(&value); hashTable1 = hashCreate(); hashSet(hashTable1, "a", valueCreateBoolean(false)); hashTable2 = hashCreate(); value = valueCreateHashTable(hashTable1, true, true); TestCase_assert(value.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, value.type); TestCase_assert(value.value.hashTable != hashTable1, "Expected differing pointers, but both are %p", hashTable1); TestCase_assert(value.value.hashTable->count == hashTable1->count, "Expected " SIZE_T_FORMAT " but got " SIZE_T_FORMAT, hashTable1->count, value.value.hashTable->count); TestCase_assert(hashGet(value.value.hashTable, "a") != NULL, "Expected non-NULL but got NULL"); TestCase_assert(hashGet(value.value.hashTable, "a")->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, hashGet(value.value.hashTable, "a")->type); TestCase_assert(!hashGet(value.value.hashTable, "a")->value.boolean, "Expected false but got true"); valueDispose(&value); value = valueCreateHashTable(hashTable2, true, false); TestCase_assert(value.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, value.type); TestCase_assert(value.value.hashTable == hashTable2, "Expected %p but got %p", hashTable2, value.value.hashTable); valueDispose(&value); array1 = arrayCreate(); arrayAppend(array1, valueCreateBoolean(false)); array2 = arrayCreate(); value = valueCreateArray(array1, true, true); TestCase_assert(value.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, value.type); TestCase_assert(value.value.array != array1, "Expected differing pointers, but both are %p", array1); TestCase_assert(value.value.array->count == array1->count, "Expected " SIZE_T_FORMAT " but got " SIZE_T_FORMAT, array1->count, value.value.array->count); TestCase_assert(arrayGet(value.value.array, 0) != NULL, "Expected non-NULL but got NULL"); TestCase_assert(arrayGet(value.value.array, 0)->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, arrayGet(value.value.array, 0)->type); TestCase_assert(!arrayGet(value.value.array, 0)->value.boolean, "Expected false but got true"); valueDispose(&value); value = valueCreateArray(array2, true, false); TestCase_assert(value.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, value.type); TestCase_assert(value.value.array == array2, "Expected %p but got %p", array2, value.value.array); valueDispose(&value); assArray1 = associativeArrayCreate(); associativeArrayAppend(assArray1, "a", valueCreateBoolean(false)); assArray2 = associativeArrayCreate(); value = valueCreateAssociativeArray(assArray1, true, true); TestCase_assert(value.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, value.type); TestCase_assert(value.value.associativeArray != assArray1, "Expected differing pointers, but both are %p", assArray1); TestCase_assert(value.value.associativeArray->count == assArray1->count, "Expected " SIZE_T_FORMAT " but got " SIZE_T_FORMAT, assArray1->count, value.value.associativeArray->count); TestCase_assert(associativeArrayGetValueAtIndex(value.value.associativeArray, 0) != NULL, "Expected non-NULL but got NULL"); TestCase_assert(associativeArrayGetValueAtIndex(value.value.associativeArray, 0)->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, associativeArrayGetValueAtIndex(value.value.associativeArray, 0)->type); TestCase_assert(!associativeArrayGetValueAtIndex(value.value.associativeArray, 0)->value.boolean, "Expected false but got true"); TestCase_assert(!strcmp(associativeArrayGetKeyAtIndex(value.value.associativeArray, 0), "a"), "Expected \"a\" but got \"%s\"", associativeArrayGetKeyAtIndex(value.value.associativeArray, 0)); valueDispose(&value); value = valueCreateAssociativeArray(assArray2, true, false); TestCase_assert(value.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, value.type); TestCase_assert(value.value.associativeArray == assArray2, "Expected %p but got %p", assArray2, value.value.associativeArray); valueDispose(&value); }
static void testCopy() { DataValue value, copy; HashTable * hashTable; DataArray * array; AssociativeArray * assArray; value = valueCreateBoolean(false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, copy.type); TestCase_assert(!copy.value.boolean, "Expected false but got true"); valueDispose(&value); valueDispose(©); value = valueCreateBoolean(true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, copy.type); TestCase_assert(copy.value.boolean, "Expected true but got false"); valueDispose(&value); valueDispose(©); value = valueCreateInt8(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, copy.type); TestCase_assert(copy.value.int8 == 0, "Expected 0 but got %d", copy.value.int8); valueDispose(&value); valueDispose(©); value = valueCreateInt8(INT8_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, copy.type); TestCase_assert(copy.value.int8 == INT8_MIN, "Expected -128 but got %d", copy.value.int8); valueDispose(&value); valueDispose(©); value = valueCreateUInt8(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, copy.type); TestCase_assert(copy.value.uint8 == 0, "Expected 0 but got %u", copy.value.uint8); valueDispose(&value); valueDispose(©); value = valueCreateUInt8(UINT8_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, copy.type); TestCase_assert(copy.value.uint8 == UINT8_MAX, "Expected 255 but got %u", copy.value.uint8); valueDispose(&value); valueDispose(©); value = valueCreateInt16(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, copy.type); TestCase_assert(copy.value.int16 == 0, "Expected 0 but got %d", copy.value.int16); valueDispose(&value); valueDispose(©); value = valueCreateInt16(INT16_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, copy.type); TestCase_assert(copy.value.int16 == INT16_MIN, "Expected -32768 but got %d", copy.value.int16); valueDispose(&value); valueDispose(©); value = valueCreateUInt16(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, copy.type); TestCase_assert(copy.value.uint16 == 0, "Expected 0 but got %u", copy.value.uint16); valueDispose(&value); valueDispose(©); value = valueCreateUInt16(UINT16_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, copy.type); TestCase_assert(copy.value.uint16 == UINT16_MAX, "Expected 65535 but got %u", copy.value.uint16); valueDispose(&value); valueDispose(©); value = valueCreateInt32(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, copy.type); TestCase_assert(copy.value.int32 == 0, "Expected 0 but got %d", copy.value.int32); valueDispose(&value); valueDispose(©); value = valueCreateInt32(INT32_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, copy.type); TestCase_assert(copy.value.int32 == INT32_MIN, "Expected -214783648 but got %d", copy.value.int32); valueDispose(&value); valueDispose(©); value = valueCreateUInt32(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, copy.type); TestCase_assert(copy.value.uint32 == 0, "Expected 0 but got %u", copy.value.uint32); valueDispose(&value); valueDispose(©); value = valueCreateUInt32(UINT32_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, copy.type); TestCase_assert(copy.value.uint32 == UINT32_MAX, "Expected 4294967295 but got %u", copy.value.uint32); valueDispose(&value); valueDispose(©); value = valueCreateInt64(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, copy.type); TestCase_assert(copy.value.int64 == 0, "Expected 0 but got " INT64_FORMAT, copy.value.int64); valueDispose(&value); valueDispose(©); value = valueCreateInt64(INT64_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, copy.type); TestCase_assert(copy.value.int64 == INT64_MIN, "Expected -9223372036854775808 but got " INT64_FORMAT, copy.value.int64); valueDispose(&value); valueDispose(©); value = valueCreateUInt64(0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, copy.type); TestCase_assert(copy.value.uint64 == 0, "Expected 0 but got " UINT64_FORMAT, copy.value.uint64); valueDispose(&value); valueDispose(©); value = valueCreateUInt64(UINT64_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, copy.type); TestCase_assert(copy.value.uint64 == UINT64_MAX, "Expected 18446744073709551615 but got " UINT64_FORMAT, copy.value.uint64); valueDispose(&value); valueDispose(©); value = valueCreateFloat(0.0f); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, copy.type); TestCase_assert(copy.value.float32 == 0.0f, "Expected 0.0 but got %f", copy.value.float32); valueDispose(&value); valueDispose(©); value = valueCreateFloat(FLT_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, copy.type); TestCase_assert(copy.value.float32 == FLT_MAX, "Expected %f but got %f", FLT_MAX, copy.value.float32); valueDispose(&value); valueDispose(©); value = valueCreateDouble(0.0); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, copy.type); TestCase_assert(copy.value.float64 == 0.0, "Expected 0.0 but got %f", copy.value.float64); valueDispose(&value); valueDispose(©); value = valueCreateDouble(DBL_MAX); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, copy.type); TestCase_assert(copy.value.float64 == DBL_MAX, "Expected %f but got %f", DBL_MAX, copy.value.float64); valueDispose(&value); valueDispose(©); value = valueCreateFixed16_16(0x00000); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, copy.type); TestCase_assert(copy.value.fixed == 0.0, "Expected 0.0 but got %f", copy.value.fixed); valueDispose(&value); valueDispose(©); value = valueCreateFixed16_16(FIXED_16_16_MIN); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, copy.type); TestCase_assert(copy.value.fixed == FIXED_16_16_MIN, "Expected 0x%05X but got 0x%05X", FIXED_16_16_MIN, copy.value.fixed); valueDispose(&value); valueDispose(©); value = valueCreatePointer(NULL); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, copy.type); TestCase_assert(copy.value.pointer == NULL, "Expected NULL but got %p", copy.value.pointer); valueDispose(&value); valueDispose(©); value = valueCreatePointer((void *) 0xFFFFFFFF); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, copy.type); TestCase_assert(copy.value.pointer == (void *) 0xFFFFFFFF, "Expected 0xffffffff but got %p", copy.value.pointer); valueDispose(&value); valueDispose(©); value = valueCreateString("", DATA_USE_STRLEN, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, copy.type); TestCase_assert(copy.value.string != NULL, "Expected non-NULL but got NULL"); TestCase_assert(!strcmp(copy.value.string, ""), "Expected \"\" but got \"%s\"", copy.value.string); valueDispose(&value); valueDispose(©); value = valueCreateString("abc", DATA_USE_STRLEN, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, copy.type); TestCase_assert(copy.value.string != NULL, "Expected non-NULL but got NULL"); TestCase_assert(!strcmp(copy.value.string, "abc"), "Expected \"abc\" but got \"%s\"", copy.value.string); TestCase_assert(copy.value.string == value.value.string, "Expected %p but got %p", value.value.string, copy.value.string); valueDispose(&value); valueDispose(©); value = valueCreateString("abc", DATA_USE_STRLEN, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, copy.type); TestCase_assert(copy.value.string != NULL, "Expected non-NULL but got NULL"); TestCase_assert(!strcmp(copy.value.string, "abc"), "Expected \"abc\" but got \"%s\"", copy.value.string); TestCase_assert(copy.value.string != value.value.string, "Expected pointers to differ, but both are %p", copy.value.string); valueDispose(&value); valueDispose(©); value = valueCreateBlob("", 0, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, copy.type); TestCase_assert(copy.value.blob.bytes != NULL, "Expected non-NULL but got NULL"); TestCase_assert(copy.value.blob.length == 0, "Expected 0 but got " SIZE_T_FORMAT, copy.value.blob.length); valueDispose(&value); valueDispose(©); value = valueCreateBlob("\x00\xFF", 2, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, copy.type); TestCase_assert(copy.value.blob.bytes != NULL, "Expected non-NULL but got NULL"); TestCase_assert(copy.value.blob.length == 2, "Expected 2 but got " SIZE_T_FORMAT, copy.value.blob.length); TestCase_assert(!memcmp(copy.value.blob.bytes, "\x00\xFF", 2), "Expected {0x00, 0xFF} but got {0x%02X, 0x%02X}", ((char *) copy.value.blob.bytes)[0], ((char *) copy.value.blob.bytes)[1]); TestCase_assert(copy.value.blob.bytes == value.value.blob.bytes, "Expected %p but got %p", value.value.blob.bytes, copy.value.blob.bytes); valueDispose(&value); valueDispose(©); value = valueCreateBlob("\x00\xFF", 2, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, copy.type); TestCase_assert(copy.value.blob.bytes != NULL, "Expected non-NULL but got NULL"); TestCase_assert(copy.value.blob.length == 2, "Expected 2 but got " SIZE_T_FORMAT, copy.value.blob.length); TestCase_assert(!memcmp(copy.value.blob.bytes, "\x00\xFF", 2), "Expected {0x00, 0xFF} but got {0x%02X, 0x%02X}", ((char *) copy.value.blob.bytes)[0], ((char *) copy.value.blob.bytes)[1]); TestCase_assert(copy.value.blob.bytes != value.value.blob.bytes, "Expected pointers to differ, but both are %p", copy.value.blob.bytes); valueDispose(&value); valueDispose(©); hashTable = hashCreate(); hashSet(hashTable, "a", valueCreateBoolean(false)); value = valueCreateHashTable(hashTable, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, copy.type); TestCase_assert(copy.value.hashTable == value.value.hashTable, "Expected %p but got %p", value.value.hashTable, copy.value.hashTable); valueDispose(&value); valueDispose(©); value = valueCreateHashTable(hashTable, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, copy.type); TestCase_assert(copy.value.hashTable->count == 1, "Expected 1 but got " SIZE_T_FORMAT, copy.value.hashTable->count); TestCase_assert(copy.value.hashTable != value.value.hashTable, "Expected pointers to differ, but both are %p", copy.value.hashTable); valueDispose(&value); valueDispose(©); hashDispose(hashTable); array = arrayCreate(); arrayAppend(array, valueCreateBoolean(false)); value = valueCreateArray(array, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, copy.type); TestCase_assert(copy.value.array == value.value.array, "Expected %p but got %p", value.value.array, copy.value.array); valueDispose(&value); valueDispose(©); value = valueCreateArray(array, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, copy.type); TestCase_assert(copy.value.array->count == 1, "Expected 1 but got " SIZE_T_FORMAT, copy.value.array->count); TestCase_assert(copy.value.array != value.value.array, "Expected pointers to differ, but both are %p", copy.value.array); valueDispose(&value); valueDispose(©); arrayDispose(array); assArray = associativeArrayCreate(); associativeArrayAppend(assArray, "a", valueCreateBoolean(false)); value = valueCreateAssociativeArray(assArray, false, false); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, copy.type); TestCase_assert(copy.value.associativeArray == value.value.associativeArray, "Expected %p but got %p", value.value.associativeArray, copy.value.associativeArray); valueDispose(&value); valueDispose(©); value = valueCreateAssociativeArray(assArray, true, true); copy = valueCopy(&value); TestCase_assert(copy.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, copy.type); TestCase_assert(copy.value.associativeArray->count == 1, "Expected 1 but got " SIZE_T_FORMAT, copy.value.associativeArray->count); TestCase_assert(copy.value.associativeArray != value.value.associativeArray, "Expected pointers to differ, but both are %p", copy.value.associativeArray); valueDispose(&value); valueDispose(©); associativeArrayDispose(assArray); }