int initMagneticField() { _conductors = arrayNew(1); _fieldPoints = arrayNew(2048); pthread_mutex_init(&_fieldPointsMutex, NULL); Conductor* conductor1 = (Conductor*) malloc(sizeof(Conductor)); conductor1->position = (Vector) { 12, -12, -12 }; conductor1->I = 6000; conductor1->permeability = 2.5 * 1.0e-1; conductor1->l = (Vector) { 4, 0.6, 0.6 }; arrayAppend(_conductors, conductor1); Conductor* conductor2 = (Conductor*) malloc(sizeof(Conductor)); conductor2->position = (Vector) { 12, 12, -12 }; conductor2->I = 3000; conductor2->permeability = 2.5 * 1.0e-1; conductor2->l = (Vector) { 4, 0.4, 0.4 }; arrayAppend(_conductors, conductor2); Conductor* conductor3 = (Conductor*) malloc(sizeof(Conductor)); conductor3->position = (Vector) { 12, 12, 12 }; conductor3->I = 9000; conductor3->permeability = 2.5 * 1.0e-1; conductor3->l = (Vector) { 4, 0.9, 0.9 }; arrayAppend(_conductors, conductor3); Conductor* conductor4 = (Conductor*) malloc(sizeof(Conductor)); conductor4->position = (Vector) { 12, -12, 12 }; conductor4->I = 1000; conductor4->permeability = 2.5 * 1.0e-1; conductor4->l = (Vector) { 4, 0.3, 0.3 }; arrayAppend(_conductors, conductor4); return 1; }
static int readColors(lua_State *L, Array *colors) { SDL_Color color; color = videoGetColorRGB(L, -1); return arrayAppend(colors, &color) != -1; }
static int readPoints(lua_State *L, Array *points) { SDL_Point p; /* Verify the point as a table */ if (lua_type(L, -1) != LUA_TTABLE) return 0; p.x = tableGetInt(L, -1, "x"); p.y = tableGetInt(L, -1, "y"); return arrayAppend(points, &p) != -1; }
static int writer(lua_State *L, const char *data, size_t sz, LoadState *state) { size_t i; for (i = 0; i < sz; ++i) { if (arrayAppend(&state->buffer, &data[i]) < 0) { arrayFree(&state->buffer); lua_pushstring(L, strerror(errno)); return -1; } } return 0; }
static int readRects(lua_State *L, Array *rects) { SDL_Rect r; /* Verify the point as a table */ if (lua_type(L, -1) != LUA_TTABLE) return 0; r.w = tableGetInt(L, -1, "w"); r.h = tableGetInt(L, -1, "h"); r.x = tableGetInt(L, -1, "x"); r.y = tableGetInt(L, -1, "y"); return arrayAppend(rects, &r) != -1; }
/* appends an entry; * does not call httpHeaderEntryClone() so one should not reuse "*e" */ void httpHeaderAddEntry(HttpHeader * hdr, HttpHeaderEntry * e) { assert(hdr && e); assert_eid(e->id); debug(55, 7) ("%p adding entry: %d at %d\n", hdr, e->id, hdr->entries.count); if (CBIT_TEST(hdr->mask, e->id)) Headers[e->id].stat.repCount++; else CBIT_SET(hdr->mask, e->id); arrayAppend(&hdr->entries, e); /* increment header length, allow for ": " and crlf */ hdr->len += strLen(e->name) + 2 + strLen(e->value) + 2; }
static int swdeaSwapDirMatchCacheDir(char *t, cache_select_t *p, char *cache_dir) { int i = 0; int i_t_count = 0; int i_s_count = Config.cacheSwap.n_configured; SwapDir *gswap = Config.cacheSwap.swapDirs; if(reconfigure_in_thread) { i_s_count = Config_r.cacheSwap.n_configured; gswap = Config_r.cacheSwap.swapDirs; } for(i = 0; i < i_s_count; i++) { if(0 == strncmp(gswap[i].path, t, strlen(t))) { if (gswap[i].flags.read_only) { debug(172, 1)("%s %s match cache_dir Read_only %s %s\n", PMOD_NAME, __func__, gswap[i].path, t); } else if (0 == cache_dir[i]) { debug(172, 2)("%s %s match swap[%d] %s %s\n", PMOD_NAME, __func__, i, gswap[i].path, t); arrayAppend(&p->dir, (void *)i); p->dir.items[p->dir.count-1] = (void *)i; i_t_count++; cache_dir[i] = 1; } else { debug(172, 1)("%s %s already matchd %s %s\n", PMOD_NAME, __func__, gswap[i].path, t); } } else { debug(172, 1)("%s %s not match cache_dir\n", PMOD_NAME, __func__); } } return i_t_count; }
void JSONItem::arrayAppend(const wxString& value) { arrayAppend(JSONItem(wxT(""), value, cJSON_String)); }
void JSONElement::arrayAppend(const wxString& value) { arrayAppend(JSONElement(wxT(""), value, cJSON_String)); }
int main(int argc, char* argv[]){ display.create(640, 480); // Load geometry data // GLuint bo_vertex, bo_index; glGenBuffers(1, &bo_vertex); glGenBuffers(1, &bo_index); GLuint* count_nodes = rawdimp("../wtetris/scene.rawd", bo_vertex, bo_index); Geom* nodes = (Geom*)(count_nodes + 1); Geom* goem_border = nodes; Geom* geom_digits = nodes + 1; // Create and load shader programs // char* src_vs = file.loadText("../wtetris/shaders/base.vs"); char* src_fs = file.loadText("../wtetris/shaders/base.fs"); GLuint vs = gl.createShader(GL_VERTEX_SHADER, src_vs); GLuint fs = gl.createShader(GL_FRAGMENT_SHADER, src_fs); GLuint program = gl.createProgram(vs, fs); glUseProgram(program); // Get handle to shader vars // GLint loc_world = glGetUniformLocation(program, "world"); GLint loc_view = glGetUniformLocation(program, "view"); GLint loc_proj = glGetUniformLocation(program, "proj"); // Camera setup - projecton and initial position Camera cam; cam.loc_view = loc_view; cam.loc_proj = loc_proj; matIdent(cam.mat_world); toggleProjection(&cam); float* p = &cam.mat_world[12]; p[0] = 12; p[1] = 30; p[2] = 40; // Hide mouse, and set to center mouse.hide(); mouse.setPos(display.getWidth()/2, display.getHeight()/2); // Persistent OpenGL state glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CCW); glPolygonMode(GL_FRONT, GL_LINE); // Core struct init // scene.cam = &cam; Array objects; arrayInit(&objects, 5); scene.objects = &objects; // Visible objects =================================== Board board; boardInit(&board, goem_border, 20, 12); arrayAppend(&objects, &board); Label title; labelInit(&title, -12.5, 30, geom_digits); char text_title[] = "wtetris"; labelSet(&title, text_title); labelScaleSet(&title, 5); arrayAppend(&objects, &title); Label info; labelInit(&info, -3.5, 20, geom_digits); char text_info[] = "start:enter\nquit:q"; labelSet(&info, text_info); labelScaleSet(&info, 2); arrayAppend(&objects, &info); Label time; labelInit(&time, -16.5, -18.0, geom_digits); char score_text[] = "time:\n2501"; labelSet(&time, score_text); labelScaleSet(&time, 2); arrayAppend(&objects, &time); Label score; labelInit(&score, 26, -18.0, geom_digits); char text[] = "score:\n0000"; labelSet(&score, text); labelScaleSet(&score, 2); arrayAppend(&objects, &score); // =================================================== Wave wave; wave.amp = 2; wave.wlen = 2; wave.time = 1000; waveLocsSet(&wave, program); wavePosSet(&wave, &board.piece); scene.wave = &wave; Renderer renderer; renderer.loc_world = loc_world; // Initial game state // scene.state = st_title; // LOOP ----------------------------------------------------------------------- while (1){ events.process(); if (keyboard.keyHit('q')){ break; } if (keyboard.keyHit('r')){ toggleProjection(&cam); } //fpscont(&cam); scene.state(&board); wave.time += 0.2; waveToProg(&wave); cameraWorldToView(&cam); glUniformMatrix4fv(loc_view, 1, 0, cam.mat_view); render(&renderer, &scene); display.update(); SDL_Delay(16); } return 0; }
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); }
void updateMagneticField(const RenderContext* context) { static const Vector minCellPosRel = { -48, -48, -48 }; static const Vector maxCellPosRel = { 48, 48, 48 }; static const int cellStep = 8; Vector minCellPos = vectorSum(context->camera.position, minCellPosRel); Vector maxCellPos = vectorSum(context->camera.position, maxCellPosRel); size_t i, count; // remove points which is too far from camera for (i = 0, count = arrayGetLength(_fieldPoints); i < count; ++i) { pthread_mutex_lock(&_fieldPointsMutex); VectorFieldPoint* point = (VectorFieldPoint*) arrayGetAt(_fieldPoints, i); if (point->position.x < minCellPos.x + minCellPosRel.x || point->position.x > maxCellPos.x + maxCellPosRel.x || point->position.y < minCellPos.y + minCellPosRel.y || point->position.y > maxCellPos.y + maxCellPosRel.y || point->position.z < minCellPos.z + minCellPosRel.z || point->position.z > maxCellPos.z + maxCellPosRel.z) { arrayDestroy(_fieldPoints, i); --i; --count; } pthread_mutex_unlock(&_fieldPointsMutex); } // compute points int x, y, z; for (x = (int) (minCellPos.x / cellStep) * cellStep; x <= maxCellPos.x; x += cellStep) { for (y = (int) (minCellPos.y / cellStep) * cellStep; y <= maxCellPos.y; y += cellStep) { for (z = (int) (minCellPos.z / cellStep) * cellStep; z <= maxCellPos.z; z += cellStep) { const Vector position = { x, y, z }; // try to find int pointExists = 0; for (i = 0, count = arrayGetLength(_fieldPoints); i < count; ++i) { pthread_mutex_lock(&_fieldPointsMutex); VectorFieldPoint* point = (VectorFieldPoint*) arrayGetAt(_fieldPoints, i); if (point->position.x >= x && point->position.x <= x + cellStep && point->position.y >= y && point->position.y <= y + cellStep && point->position.z >= z && point->position.z <= z + cellStep) { pointExists = 1; pthread_mutex_unlock(&_fieldPointsMutex); break; } pthread_mutex_unlock(&_fieldPointsMutex); } if (pointExists) { continue; } // compute if not exist VectorFieldPoint* result = (VectorFieldPoint*) malloc(sizeof(VectorFieldPoint)); result->position = position; result->direction = vectorZero; for (i = 0, count = arrayGetLength(_conductors); i < count; ++i) { Conductor* conductor = (Conductor*) arrayGetAt(_conductors, i); result->direction = vectorSum( result->direction, calculateMagneticFieldPoint(conductor->I, conductor->permeability, conductor->l, vectorSubstract(position, conductor->position)) ); } pthread_mutex_lock(&_fieldPointsMutex); arrayAppend(_fieldPoints, result); pthread_mutex_unlock(&_fieldPointsMutex); } } } }
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); }