static bool S_is_updir(CharBuf *path) { if (CB_Equals_Str(path, ".", 1) || CB_Equals_Str(path, "..", 2)) { return true; } else { return false; } }
static void test_seg_name_and_num(TestBatch *batch) { Segment *segment_z = Seg_new(35); CharBuf *seg_z_name = Seg_num_to_name(35); TEST_TRUE(batch, Seg_Get_Number(segment_z) == I64_C(35), "Get_Number"); TEST_TRUE(batch, CB_Equals_Str(Seg_Get_Name(segment_z), "seg_z", 5), "Get_Name"); TEST_TRUE(batch, CB_Equals_Str(seg_z_name, "seg_z", 5), "num_to_name"); DECREF(seg_z_name); DECREF(segment_z); }
Normalizer* Normalizer_init(Normalizer *self, const CharBuf *form, bool_t case_fold, bool_t strip_accents) { int options = UTF8PROC_STABLE; if (form == NULL || CB_Equals_Str(form, "NFKC", 4) || CB_Equals_Str(form, "nfkc", 4) ) { options |= UTF8PROC_COMPOSE | UTF8PROC_COMPAT; } else if (CB_Equals_Str(form, "NFC", 3) || CB_Equals_Str(form, "nfc", 3)) { options |= UTF8PROC_COMPOSE; } else if (CB_Equals_Str(form, "NFKD", 4) || CB_Equals_Str(form, "nfkd", 4)) { options |= UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT; } else if (CB_Equals_Str(form, "NFD", 3) || CB_Equals_Str(form, "nfd", 3)) { options |= UTF8PROC_DECOMPOSE; } else { THROW(ERR, "Invalid normalization form %o", form); } if (case_fold) { options |= UTF8PROC_CASEFOLD; } if (strip_accents) { options |= UTF8PROC_STRIPMARK; } self->options = options; return self; }
static void test_Write_File_and_Read_File(TestBatch *batch) { RAMFolder *folder = RAMFolder_new(NULL); Segment *segment = Seg_new(100); Segment *got = Seg_new(100); CharBuf *meta; CharBuf *flotsam = (CharBuf*)ZCB_WRAP_STR("flotsam", 7); CharBuf *jetsam = (CharBuf*)ZCB_WRAP_STR("jetsam", 6); Seg_Set_Count(segment, 111); Seg_Store_Metadata_Str(segment, "foo", 3, (Obj*)CB_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(batch, Seg_Get_Count(got) == Seg_Get_Count(segment), "Round-trip count through file"); TEST_TRUE(batch, Seg_Field_Num(got, jetsam) == Seg_Field_Num(segment, jetsam), "Round trip field names through file"); meta = (CharBuf*)Seg_Fetch_Metadata_Str(got, "foo", 3); TEST_TRUE(batch, meta && CB_Is_A(meta, CHARBUF) && CB_Equals_Str(meta, "bar", 3), "Round trip metadata through file"); DECREF(got); DECREF(segment); DECREF(folder); }
NumericType* NumType_load(NumericType *self, Obj *dump) { UNUSED_VAR(self); Hash *source = (Hash*)CERTIFY(dump, HASH); // Get a VTable CharBuf *class_name = (CharBuf*)Hash_Fetch_Str(source, "_class", 6); CharBuf *type_spec = (CharBuf*)Hash_Fetch_Str(source, "type", 4); VTable *vtable = NULL; if (class_name != NULL && Obj_Is_A((Obj*)class_name, CHARBUF)) { vtable = VTable_singleton(class_name, NULL); } else if (type_spec != NULL && Obj_Is_A((Obj*)type_spec, CHARBUF)) { if (CB_Equals_Str(type_spec, "i32_t", 5)) { vtable = INT32TYPE; } else if (CB_Equals_Str(type_spec, "i64_t", 5)) { vtable = INT64TYPE; } else if (CB_Equals_Str(type_spec, "f32_t", 5)) { vtable = FLOAT32TYPE; } else if (CB_Equals_Str(type_spec, "f64_t", 5)) { vtable = FLOAT64TYPE; } else { THROW(ERR, "Unrecognized type string: '%o'", type_spec); } } CERTIFY(vtable, VTABLE); NumericType *loaded = (NumericType*)VTable_Make_Obj(vtable); // Extract boost. Obj *boost_dump = Hash_Fetch_Str(source, "boost", 5); float boost = boost_dump ? (float)Obj_To_F64(boost_dump) : 1.0f; // Find boolean properties. Obj *indexed_dump = Hash_Fetch_Str(source, "indexed", 7); Obj *stored_dump = Hash_Fetch_Str(source, "stored", 6); Obj *sort_dump = Hash_Fetch_Str(source, "sortable", 8); bool_t indexed = indexed_dump ? (bool_t)Obj_To_I64(indexed_dump) : true; bool_t stored = stored_dump ? (bool_t)Obj_To_I64(stored_dump) : true; bool_t sortable = sort_dump ? (bool_t)Obj_To_I64(sort_dump) : false; return NumType_init2(loaded, boost, indexed, stored, sortable); }
static void test_Trim(TestBatch *batch) { uint32_t spaces[] = { ' ', '\t', '\r', '\n', 0x000B, 0x000C, 0x000D, 0x0085, 0x00A0, 0x1680, 0x180E, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000 }; uint32_t num_spaces = sizeof(spaces) / sizeof(uint32_t); uint32_t i; CharBuf *got = CB_new(0); // Surround a smiley with lots of whitespace. for (i = 0; i < num_spaces; i++) { CB_Cat_Char(got, spaces[i]); } CB_Cat_Char(got, 0x263A); for (i = 0; i < num_spaces; i++) { CB_Cat_Char(got, spaces[i]); } TEST_TRUE(batch, CB_Trim_Top(got), "Trim_Top returns true on success"); TEST_FALSE(batch, CB_Trim_Top(got), "Trim_Top returns false on failure"); TEST_TRUE(batch, CB_Trim_Tail(got), "Trim_Tail returns true on success"); TEST_FALSE(batch, CB_Trim_Tail(got), "Trim_Tail returns false on failure"); TEST_TRUE(batch, CB_Equals_Str(got, smiley, smiley_len), "Trim_Top and Trim_Tail worked"); // Build the spacey smiley again. CB_Truncate(got, 0); for (i = 0; i < num_spaces; i++) { CB_Cat_Char(got, spaces[i]); } CB_Cat_Char(got, 0x263A); for (i = 0; i < num_spaces; i++) { CB_Cat_Char(got, spaces[i]); } TEST_TRUE(batch, CB_Trim(got), "Trim returns true on success"); TEST_FALSE(batch, CB_Trim(got), "Trim returns false on failure"); TEST_TRUE(batch, CB_Equals_Str(got, smiley, smiley_len), "Trim worked"); DECREF(got); }
static void test_To_String(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); CharBuf *f32_string = Float32_To_String(f32); CharBuf *f64_string = Float64_To_String(f64); CharBuf *i32_string = Int32_To_String(i32); CharBuf *i64_string = Int64_To_String(i64); CharBuf *true_string = Bool_To_String(CFISH_TRUE); CharBuf *false_string = Bool_To_String(CFISH_FALSE); TEST_TRUE(batch, CB_Starts_With_Str(f32_string, "1.3", 3), "Float32_To_String"); TEST_TRUE(batch, CB_Starts_With_Str(f64_string, "1.3", 3), "Float64_To_String"); TEST_TRUE(batch, CB_Equals_Str(i32_string, "2147483647", 10), "Int32_To_String"); TEST_TRUE(batch, CB_Equals_Str(i64_string, "9223372036854775807", 19), "Int64_To_String"); TEST_TRUE(batch, CB_Equals_Str(true_string, "true", 4), "Bool_To_String [true]"); TEST_TRUE(batch, CB_Equals_Str(false_string, "false", 5), "Bool_To_String [false]"); DECREF(false_string); DECREF(true_string); DECREF(i64_string); DECREF(i32_string); DECREF(f64_string); DECREF(f32_string); DECREF(i64); DECREF(i32); DECREF(f64); DECREF(f32); }
static void test_metadata_storage(TestBatch *batch) { Segment *segment = Seg_new(1); CharBuf *got; Seg_Store_Metadata_Str(segment, "foo", 3, (Obj*)CB_newf("bar")); got = (CharBuf*)Seg_Fetch_Metadata_Str(segment, "foo", 3); TEST_TRUE(batch, got && CB_Is_A(got, CHARBUF) && CB_Equals_Str(got, "bar", 3), "metadata round trip"); DECREF(segment); }
static void test_Local_Find_Folder(TestBatchRunner *runner) { RAMFolder *folder = RAMFolder_new(NULL); RAMFolder *local; FileHandle *fh; RAMFolder_MkDir(folder, foo); RAMFolder_MkDir(folder, foo_bar); fh = RAMFolder_Open_FileHandle(folder, boffo, FH_CREATE | FH_WRITE_ONLY); DECREF(fh); fh = RAMFolder_Open_FileHandle(folder, foo_boffo, FH_CREATE | FH_WRITE_ONLY); DECREF(fh); local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, nope); TEST_TRUE(runner, local == NULL, "Non-existent entry yields NULL"); ZombieCharBuf *empty = ZCB_BLANK(); local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, (CharBuf*)empty); TEST_TRUE(runner, local == NULL, "Empty string yields NULL"); local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, foo_bar); TEST_TRUE(runner, local == NULL, "nested folder yields NULL"); local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, foo_boffo); TEST_TRUE(runner, local == NULL, "nested file yields NULL"); local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, boffo); TEST_TRUE(runner, local == NULL, "local file yields NULL"); local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, bar); TEST_TRUE(runner, local == NULL, "name of nested folder yields NULL"); local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, foo); TEST_TRUE(runner, local && RAMFolder_Is_A(local, RAMFOLDER) && CB_Equals_Str(RAMFolder_Get_Path(local), "foo", 3), "Find local directory"); DECREF(folder); }
static void test_numbers(TestBatch *batch) { Integer64 *i64 = Int64_new(33); CharBuf *json = Json_to_json((Obj*)i64); CB_Trim(json); TEST_TRUE(batch, json && CB_Equals_Str(json, "33", 2), "Integer"); DECREF(json); Float64 *f64 = Float64_new(33.33); json = Json_to_json((Obj*)f64); if (json) { double value = CB_To_F64(json); double diff = 33.33 - value; if (diff < 0.0) { diff = 0.0 - diff; } TEST_TRUE(batch, diff < 0.0001, "Float"); DECREF(json); } else { FAIL(batch, "Float conversion to json failed."); } DECREF(i64); DECREF(f64); }