/** * Runs a tests to make sure the text-store works. */ #include #include #include int main() { textstore_t store; if(textstore_init(&store, 64)) return 1; // A reference counted string handle. char *handle1_expected = "Hello world!"; textstore_handle handle1 = textstore_intern(&store, handle1_expected); if (strcmp(textstore_string(&store, handle1), handle1_expected) != 0) return 1; char *handle2_expected = "A different string!"; textstore_handle handle2 = textstore_intern(&store, handle2_expected); if (strcmp(textstore_string(&store, handle2), handle2_expected)) return 1; // A handle may become invalidated after all references to it are released. textstore_release(&store, handle1); if (textstore_gc(&store) != 0) return 1; // Trying to get the data of an inactive handle should return null. char *invalid_string = textstore_string(&store, handle1); if (invalid_string != NULL) return 1; // Interning new strings may re-use previously gc'd handle indexes. // But they are not equal due to their cycle bits. textstore_handle handle3 = textstore_intern(&store, "Hello world."); if (handle1 == handle3) return 1; // And trying to access a previously gc'd handle will still return an error, // even if its index is in use. invalid_string = textstore_string(&store, handle1); if (invalid_string != NULL) return 1; // Likewise, interning the same string will result in identical handles. textstore_handle handle4 = textstore_intern(&store, "Hello world."); if (handle3 != handle4) return 1; // It's also possible to intern strings that aren't null-terminated. textstore_handle handle5 = textstore_intern_len(&store, &(char){'.'}, 1); // But the internally stored string is null-terminated for ecosystem compatibility. if (strcmp(textstore_string(&store, handle5), ".") != 0) return 1; { textstore_handle temp_handle; temp_handle = textstore_intern(&store, "blah blah"); textstore_release(&store, temp_handle); temp_handle = textstore_intern(&store, "random text"); textstore_release(&store, temp_handle); temp_handle = textstore_intern(&store, "text"); textstore_release(&store, temp_handle); temp_handle = textstore_intern(&store, "random text2"); textstore_release(&store, temp_handle); } textstore_handle handle6 = textstore_intern(&store, "All your bases..."); textstore_gc(&store); // Even after a GC and the underlying text being invalidated and shifted // about, valid handles can still be used. if (strcmp(textstore_string(&store, handle6), "All your bases...") != 0) return 1; textstore_free(&store); printf("All tests passed.\n"); return 0; }