static void test_Local_Delete(TestBatchRunner *runner) { RAMFolder *folder = RAMFolder_new(NULL); FileHandle *fh; fh = RAMFolder_Open_FileHandle(folder, boffo, FH_CREATE | FH_WRITE_ONLY); DECREF(fh); TEST_TRUE(runner, RAMFolder_Local_Delete(folder, boffo), "Local_Delete on file succeeds"); RAMFolder_Local_MkDir(folder, foo); fh = RAMFolder_Open_FileHandle(folder, foo_boffo, FH_CREATE | FH_WRITE_ONLY); DECREF(fh); Err_set_error(NULL); TEST_FALSE(runner, RAMFolder_Local_Delete(folder, foo), "Local_Delete on non-empty dir fails"); RAMFolder_Delete(folder, foo_boffo); TEST_TRUE(runner, RAMFolder_Local_Delete(folder, foo), "Local_Delete on empty dir succeeds"); DECREF(folder); }
static void test_calc_proximity_boost(TestBatchRunner *runner) { VArray *spans = VA_new(0); HeatMap *heat_map = HeatMap_new(spans, 133); Span *span1 = Span_new( 0, 10, 1.0f); Span *span2 = Span_new( 10, 10, 1.0f); Span *span3 = Span_new( 5, 4, 1.0f); Span *span4 = Span_new(100, 10, 1.0f); Span *span5 = Span_new(150, 10, 1.0f); float big_boost = HeatMap_Calc_Proximity_Boost(heat_map, span1, span2); float eq_big_boost = HeatMap_Calc_Proximity_Boost(heat_map, span1, span3); float smaller_boost = HeatMap_Calc_Proximity_Boost(heat_map, span1, span4); float zero_boost = HeatMap_Calc_Proximity_Boost(heat_map, span1, span5); TEST_TRUE(runner, big_boost == eq_big_boost, "overlapping and abutting produce the same proximity boost"); TEST_TRUE(runner, big_boost > smaller_boost, "closer is better"); TEST_TRUE(runner, zero_boost == 0.0, "distance outside of window yields no prox boost"); DECREF(span1); DECREF(span2); DECREF(span3); DECREF(span4); DECREF(span5); DECREF(heat_map); DECREF(spans); }
static void test_Consolidate(TestBatchRunner *runner) { Folder *folder = S_folder_with_contents(); FileHandle *fh; // Fake up detritus from failed consolidation. fh = Folder_Open_FileHandle(folder, cf_file, FH_CREATE | FH_WRITE_ONLY | FH_EXCLUSIVE); DECREF(fh); fh = Folder_Open_FileHandle(folder, cfmeta_temp, FH_CREATE | FH_WRITE_ONLY | FH_EXCLUSIVE); DECREF(fh); CompoundFileWriter *cf_writer = CFWriter_new(folder); CFWriter_Consolidate(cf_writer); PASS(runner, "Consolidate completes despite leftover files"); DECREF(cf_writer); TEST_TRUE(runner, Folder_Exists(folder, cf_file), "cf.dat file written"); TEST_TRUE(runner, Folder_Exists(folder, cfmeta_file), "cfmeta.json file written"); TEST_FALSE(runner, Folder_Exists(folder, foo), "original file zapped"); TEST_FALSE(runner, Folder_Exists(folder, cfmeta_temp), "detritus from failed consolidation zapped"); DECREF(folder); }
static void test_numbers(TestBatchRunner *runner) { Integer *i64 = Int_new(33); String *json = Json_to_json((Obj*)i64); String *trimmed = Str_Trim(json); TEST_TRUE(runner, Str_Equals_Utf8(trimmed, "33", 2), "Integer"); DECREF(json); DECREF(trimmed); Float *f64 = Float_new(33.33); json = Json_to_json((Obj*)f64); if (json) { double value = Str_To_F64(json); double diff = 33.33 - value; if (diff < 0.0) { diff = 0.0 - diff; } TEST_TRUE(runner, diff < 0.0001, "Float"); DECREF(json); } else { FAIL(runner, "Float conversion to json failed."); } DECREF(i64); DECREF(f64); }
static void test_Dump_Load_and_Equals(TestBatch *batch) { CharBuf *EN = (CharBuf*)ZCB_WRAP_STR("en", 2); CharBuf *ES = (CharBuf*)ZCB_WRAP_STR("es", 2); Stemmer *stemmer = Stemmer_new(EN); Stemmer *other = Stemmer_new(ES); Obj *dump = (Obj*)Stemmer_Dump(stemmer); Obj *other_dump = (Obj*)Stemmer_Dump(other); Stemmer *clone = (Stemmer*)Stemmer_Load(other, dump); Stemmer *other_clone = (Stemmer*)Stemmer_Load(other, other_dump); TEST_FALSE(batch, Stemmer_Equals(stemmer, (Obj*)other), "Equals() false with different language"); TEST_TRUE(batch, Stemmer_Equals(stemmer, (Obj*)clone), "Dump => Load round trip"); TEST_TRUE(batch, Stemmer_Equals(other, (Obj*)other_clone), "Dump => Load round trip"); DECREF(stemmer); DECREF(dump); DECREF(clone); DECREF(other); DECREF(other_dump); DECREF(other_clone); }
static void test_all(TestBatchRunner *runner) { LockFreeRegistry *registry = LFReg_new(10); StupidHashCharBuf *foo = StupidHashCharBuf_new("foo"); StupidHashCharBuf *bar = StupidHashCharBuf_new("bar"); StupidHashCharBuf *baz = StupidHashCharBuf_new("baz"); StupidHashCharBuf *foo_dupe = StupidHashCharBuf_new("foo"); TEST_TRUE(runner, LFReg_Register(registry, (Obj*)foo, (Obj*)foo), "Register() returns true on success"); TEST_FALSE(runner, LFReg_Register(registry, (Obj*)foo_dupe, (Obj*)foo_dupe), "Can't Register() keys that test equal"); TEST_TRUE(runner, LFReg_Register(registry, (Obj*)bar, (Obj*)bar), "Register() key with the same Hash_Sum but that isn't Equal"); TEST_TRUE(runner, LFReg_Fetch(registry, (Obj*)foo_dupe) == (Obj*)foo, "Fetch()"); TEST_TRUE(runner, LFReg_Fetch(registry, (Obj*)bar) == (Obj*)bar, "Fetch() again"); TEST_TRUE(runner, LFReg_Fetch(registry, (Obj*)baz) == NULL, "Fetch() non-existent key returns NULL"); DECREF(foo_dupe); DECREF(baz); DECREF(bar); DECREF(foo); DECREF(registry); }
static void test_Dump_Load_and_Equals(TestBatchRunner *runner) { Query *a_leaf = (Query*)TestUtils_make_leaf_query(NULL, "a"); Query *b_leaf = (Query*)TestUtils_make_leaf_query(NULL, "b"); NOTQuery *query = NOTQuery_new(a_leaf); NOTQuery *kids_differ = NOTQuery_new(b_leaf); NOTQuery *boost_differs = NOTQuery_new(a_leaf); Obj *dump = (Obj*)NOTQuery_Dump(query); NOTQuery *clone = (NOTQuery*)Obj_Load(dump, dump); TEST_FALSE(runner, NOTQuery_Equals(query, (Obj*)kids_differ), "Different kids spoil Equals"); TEST_TRUE(runner, NOTQuery_Equals(query, (Obj*)boost_differs), "Equals with identical boosts"); NOTQuery_Set_Boost(boost_differs, 1.5); TEST_FALSE(runner, NOTQuery_Equals(query, (Obj*)boost_differs), "Different boost spoils Equals"); TEST_TRUE(runner, NOTQuery_Equals(query, (Obj*)clone), "Dump => Load round trip"); DECREF(a_leaf); DECREF(b_leaf); DECREF(query); DECREF(kids_differ); DECREF(boost_differs); DECREF(dump); DECREF(clone); }
static void test_Read_File_and_Write_File(TestBatchRunner *runner) { Snapshot *snapshot = Snapshot_new(); Folder *folder = (Folder*)RAMFolder_new(NULL); String *snap = (String*)SSTR_WRAP_UTF8("snap", 4); String *foo = (String*)SSTR_WRAP_UTF8("foo", 3); Snapshot_Add_Entry(snapshot, foo); Snapshot_Write_File(snapshot, folder, snap); Snapshot *dupe = Snapshot_new(); Snapshot *read_retval = Snapshot_Read_File(dupe, folder, snap); TEST_TRUE(runner, dupe == read_retval, "Read_File() returns the object"); VArray *orig_list = Snapshot_List(snapshot); VArray *dupe_list = Snapshot_List(dupe); TEST_TRUE(runner, VA_Equals(orig_list, (Obj*)dupe_list), "Round trip through Write_File() and Read_File()"); DECREF(orig_list); DECREF(dupe_list); DECREF(dupe); DECREF(snapshot); DECREF(folder); }
static void test_Cat(TestBatchRunner *runner) { String *wanted = Str_newf("a%s", smiley); String *source; String *got; source = S_get_str(""); got = Str_Cat(source, wanted); TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "Cat"); DECREF(got); DECREF(source); source = S_get_str("a"); got = Str_Cat_Utf8(source, smiley, smiley_len); TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "Cat_Utf8"); DECREF(got); DECREF(source); source = S_get_str("a"); got = Str_Cat_Trusted_Utf8(source, smiley, smiley_len); TEST_TRUE(runner, Str_Equals(wanted, (Obj*)got), "Cat_Trusted_Utf8"); DECREF(got); DECREF(source); DECREF(wanted); }
static void test_Local_Open_Dir(TestBatchRunner *runner) { Folder *real_folder = S_folder_with_contents(); CompoundFileReader *cf_reader = CFReader_open(real_folder); bool saw_foo = false; bool saw_stuff = false; bool stuff_was_dir = false; CFReader_MkDir(cf_reader, stuff); DirHandle *dh = CFReader_Local_Open_Dir(cf_reader); while (DH_Next(dh)) { String *entry = DH_Get_Entry(dh); if (Str_Equals(entry, (Obj*)foo)) { saw_foo = true; } else if (Str_Equals(entry, (Obj*)stuff)) { saw_stuff = true; stuff_was_dir = DH_Entry_Is_Dir(dh); } DECREF(entry); } TEST_TRUE(runner, saw_foo, "DirHandle iterated over virtual file"); TEST_TRUE(runner, saw_stuff, "DirHandle iterated over real directory"); TEST_TRUE(runner, stuff_was_dir, "DirHandle knew that real entry was dir"); DECREF(dh); DECREF(cf_reader); DECREF(real_folder); }
static void test_Local_Open_In(TestBatchRunner *runner) { Folder *real_folder = S_folder_with_contents(); CompoundFileReader *cf_reader = CFReader_open(real_folder); InStream *instream; instream = CFReader_Local_Open_In(cf_reader, foo); TEST_TRUE(runner, instream != NULL, "Local_Open_In for virtual file"); TEST_TRUE(runner, Str_Starts_With(InStream_Get_Filename(instream), CFReader_Get_Path(cf_reader)), "InStream's path includes directory"); DECREF(instream); OutStream *outstream = CFReader_Open_Out(cf_reader, baz); OutStream_Write_Bytes(outstream, "baz", 3); OutStream_Close(outstream); DECREF(outstream); instream = CFReader_Local_Open_In(cf_reader, baz); TEST_TRUE(runner, instream != NULL, "Local_Open_In pass-through for real file"); DECREF(instream); Err_set_error(NULL); instream = CFReader_Local_Open_In(cf_reader, stuff); TEST_TRUE(runner, instream == NULL, "Local_Open_In for non-existent file returns NULL"); TEST_TRUE(runner, Err_get_error() != NULL, "Local_Open_In for non-existent file sets global error"); DECREF(cf_reader); DECREF(real_folder); }
static void test_Mimic_and_Clone(TestBatchRunner *runner) { String *wanted = S_get_str("foo"); CharBuf *wanted_cb = S_get_cb("foo"); CharBuf *got = S_get_cb("bar"); CB_Mimic(got, (Obj*)wanted); TEST_TRUE(runner, S_cb_equals(got, wanted), "Mimic String"); DECREF(got); got = S_get_cb("bar"); CB_Mimic(got, (Obj*)wanted_cb); TEST_TRUE(runner, S_cb_equals(got, wanted), "Mimic CharBuf"); DECREF(got); got = S_get_cb("bar"); CB_Mimic_Utf8(got, "foo", 3); TEST_TRUE(runner, S_cb_equals(got, wanted), "Mimic_Utf8"); DECREF(got); got = CB_Clone(wanted_cb); TEST_TRUE(runner, S_cb_equals(got, wanted), "Clone"); DECREF(got); DECREF(wanted); DECREF(wanted_cb); }
static void test_Cat(TestBatchRunner *runner) { String *wanted = Str_newf("a%s", smiley); CharBuf *got = S_get_cb(""); CB_Cat(got, wanted); TEST_TRUE(runner, S_cb_equals(got, wanted), "Cat"); DECREF(got); got = S_get_cb("a"); CB_Cat_Char(got, 0x263A); TEST_TRUE(runner, S_cb_equals(got, wanted), "Cat_Char"); DECREF(got); got = S_get_cb("a"); CB_Cat_Utf8(got, smiley, smiley_len); TEST_TRUE(runner, S_cb_equals(got, wanted), "Cat_Utf8"); DECREF(got); got = S_get_cb("a"); CB_Cat_Trusted_Utf8(got, smiley, smiley_len); TEST_TRUE(runner, S_cb_equals(got, wanted), "Cat_Trusted_Utf8"); DECREF(got); DECREF(wanted); }
static void test_Equals(TestBatchRunner *runner) { ByteBuf *bb = BB_new_bytes("foo", 4); // Include terminating NULL. { ByteBuf *other = BB_new_bytes("foo", 4); TEST_TRUE(runner, BB_Equals(bb, (Obj*)other), "Equals"); DECREF(other); } TEST_TRUE(runner, BB_Equals_Bytes(bb, "foo", 4), "Equals_Bytes"); TEST_FALSE(runner, BB_Equals_Bytes(bb, "foo", 3), "Equals_Bytes spoiled by different size"); TEST_FALSE(runner, BB_Equals_Bytes(bb, "bar", 4), "Equals_Bytes spoiled by different content"); { ByteBuf *other = BB_new_bytes("foo", 3); TEST_FALSE(runner, BB_Equals(bb, (Obj*)other), "Different size spoils Equals"); DECREF(other); } { ByteBuf *other = BB_new_bytes("bar", 4); TEST_UINT_EQ(runner, BB_Get_Size(bb), BB_Get_Size(other), "same length"); TEST_FALSE(runner, BB_Equals(bb, (Obj*)other), "Different content spoils Equals"); DECREF(other); } DECREF(bb); }
static void test_Clone(TestBatch *batch) { Float32 *f32 = Float32_new(1.33f); Float64 *f64 = Float64_new(1.33); Integer32 *i32 = Int32_new(I32_MAX); Integer64 *i64 = Int64_new(I64_MAX); Float32 *f32_dupe = Float32_Clone(f32); Float64 *f64_dupe = Float64_Clone(f64); Integer32 *i32_dupe = Int32_Clone(i32); Integer64 *i64_dupe = Int64_Clone(i64); TEST_TRUE(batch, Float32_Equals(f32, (Obj*)f32_dupe), "Float32 Clone"); TEST_TRUE(batch, Float64_Equals(f64, (Obj*)f64_dupe), "Float64 Clone"); TEST_TRUE(batch, Int32_Equals(i32, (Obj*)i32_dupe), "Integer32 Clone"); TEST_TRUE(batch, Int64_Equals(i64, (Obj*)i64_dupe), "Integer64 Clone"); TEST_TRUE(batch, Bool_Equals(CFISH_TRUE, (Obj*)Bool_Clone(CFISH_TRUE)), "BoolNum Clone"); DECREF(i64_dupe); DECREF(i32_dupe); DECREF(f64_dupe); DECREF(f32_dupe); DECREF(i64); DECREF(i32); DECREF(f64); DECREF(f32); }
static void test_Contains_and_Find(TestBatchRunner *runner) { String *string; String *substring = S_get_str("foo"); string = S_get_str(""); TEST_FALSE(runner, Str_Contains(string, substring), "Not contained in empty string"); TEST_INT_EQ(runner, S_find(string, substring), -1, "Not found in empty string"); DECREF(string); string = S_get_str("foo"); TEST_TRUE(runner, Str_Contains(string, substring), "Contained in complete string"); TEST_INT_EQ(runner, S_find(string, substring), 0, "Find complete string"); DECREF(string); string = S_get_str("afoo"); TEST_TRUE(runner, Str_Contains(string, substring), "Contained after first"); TEST_INT_EQ(runner, S_find(string, substring), 1, "Find after first"); String *prefix = Str_SubString(string, 0, 3); TEST_FALSE(runner, Str_Contains(prefix, substring), "Don't overrun"); DECREF(prefix); DECREF(string); string = S_get_str("afood"); TEST_TRUE(runner, Str_Contains(string, substring), "Contained in middle"); TEST_INT_EQ(runner, S_find(string, substring), 1, "Find in middle"); DECREF(string); DECREF(substring); }
static void test_Mimic(TestBatch *batch) { Float32 *f32 = Float32_new(1.33f); Float64 *f64 = Float64_new(1.33); Integer32 *i32 = Int32_new(I32_MAX); Integer64 *i64 = Int64_new(I64_MAX); Float32 *f32_dupe = Float32_new(0.0f); Float64 *f64_dupe = Float64_new(0.0); Integer32 *i32_dupe = Int32_new(0); Integer64 *i64_dupe = Int64_new(0); Float32_Mimic(f32_dupe, (Obj*)f32); Float64_Mimic(f64_dupe, (Obj*)f64); Int32_Mimic(i32_dupe, (Obj*)i32); Int64_Mimic(i64_dupe, (Obj*)i64); TEST_TRUE(batch, Float32_Equals(f32, (Obj*)f32_dupe), "Float32 Mimic"); TEST_TRUE(batch, Float64_Equals(f64, (Obj*)f64_dupe), "Float64 Mimic"); TEST_TRUE(batch, Int32_Equals(i32, (Obj*)i32_dupe), "Integer32 Mimic"); TEST_TRUE(batch, Int64_Equals(i64, (Obj*)i64_dupe), "Integer64 Mimic"); DECREF(i64_dupe); DECREF(i32_dupe); DECREF(f64_dupe); DECREF(f32_dupe); DECREF(i64); DECREF(i32); DECREF(f64); DECREF(f32); }
static void test_To_F64(TestBatchRunner *runner) { String *string; string = S_get_str("1.5"); double difference = 1.5 - Str_To_F64(string); if (difference < 0) { difference = 0 - difference; } TEST_TRUE(runner, difference < 0.001, "To_F64"); DECREF(string); string = S_get_str("-1.5"); difference = 1.5 + Str_To_F64(string); if (difference < 0) { difference = 0 - difference; } TEST_TRUE(runner, difference < 0.001, "To_F64 negative"); DECREF(string); // TODO: Enable this test when we have real substrings. /*string = S_get_str("1.59"); double value_full = Str_To_F64(string); Str_Set_Size(string, 3); double value_short = Str_To_F64(string); TEST_TRUE(runner, value_short < value_full, "TO_F64 doesn't run past end of string"); DECREF(string);*/ }
static void test_Set_and_Get(TestBatch *batch) { unsigned i, max; const uint32_t three = 3; const uint32_t seventeen = 17; BitVector *bit_vec = BitVec_new(8); BitVec_Set(bit_vec, three); TEST_TRUE(batch, BitVec_Get_Capacity(bit_vec) < seventeen, "set below cap"); BitVec_Set(bit_vec, seventeen); TEST_TRUE(batch, BitVec_Get_Capacity(bit_vec) > seventeen, "set above cap causes BitVector to grow"); for (i = 0, max = BitVec_Get_Capacity(bit_vec); i < max; i++) { if (i == three || i == seventeen) { TEST_TRUE(batch, BitVec_Get(bit_vec, i), "set/get %d", i); } else { TEST_FALSE(batch, BitVec_Get(bit_vec, i), "get %d", i); } } TEST_FALSE(batch, BitVec_Get(bit_vec, i), "out of range get"); DECREF(bit_vec); }
static void test_To_I64(TestBatchRunner *runner) { String *string; string = S_get_str("10"); TEST_TRUE(runner, Str_To_I64(string) == 10, "To_I64"); DECREF(string); string = S_get_str("-10"); TEST_TRUE(runner, Str_To_I64(string) == -10, "To_I64 negative"); DECREF(string); string = S_get_str("10."); TEST_TRUE(runner, Str_To_I64(string) == 10, "To_I64 stops at non-digits"); DECREF(string); string = S_get_str("10A"); TEST_TRUE(runner, Str_To_I64(string) == 10, "To_I64 stops at out-of-range digits"); DECREF(string); string = S_get_str("-JJ"); TEST_TRUE(runner, Str_BaseX_To_I64(string, 20) == -399, "BaseX_To_I64 base 20"); DECREF(string); }
static void test_Equals(TestBatchRunner *runner) { ByteBuf *wanted = BB_new_bytes("foo", 4); // Include terminating NULL. ByteBuf *got = BB_new_bytes("foo", 4); TEST_TRUE(runner, BB_Equals(wanted, (Obj*)got), "Equals"); TEST_INT_EQ(runner, BB_Hash_Sum(got), BB_Hash_Sum(wanted), "Hash_Sum"); TEST_TRUE(runner, BB_Equals_Bytes(got, "foo", 4), "Equals_Bytes"); TEST_FALSE(runner, BB_Equals_Bytes(got, "foo", 3), "Equals_Bytes spoiled by different size"); TEST_FALSE(runner, BB_Equals_Bytes(got, "bar", 4), "Equals_Bytes spoiled by different content"); BB_Set_Size(got, 3); TEST_FALSE(runner, BB_Equals(wanted, (Obj*)got), "Different size spoils Equals"); TEST_FALSE(runner, BB_Hash_Sum(got) == BB_Hash_Sum(wanted), "Different size spoils Hash_Sum (probably -- at least this one)"); BB_Mimic_Bytes(got, "bar", 4); TEST_INT_EQ(runner, BB_Get_Size(wanted), BB_Get_Size(got), "same length"); TEST_FALSE(runner, BB_Equals(wanted, (Obj*)got), "Different content spoils Equals"); DECREF(got); DECREF(wanted); }
static void test_Exists(TestBatch *batch) { Folder *folder = (Folder*)RAMFolder_new(NULL); FileHandle *fh; Folder_MkDir(folder, &foo); Folder_MkDir(folder, &foo_bar); fh = Folder_Open_FileHandle(folder, &boffo, FH_CREATE | FH_WRITE_ONLY); DECREF(fh); fh = Folder_Open_FileHandle(folder, &foo_boffo, FH_CREATE | FH_WRITE_ONLY); DECREF(fh); TEST_TRUE(batch, Folder_Exists(folder, &foo), "Dir exists"); TEST_TRUE(batch, Folder_Exists(folder, &boffo), "File exists"); TEST_TRUE(batch, Folder_Exists(folder, &foo_bar), "Nested dir exists"); TEST_TRUE(batch, Folder_Exists(folder, &foo_boffo), "Nested file exists"); TEST_FALSE(batch, Folder_Exists(folder, &banana), "Non-existent entry"); TEST_FALSE(batch, Folder_Exists(folder, &foo_foo), "Non-existent nested entry"); DECREF(folder); }
static void test_back_utf8_char(TestBatchRunner *runner) { char buffer[4]; char *buf = buffer + 1; uint32_t len = Str_encode_utf8_char(0x263A, buffer); char *end = buffer + len; TEST_TRUE(runner, StrHelp_back_utf8_char(end, buffer) == buffer, "back_utf8_char"); TEST_TRUE(runner, StrHelp_back_utf8_char(end, buf) == NULL, "back_utf8_char returns NULL rather than back up beyond start"); TEST_TRUE(runner, StrHelp_back_utf8_char(buffer, buffer) == NULL, "back_utf8_char returns NULL when end == start"); int32_t code_point; for (code_point = 0; code_point <= 0x10FFFF; code_point++) { uint32_t size = Str_encode_utf8_char(code_point, buffer); char *start = buffer; char *end = start + size; if (StrHelp_back_utf8_char(end, start) != start) { break; } } if (code_point == 0x110000) { PASS(runner, "back_utf8_char works for code points 0 - 0x10FFFF"); } else { FAIL(runner, "Failed back_utf8_char at 0x%.1X", (unsigned)code_point); } }
static void test_Dump_Load_and_Equals(TestBatchRunner *runner) { Query *a_leaf = (Query*)TestUtils_make_leaf_query(NULL, "a"); Query *b_leaf = (Query*)TestUtils_make_leaf_query(NULL, "b"); Query *c_leaf = (Query*)TestUtils_make_leaf_query(NULL, "c"); RequiredOptionalQuery *query = ReqOptQuery_new(a_leaf, b_leaf); RequiredOptionalQuery *kids_differ = ReqOptQuery_new(a_leaf, c_leaf); RequiredOptionalQuery *boost_differs = ReqOptQuery_new(a_leaf, b_leaf); Obj *dump = (Obj*)ReqOptQuery_Dump(query); RequiredOptionalQuery *clone = (RequiredOptionalQuery*)Freezer_load(dump); TEST_FALSE(runner, ReqOptQuery_Equals(query, (Obj*)kids_differ), "Different kids spoil Equals"); TEST_TRUE(runner, ReqOptQuery_Equals(query, (Obj*)boost_differs), "Equals with identical boosts"); ReqOptQuery_Set_Boost(boost_differs, 1.5); TEST_FALSE(runner, ReqOptQuery_Equals(query, (Obj*)boost_differs), "Different boost spoils Equals"); TEST_TRUE(runner, ReqOptQuery_Equals(query, (Obj*)clone), "Dump => Load round trip"); DECREF(a_leaf); DECREF(b_leaf); DECREF(c_leaf); DECREF(query); DECREF(kids_differ); DECREF(boost_differs); DECREF(dump); DECREF(clone); }
static void test_Close(TestBatchRunner *runner) { String *test_filename = SSTR_WRAP_C("_fstest"); FSFileHandle *fh; S_remove(test_filename); fh = FSFH_open(test_filename, FH_CREATE | FH_WRITE_ONLY | FH_EXCLUSIVE); TEST_TRUE(runner, FSFH_Close(fh), "Close returns true for write-only"); DECREF(fh); // Simulate an OS error when closing the file descriptor. This // approximates what would happen if, say, we run out of disk space. S_remove(test_filename); fh = FSFH_open(test_filename, FH_CREATE | FH_WRITE_ONLY | FH_EXCLUSIVE); #ifdef _MSC_VER SKIP(runner, 2, "LUCY-155"); #else int saved_fd = FSFH_Set_FD(fh, -1); Err_set_error(NULL); bool result = FSFH_Close(fh); TEST_FALSE(runner, result, "Failed Close() returns false"); TEST_TRUE(runner, Err_get_error() != NULL, "Failed Close() sets global error"); FSFH_Set_FD(fh, saved_fd); #endif /* _MSC_VER */ DECREF(fh); fh = FSFH_open(test_filename, FH_READ_ONLY); TEST_TRUE(runner, FSFH_Close(fh), "Close returns true for read-only"); DECREF(fh); S_remove(test_filename); }
static void test_Dump_Load_and_Equals(TestBatchRunner *runner) { String *EN = (String*)SSTR_WRAP_UTF8("en", 2); String *ES = (String*)SSTR_WRAP_UTF8("es", 2); SnowballStemmer *stemmer = SnowStemmer_new(EN); SnowballStemmer *other = SnowStemmer_new(ES); Obj *dump = (Obj*)SnowStemmer_Dump(stemmer); Obj *other_dump = (Obj*)SnowStemmer_Dump(other); SnowballStemmer *clone = (SnowballStemmer*)SnowStemmer_Load(other, dump); SnowballStemmer *other_clone = (SnowballStemmer*)SnowStemmer_Load(other, other_dump); TEST_FALSE(runner, SnowStemmer_Equals(stemmer, (Obj*)other), "Equals() false with different language"); TEST_TRUE(runner, SnowStemmer_Equals(stemmer, (Obj*)clone), "Dump => Load round trip"); TEST_TRUE(runner, SnowStemmer_Equals(other, (Obj*)other_clone), "Dump => Load round trip"); DECREF(stemmer); DECREF(dump); DECREF(clone); DECREF(other); DECREF(other_dump); DECREF(other_clone); }
static void test_node_read_deep_stack(void) { static const int depth = 1200; char buf[4096]; uint8_t* p = (uint8_t*)buf; for (int i = 0; i < depth; ++i) { *p++ = 0x81; // one pair map *p++ = 0x04; // key four *p++ = 0x91; // value one element array } *p++ = 0x07; // final array value seven mpack_tree_t tree; TEST_TREE_INIT(&tree, buf, (size_t)(p - (uint8_t*)buf)); #ifdef MPACK_MALLOC mpack_node_t node = mpack_tree_root(&tree); for (int i = 0; i < depth; ++i) { TEST_TRUE(mpack_tree_error(&tree) == mpack_ok, "error at depth %i", i); TEST_TRUE(mpack_node_map_count(node) == 1, "error at depth %i", i); TEST_TRUE(mpack_node_u8(mpack_node_map_key_at(node, 0)) == 4, "error at depth %i", i); TEST_TRUE(mpack_node_array_length(mpack_node_map_value_at(node, 0)) == 1, "error at depth %i", i); node = mpack_node_array_at(mpack_node_map_value_at(node, 0), 0); } TEST_TRUE(mpack_node_u8(node) == 7, "error in final node"); TEST_TREE_DESTROY_NOERROR(&tree); #else TEST_TREE_DESTROY_ERROR(&tree, mpack_error_too_big); #endif }
static void test_Write_File_and_Read_File(TestBatchRunner *runner) { RAMFolder *folder = RAMFolder_new(NULL); Segment *segment = Seg_new(100); Segment *got = Seg_new(100); String *meta; String *flotsam = (String*)SSTR_WRAP_UTF8("flotsam", 7); String *jetsam = (String*)SSTR_WRAP_UTF8("jetsam", 6); Seg_Set_Count(segment, 111); Seg_Store_Metadata_Utf8(segment, "foo", 3, (Obj*)Str_newf("bar")); Seg_Add_Field(segment, flotsam); Seg_Add_Field(segment, jetsam); RAMFolder_MkDir(folder, Seg_Get_Name(segment)); Seg_Write_File(segment, (Folder*)folder); Seg_Read_File(got, (Folder*)folder); TEST_TRUE(runner, Seg_Get_Count(got) == Seg_Get_Count(segment), "Round-trip count through file"); TEST_TRUE(runner, Seg_Field_Num(got, jetsam) == Seg_Field_Num(segment, jetsam), "Round trip field names through file"); meta = (String*)Seg_Fetch_Metadata_Utf8(got, "foo", 3); TEST_TRUE(runner, meta && Str_Is_A(meta, STRING) && Str_Equals_Utf8(meta, "bar", 3), "Round trip metadata through file"); DECREF(got); DECREF(segment); DECREF(folder); }
static void test_cas_ptr(TestBatch *batch) { int foo = 1; int bar = 2; int *foo_pointer = &foo; int *bar_pointer = &bar; int *target = NULL; TEST_TRUE(batch, Atomic_cas_ptr((void**)&target, NULL, foo_pointer), "cas_ptr returns true on success"); TEST_TRUE(batch, target == foo_pointer, "cas_ptr sets target"); target = NULL; TEST_FALSE(batch, Atomic_cas_ptr((void**)&target, bar_pointer, foo_pointer), "cas_ptr returns false when it old_value doesn't match"); TEST_TRUE(batch, target == NULL, "cas_ptr doesn't do anything to target when old_value doesn't match"); target = foo_pointer; TEST_TRUE(batch, Atomic_cas_ptr((void**)&target, foo_pointer, bar_pointer), "cas_ptr from one value to another"); TEST_TRUE(batch, target == bar_pointer, "cas_ptr sets target"); }
static void test_Local_MkDir(TestBatchRunner *runner) { RAMFolder *folder = RAMFolder_new(NULL); bool result; result = RAMFolder_Local_MkDir(folder, foo); TEST_TRUE(runner, result, "Local_MkDir succeeds and returns true"); Err_set_error(NULL); result = RAMFolder_Local_MkDir(folder, foo); TEST_FALSE(runner, result, "Local_MkDir returns false when a dir already exists"); TEST_TRUE(runner, Err_get_error() != NULL, "Local_MkDir sets Err_error when a dir already exists"); TEST_TRUE(runner, RAMFolder_Exists(folder, foo), "Existing dir untouched after failed Local_MkDir"); FileHandle *fh = RAMFolder_Open_FileHandle(folder, boffo, FH_CREATE | FH_WRITE_ONLY); DECREF(fh); Err_set_error(NULL); result = RAMFolder_Local_MkDir(folder, foo); TEST_FALSE(runner, result, "Local_MkDir returns false when a file already exists"); TEST_TRUE(runner, Err_get_error() != NULL, "Local_MkDir sets Err_error when a file already exists"); TEST_TRUE(runner, RAMFolder_Exists(folder, boffo) && !RAMFolder_Local_Is_Directory(folder, boffo), "Existing file untouched after failed Local_MkDir"); DECREF(folder); }