Debian Codegraph @

tests/test_serialization.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * libdatrie - Double-Array Trie Library
4 * Copyright (C) 2013 Theppitak Karoonboonyanan <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * test_serialization.c - Test for datrie file and in-memory blob operations
23 * Created: 2019-11-11
24 * Author: Theppitak Karoonboonyanan <[email protected]> and KOLANICH <[email protected]>
25 */
26
27 #include <datrie/trie.h>
28 #include "utils.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <wchar.h>
33
34 #define TRIE_FILENAME "test_serialization.tri"
35
36 int
37 main (void)
38 {
39 Trie *test_trie;
40 DictRec *dict_p;
41
42 msg_step ("Preparing trie");
43 test_trie = en_trie_new ();
44 if (!test_trie) {
45 printf ("Failed to allocate test trie.\n");
46 goto err_trie_not_created;
47 }
48
49 /* add/remove some words */
50 for (dict_p = dict_src; dict_p->key; dict_p++) {
51 if (!trie_store (test_trie, dict_p->key, dict_p->data)) {
52 printf ("Failed to add key '%ls', data %d.\n",
53 (wchar_t *)dict_p->key, dict_p->data);
54 goto err_trie_created;
55 }
56 }
57
58 /* save & close */
59 msg_step ("Saving trie to file");
60 remove (TRIE_FILENAME); /* error ignored */
61 if (trie_save (test_trie, TRIE_FILENAME) != 0) {
62 printf ("Failed to save trie to file '%s'.\n", TRIE_FILENAME);
63 goto err_trie_created;
64 }
65
66 msg_step ("Getting serialized trie size");
67 size_t size = trie_get_serialized_size (test_trie);
68 printf ("serialized trie size %lu\n", size);
69 msg_step ("Allocating");
70 uint8 *trieSerializedData = malloc (size);
71 if (!trieSerializedData) {
72 printf ("Failed to allocate trieSerializedData.\n");
73 goto err_trie_saved;
74 }
75 printf ("allocated %p\n", trieSerializedData);
76 msg_step ("Serializing");
77 trie_serialize (test_trie, trieSerializedData);
78 msg_step ("Serialized");
79
80 FILE *f = fopen (TRIE_FILENAME, "rb");
81 if (!f) {
82 printf ("Failed to reopen trie file " TRIE_FILENAME ".\n");
83 goto err_serial_data_allocated;
84 }
85 fseek (f, 0, SEEK_END);
86 size_t file_size = ftell (f);
87 fseek (f, 0, SEEK_SET);
88
89 if (size != file_size) {
90 printf ("Trie serialized data doesn't match size of the file.\n");
91 goto err_file_reopened;
92 }
93
94 unsigned char *trieFileData = malloc (size);
95 if (!trieFileData) {
96 printf ("Failed to allocate trieFileData.\n");
97 goto err_file_reopened;
98 }
99 if (fread (trieFileData, 1, size, f) != size) {
100 printf ("Failed to read back the serialized trie file.\n");
101 goto err_file_data_allocated;
102 }
103 if (memcmp (trieSerializedData, trieFileData, size) != 0) {
104 printf ("Trie serialized data doesn't match contents of the file.\n");
105 goto err_file_data_allocated;
106 }
107 printf ("PASS!\n");
108
109 free (trieFileData);
110 fclose (f);
111 free (trieSerializedData);
112 remove (TRIE_FILENAME);
113 trie_free (test_trie);
114 return 0;
115
116 err_file_data_allocated:
117 free (trieFileData);
118 err_file_reopened:
119 fclose (f);
120 err_serial_data_allocated:
121 free (trieSerializedData);
122 err_trie_saved:
123 remove (TRIE_FILENAME);
124 err_trie_created:
125 trie_free (test_trie);
126 err_trie_not_created:
127 return 1;
128 }
129
130 /*
131 vi:ts=4:ai:expandtab
132 */