static void add_generator (unsigned *n_inout, DskJsonValue ***arr_inout, unsigned *alloced_inout, int px, int py, unsigned update_number) { DskJsonMember members[6]; static const char *colors[] = { "#ffffff", "#ff0000", "#00ff00", "#2222ff", "#ff00ff", "#00ffff", "#ffff00" }; members[0].name = "x"; members[0].value = dsk_json_value_new_number (px - TILE_SIZE * 7 / 8); members[1].name = "y"; members[1].value = dsk_json_value_new_number (py - TILE_SIZE * 7 / 8); members[2].name = "width"; members[2].value = dsk_json_value_new_number (TILE_SIZE * 7 / 4); members[3].name = "height"; members[3].value = dsk_json_value_new_number (TILE_SIZE * 7 / 4); members[4].name = "color"; members[4].value = dsk_json_value_new_string (7, (char*) colors[update_number % DSK_N_ELEMENTS (colors)]); members[5].name = "type"; members[5].value = dsk_json_value_new_string (10, "hollow_box"); append_element_json (n_inout, arr_inout, alloced_inout, dsk_json_value_new_object (DSK_N_ELEMENTS (members), members)); }
static void test_various_read_write (void) { unsigned i; for (i = 0; i < DSK_N_ELEMENTS (test_datasets); i++) test_various_read_write_1 (test_datasets[i].name, test_datasets[i].n_entries, test_datasets[i].entries, test_datasets[i].to_write); if (cmdline_slow) for (i = 0; i < DSK_N_ELEMENTS (test_slow_datasets); i++) test_various_read_write_1 (test_slow_datasets[i].name, test_slow_datasets[i].n_entries, test_slow_datasets[i].entries, test_slow_datasets[i].to_write); }
int main(int argc, char **argv) { unsigned port = 0; DskHttpServer *server; unsigned i; DskError *error = NULL; dsk_cmdline_init ("snipez server", "Run a snipez server", NULL, 0); dsk_cmdline_add_uint ("port", "Port Number", "PORT", DSK_CMDLINE_MANDATORY, &port); dsk_cmdline_add_uint ("update-period", "Update Period", "MILLIS", 0, &update_period_msecs); dsk_cmdline_add_func ("make-maze", "Make a Maze", "WIDTHxHEIGHT", DSK_CMDLINE_OPTIONAL, handle_make_maze, NULL); dsk_cmdline_add_shortcut ('p', "port"); dsk_cmdline_process_args (&argc, &argv); server = dsk_http_server_new (); for (i = 0; i < DSK_N_ELEMENTS (handlers); i++) { dsk_http_server_match_save (server); dsk_http_server_add_match (server, DSK_HTTP_SERVER_MATCH_PATH, handlers[i].pattern); dsk_http_server_register_cgi_handler (server, (DskHttpServerCgiFunc) handlers[i].handler, NULL, NULL); dsk_http_server_match_restore (server); } if (!dsk_http_server_bind_tcp (server, NULL, port, &error)) dsk_die ("error binding to port %u: %s", port, error->message); return dsk_main_run (); }
static void char_class_union_inplace (struct CharClass *inout, const struct CharClass *addend) { unsigned i; if (IS_SINGLE_CHAR_CLASS (addend)) CHAR_CLASS_BITVEC_SET (inout, SINGLE_CHAR_CLASS_GET_CHAR (addend)); else for (i = 0; i < DSK_N_ELEMENTS (inout->set); i++) inout->set[i] |= addend->set[i]; }
static void test_simple_write_seek (void) { unsigned i; for (i = 0; i < DSK_N_ELEMENTS (test_seek_datasets); i++) test_various_write_seek_1 (test_seek_datasets[i].name, test_seek_datasets[i].n_entries, test_seek_datasets[i].entries, test_seek_datasets[i].n_neg_entries, test_seek_datasets[i].neg_entries); }
/* Parse a backslash-escape special character class, include a lot of punctuation. The backslash should already have been skipped. */ static dsk_boolean get_backslash_char_class (const char **p_at, struct CharClass **out) { const char *start = *p_at; char bs = *start; if (dsk_ascii_ispunct (bs)) *out = MK_LITERAL_CHAR_CLASS (bs); else if (bs == 'n') *out = MK_LITERAL_CHAR_CLASS ('\n'); else if (bs == 'r') *out = MK_LITERAL_CHAR_CLASS ('\r'); else if (bs == 't') *out = MK_LITERAL_CHAR_CLASS ('\t'); else if (bs == 'v') *out = MK_LITERAL_CHAR_CLASS ('\v'); else if (dsk_ascii_isdigit (bs)) { uint8_t value; if (!dsk_ascii_isdigit (start[1])) { value = start[0] - '0'; *p_at = start + 1; } else if (!dsk_ascii_isdigit (start[2])) { value = (start[0] - '0') * 8 + (start[1] - '0'); *p_at = start + 2; } else { value = (start[0] - '0') * 64 + (start[1] - '0') * 8 + (start[2] - '0'); *p_at = start + 3; } *out = MK_LITERAL_CHAR_CLASS (value); return DSK_TRUE; } else { unsigned i; for (i = 0; i < DSK_N_ELEMENTS (special_char_classes); i++) if (special_char_classes[i].c == bs) { *out = &special_char_classes[i].cclass; *p_at = start + 1; return DSK_TRUE; } return DSK_FALSE; } *p_at = start + 1; return DSK_TRUE; }
int main(int argc, char **argv) { unsigned i; char test_dir_buf[256]; DskError *error = NULL; dsk_cmdline_init ("test table internals (the 'file' abstraction)", "Test Table Internals", NULL, 0); dsk_cmdline_add_boolean ("verbose", "extra logging", NULL, 0, &cmdline_verbose); dsk_cmdline_add_boolean ("slow", "run tests that are fairly slow", NULL, 0, &cmdline_slow); dsk_cmdline_add_boolean ("keep-testdir", "do not delete working directory", NULL, 0, &cmdline_keep_testdir); dsk_cmdline_process_args (&argc, &argv); snprintf (test_dir_buf, sizeof (test_dir_buf), "test-table-file-%u-%u", (unsigned)time(NULL), (unsigned)getpid()); location = dsk_dir_new (NULL, test_dir_buf, DSK_DIR_NEW_MAYBE_CREATE, &error); if (location == NULL) dsk_die ("error making directory: %s", error->message); for (i = 0; i < DSK_N_ELEMENTS (tests); i++) { fprintf (stderr, "Test: %s... ", tests[i].name); tests[i].test (); fprintf (stderr, " done.\n"); } if (cmdline_keep_testdir) { fprintf (stderr, "test-table-file: keep-testdir: preserving test directory %s\n", dsk_dir_get_str (location)); } else { DskError *error = NULL; if (!dsk_remove_dir_recursive (dsk_dir_get_str (location), &error)) dsk_die ("error removing directory %s: %s", dsk_dir_get_str (location), error->message); } dsk_cleanup (); return 0; }
int main(int argc, char **argv) { unsigned i; dsk_cmdline_init ("test HTTP-Header parsing", "Test the HTTP-Header Parsing Code", NULL, 0); dsk_cmdline_add_boolean ("verbose", "extra logging", NULL, 0, &cmdline_verbose); dsk_cmdline_process_args (&argc, &argv); for (i = 0; i < DSK_N_ELEMENTS (tests); i++) { fprintf (stderr, "Test: %s... ", tests[i].name); tests[i].test (); fprintf (stderr, " done.\n"); } dsk_cleanup (); return 0; }
static void add_bullet (unsigned *n_inout, DskJsonValue ***arr_inout, unsigned *alloced_inout, int px, int py) { DskJsonMember members[5]; members[0].name = "x"; members[0].value = dsk_json_value_new_number (px); members[1].name = "y"; members[1].value = dsk_json_value_new_number (py); members[2].name = "radius"; members[2].value = dsk_json_value_new_number (TILE_SIZE * 3 / 8); members[3].name = "color"; members[3].value = dsk_json_value_new_string (7, "#ffffff"); members[4].name = "type"; members[4].value = dsk_json_value_new_string (6, "circle"); append_element_json (n_inout, arr_inout, alloced_inout, dsk_json_value_new_object (DSK_N_ELEMENTS (members), members)); }
static void handle_get_games_list (DskHttpServerRequest *request) { unsigned n_games = 0; Game *game; DskJsonValue **game_info, **at; for (game = all_games; game; game = game->next_game) n_games++; game_info = dsk_malloc (sizeof (DskJsonValue *) * n_games); at = game_info; for (game = all_games; game; game = game->next_game, at++) { Object *object; unsigned n_players = 0; DskJsonValue **pat; DskJsonMember members[2] = { { "name", dsk_json_value_new_string (strlen (game->name), game->name) }, { "players", NULL }, }; DskJsonValue **players; for (object = game->objects[OBJECT_TYPE_USER]; object != NULL; object = object->next_in_game) n_players++; players = dsk_malloc (sizeof (DskJsonValue *) * n_players); pat = players; for (object = game->objects[OBJECT_TYPE_USER]; object != NULL; object = object->next_in_game) { User *player = (User *)object; *pat++ = dsk_json_value_new_string (strlen (player->name), player->name); } members[1].value = dsk_json_value_new_array (n_players, players); dsk_free (players); *at++ = dsk_json_value_new_object (DSK_N_ELEMENTS (members), members); } respond_take_json (request, dsk_json_value_new_array (n_games, game_info)); dsk_free (game_info); }
static void add_wall (unsigned *n_inout, DskJsonValue ***arr_inout, unsigned *alloced_inout, int x, int y, unsigned width, unsigned height) { DskJsonMember members[6]; members[0].name = "x"; members[0].value = dsk_json_value_new_number (x); members[1].name = "y"; members[1].value = dsk_json_value_new_number (y); members[2].name = "width"; members[2].value = dsk_json_value_new_number (width); members[3].name = "height"; members[3].value = dsk_json_value_new_number (height); members[4].name = "color"; members[4].value = dsk_json_value_new_string (7, "#ffffff"); members[5].name = "type"; members[5].value = dsk_json_value_new_string (9, "rectangle"); append_element_json (n_inout, arr_inout, alloced_inout, dsk_json_value_new_object (DSK_N_ELEMENTS (members), members)); }
static void test_various_write_seek_1 (const char *name, unsigned n_entries, TestEntry *entries, unsigned n_negative, TestEntry *neg_entries) { DskError *error = NULL; unsigned i; DskTableFileInterface *iface = &dsk_table_file_interface_trivial; DskTableFileWriter *writer; DskTableFileSeeker *seeker; if (cmdline_verbose) fprintf (stderr, "running dataset %s [%u]\n", name, n_entries); else fprintf (stderr, "."); writer = iface->new_writer (iface, location, "base", &error); if (writer == NULL) dsk_die ("%s", error->message); for (i = 0; i < n_entries; i++) { TestEntry *e = entries + i; if (!writer->write (writer, strlen (e->key), (uint8_t*) e->key, strlen (e->value), (uint8_t*) e->value, &error)) dsk_die ("error writing: %s", error->message); } if (!writer->close (writer, &error)) dsk_die ("error closing writer: %s", error->message); writer->destroy (writer); /* --- now test seeker --- */ /* pick the step size. */ { static unsigned prime_table[] = { 29, 31, 37, 41, 43, 47, 53, 59, 61, 67 }; unsigned *p_ptr = prime_table + DSK_N_ELEMENTS (prime_table) - 1; unsigned max_test, n_test, test_i, step; while (n_entries % *p_ptr == 0) p_ptr--; step = *p_ptr % n_entries; /* create seeker */ seeker = iface->new_seeker (iface, location, "base", &error); if (seeker == NULL) dsk_die ("error creating seeker from newly finished writer: %s", error->message); max_test = cmdline_slow ? 100000 : 1000; n_test = DSK_MIN (n_entries, max_test); test_i = step; for (i = 0; i < n_test; i++) { unsigned key_len, value_len; const uint8_t *key_data, *value_data; /* do the seek */ if (!seeker->find (seeker, str_test_func, (void*) entries[test_i].key, DSK_TABLE_FILE_FIND_ANY, &key_len, &key_data, &value_len, &value_data, &error)) { if (error) dsk_die ("error doing find that should have succeeded: %s", error->message); else dsk_die ("not found doing find that should have succeeded"); } #if 0 dsk_warning ("test=%s, got result %.*s", entries[test_i].key, (int) key_len, key_data); #endif dsk_assert (key_len == strlen (entries[test_i].key)); dsk_assert (value_len == strlen (entries[test_i].value)); dsk_assert (memcmp (key_data, entries[test_i].key, key_len) == 0); dsk_assert (memcmp (value_data, entries[test_i].value, value_len) == 0); /* advance test_i */ test_i += step; if (test_i >= n_entries) test_i -= n_entries; } /* do negative tests */ for (i = 0; i < n_negative; i++) { unsigned key_len, value_len; const uint8_t *key_data, *value_data; /* do the seek */ if (!seeker->find (seeker, str_test_func, (void*) neg_entries[i].key, DSK_TABLE_FILE_FIND_ANY, &key_len, &key_data, &value_len, &value_data, &error)) { if (error) dsk_die ("error doing find that should have returned nothing: %s", error->message); } else if (key_len == strlen (neg_entries[i].key) && memcmp (key_data, neg_entries[i].key, key_len) == 0) { dsk_die ("found result when none expected"); } } seeker->destroy (seeker); } }
int main(int argc, char** argv) { DskBuffer gskbuffer; char buf[1024]; char *str; dsk_cmdline_init ("test dsk-buffer code", "test DskBuffer", NULL, 0); dsk_cmdline_process_args (&argc, &argv); dsk_buffer_init (&gskbuffer); dsk_assert (gskbuffer.size == 0); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_assert (gskbuffer.size == 5); dsk_assert (dsk_buffer_read (&gskbuffer, sizeof (buf), buf) == 5); dsk_assert (memcmp (buf, "hello", 5) == 0); dsk_assert (gskbuffer.size == 0); dsk_buffer_clear (&gskbuffer); dsk_buffer_init (&gskbuffer); count (&gskbuffer, 1, 100000); decount (&gskbuffer, 1, 100000); dsk_assert (gskbuffer.size == 0); dsk_buffer_clear (&gskbuffer); dsk_buffer_init (&gskbuffer); dsk_buffer_append_string (&gskbuffer, "hello\na\nb"); str = dsk_buffer_read_line (&gskbuffer); dsk_assert (str); dsk_assert (strcmp (str, "hello") == 0); dsk_free (str); str = dsk_buffer_read_line (&gskbuffer); dsk_assert (str); dsk_assert (strcmp (str, "a") == 0); dsk_free (str); dsk_assert (gskbuffer.size == 1); dsk_assert (dsk_buffer_read_line (&gskbuffer) == NULL); dsk_buffer_append_byte (&gskbuffer, '\n'); str = dsk_buffer_read_line (&gskbuffer); dsk_assert (str); dsk_assert (strcmp (str, "b") == 0); dsk_free (str); dsk_assert (gskbuffer.size == 0); dsk_buffer_clear (&gskbuffer); dsk_buffer_init (&gskbuffer); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_buffer_append_foreign (&gskbuffer, 4, "test", NULL, NULL); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_assert (gskbuffer.size == 14); dsk_assert (dsk_buffer_read (&gskbuffer, sizeof (buf), buf) == 14); dsk_assert (memcmp (buf, "hellotesthello", 14) == 0); dsk_assert (gskbuffer.size == 0); /* Test that the foreign data really is not being stored in the DskBuffer */ { char test_str[5]; strcpy (test_str, "test"); dsk_buffer_init (&gskbuffer); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_buffer_append_foreign (&gskbuffer, 4, test_str, NULL, NULL); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_assert (gskbuffer.size == 14); dsk_assert (dsk_buffer_peek (&gskbuffer, sizeof (buf), buf) == 14); dsk_assert (memcmp (buf, "hellotesthello", 14) == 0); test_str[1] = '3'; dsk_assert (gskbuffer.size == 14); dsk_assert (dsk_buffer_read (&gskbuffer, sizeof (buf), buf) == 14); dsk_assert (memcmp (buf, "hellot3sthello", 14) == 0); dsk_buffer_clear (&gskbuffer); } /* Test str_index_of */ { DskBuffer buffer = DSK_BUFFER_INIT; dsk_buffer_append_foreign (&buffer, 3, "abc", NULL, NULL); dsk_buffer_append_foreign (&buffer, 3, "def", NULL, NULL); dsk_buffer_append_foreign (&buffer, 3, "gad", NULL, NULL); #if 0 dsk_assert (dsk_buffer_str_index_of (&buffer, "cdefg") == 2); dsk_assert (dsk_buffer_str_index_of (&buffer, "ad") == 7); dsk_assert (dsk_buffer_str_index_of (&buffer, "ab") == 0); dsk_assert (dsk_buffer_str_index_of (&buffer, "a") == 0); dsk_assert (dsk_buffer_str_index_of (&buffer, "g") == 6); #endif dsk_buffer_clear (&buffer); } static const char *before_strs[] = { "", "foo", NULL, NULL }; before_strs[2] = generate_str (100, 1000); before_strs[3] = generate_str (10000, 100000); static const char *placeholder_strs[] = { "", "bar", NULL, NULL, NULL }; placeholder_strs[2] = generate_str (100, 1000); placeholder_strs[3] = generate_str (10000, 100000); placeholder_strs[4] = generate_str (100000, 1000000); static const char *after_strs[] = { "", "foo", NULL, NULL }; after_strs[2] = generate_str (100, 1000); after_strs[3] = generate_str (10000, 100000); unsigned bi, pi, ai; for (bi = 0; bi < DSK_N_ELEMENTS (before_strs); bi++) for (pi = 0; pi < DSK_N_ELEMENTS (placeholder_strs); pi++) for (ai = 0; ai < DSK_N_ELEMENTS (after_strs); ai++) { DskBuffer buffer = DSK_BUFFER_INIT; const char *pi_str = placeholder_strs[pi]; DskBufferPlaceholder placeholder; dsk_buffer_append_string (&buffer, before_strs[bi]); dsk_buffer_append_placeholder (&buffer, strlen (pi_str), &placeholder); dsk_buffer_append_string (&buffer, after_strs[ai]); dsk_buffer_placeholder_set (&placeholder, pi_str); dsk_assert (try_initial_remove (&buffer, before_strs[bi])); dsk_assert (try_initial_remove (&buffer, pi_str)); dsk_assert (try_initial_remove (&buffer, after_strs[ai])); dsk_assert (buffer.size == 0); } return 0; }