#include #include #include #include "../src/piece-table-internals.h" size_t count_pieces (piece_table *pt) { size_t count; piece *p; for (count = 0, p = pt->sentinel->next; p != pt->sentinel; p = p->next) count++; return count; } int main () { char tmp[100]; size_t expected_count; piece_table *pt = piece_table_create_from_str ("0123456789", 10); /** Inserting **/ /* Should add pieces... */ expected_count = 1; if (count_pieces (pt) != expected_count) return 10; piece_table_insert (pt, 10, "abcdefgh", 8); expected_count++; if (count_pieces (pt) != expected_count) return 11; /* ... but not always **/ piece_table_insert (pt, 18, "abcdefgh", 8); if (count_pieces (pt) != expected_count) return 12; /** Deleting **/ piece_table_delete (pt, 23, 3); if (count_pieces (pt) != expected_count) return 20; piece_table_delete (pt, 13, 4); expected_count++; if (count_pieces (pt) != expected_count) return 21; piece_table_delete (pt, 0, 10); expected_count--; if (count_pieces (pt) != expected_count) return 22; /** Result **/ piece_table_to_string (pt, tmp, 99); if (strcmp (tmp, "abchabcde")) return 30; /** Optimize **/ piece_table_optimize (pt); expected_count = 1; if (count_pieces (pt) != expected_count) return 40; /** Result **/ piece_table_to_string (pt, tmp, 99); if (strcmp (tmp, "abchabcde")) return 50; piece_table_destroy (pt); return 0; }