void TreeTest::UpdateObj() { ret = co_tree_insert(Tree16, "1TESTKEY1", 10, TestString1); ASSERT_EQ(1, ret); ret = co_tree_set_str(Tree16, "1TESTKEY1", 10, "REPLACE", 9); ASSERT_EQ(1, ret); ptr = co_tree_find(Tree16, "1TESTKEY1", 10); ASSERT_EQ(0, co_str_cmp(ptr, ReplaceString1)); // reinitialize TestString1 TestString1 = co_str8_create("1TESTVALUE1", 12, 0); // repeat for Tree32 ret = co_tree_insert(Tree32, "1TESTKEY1", 10, TestString1); ASSERT_EQ(1, ret); ret = co_tree_set_str(Tree32, "1TESTKEY1", 10, "REPLACE", 9); ASSERT_EQ(1, ret); ptr = co_tree_find(Tree32, "1TESTKEY1", 10); ASSERT_EQ(0, co_str_cmp(ptr, ReplaceString1)); }
void TreeTest::InsertObj() { ret = co_tree_insert(Tree16, "1TESTKEY1", 10, TestString1); ASSERT_EQ(1, ret); ptr = co_tree_find(Tree16, "1TESTKEY1", 10); ASSERT_EQ(TestString1, ptr); ret = co_tree_insert(Tree16, "2TESTKEY2", 10, TestString2); ASSERT_EQ(1, ret); ptr = co_tree_find(Tree16, "2TESTKEY2", 10); ASSERT_EQ(TestString2, ptr); ret = co_tree_insert_force(Tree16, "1TESTKEY1", 10, ReplaceString1); ASSERT_EQ(1, ret); ptr = co_tree_find(Tree16, "1TESTKEY1", 10); ASSERT_EQ(ReplaceString1, ptr); // reinitialize TestString1 since it was freed by co_tree_insert_force() TestString1 = co_str8_create("1TESTVALUE1", 12, 0); // repeat for Tree 32 ret = co_tree_insert(Tree32, "1TESTKEY1", 10, TestString1); ASSERT_EQ(1, ret); ptr = co_tree_find(Tree32, "1TESTKEY1", 10); ASSERT_EQ(TestString1, ptr); ret = co_tree_insert(Tree32, "2TESTKEY2", 10, TestString2); ASSERT_EQ(1, ret); ptr = co_tree_find(Tree32, "2TESTKEY2", 10); ASSERT_EQ(TestString2, ptr); ret = co_tree_insert_force(Tree32, "1TESTKEY1", 10, ReplaceString1); ASSERT_EQ(1, ret); ptr = co_tree_find(Tree32, "1TESTKEY1", 10); ASSERT_EQ(ReplaceString1, ptr); }
void TreeTest::DeleteObj() { ret = co_tree_insert(Tree16, "1TESTKEY1", 10, TestString1); ASSERT_EQ(1, ret); ret = co_tree_insert(Tree16, "2TESTKEY2", 10, TestString2); ASSERT_EQ(1, ret); ptr = co_tree_delete(Tree16, "1TESTKEY1", 10); ASSERT_EQ(TestString1, ptr); ptr = co_tree_delete(Tree16, "2TESTKEY2", 10); ASSERT_EQ(TestString2, ptr); // confirm deletions ptr = co_tree_find(Tree16, "1TESTKEY1", 10); ASSERT_EQ(NULL, ptr); ptr = co_tree_find(Tree16, "2TESTKEY2", 10); ASSERT_EQ(NULL, ptr); // repeat for Tree32 ret = co_tree_insert(Tree32, "1TESTKEY1", 10, TestString1); ASSERT_EQ(1, ret); ret = co_tree_insert(Tree32, "2TESTKEY2", 10, TestString2); ASSERT_EQ(1, ret); ptr = co_tree_delete(Tree32, "1TESTKEY1", 10); ASSERT_EQ(TestString1, ptr); ptr = co_tree_delete(Tree32, "2TESTKEY2", 10); ASSERT_EQ(TestString2, ptr); // confirm deletions ptr = co_tree_find(Tree32, "1TESTKEY1", 10); ASSERT_EQ(NULL, ptr); ptr = co_tree_find(Tree32, "2TESTKEY2", 10); ASSERT_EQ(NULL, ptr); }
size_t co_tree_import(co_obj_t **tree, const char *input, const size_t ilen) { size_t length = 0, olen = 0, read = 0, klen = 0; char *kstr = NULL; int i = 0; co_obj_t *obj = NULL; const char *cursor = input; switch((uint8_t)input[0]) { case _tree16: length = *((uint16_t *)(input + 1)); *tree = co_tree16_create(); cursor += sizeof(uint16_t) + 1; read = sizeof(uint16_t) + 1; break; case _tree32: length = (uint32_t)(*(uint32_t*)(input + 1)); *tree = co_tree32_create(); cursor += sizeof(uint32_t) + 1; read = sizeof(uint32_t) + 1; break; default: SENTINEL("Not a tree."); break; } while(i < length && read <= ilen) { DEBUG("Importing tuple:"); if((uint8_t)cursor[0] == _str8) { DEBUG("Reading key..."); cursor += 1; read += 1; klen = (uint8_t)cursor[0]; kstr = (char *)&cursor[1]; cursor += klen + 1; read += klen + 1; DEBUG("Reading value..."); switch((uint8_t)cursor[0]) { case _list16: case _list32: olen = co_list_import(&obj, cursor, ilen - read); break; case _tree16: case _tree32: olen = co_tree_import(&obj, cursor, ilen - read); break; default: olen = co_obj_import(&obj, cursor, ilen - read, 0); break; } CHECK(olen > 0, "Failed to import object."); cursor +=olen; read += olen; DEBUG("Inserting value into tree with key."); CHECK(co_tree_insert(*tree, kstr, klen, obj), "Failed to insert object."); i++; } } return read; error: if(obj != NULL) co_obj_free(obj); return -1; }