21 #include <CUnit/CUnit.h> 22 #include <string_operations.h> 24 #include "file_operations.h" 25 #include "string_operations.h" 27 #include "libfocunit.h" 29 void test_read_file_tokens() {
30 char* teststring =
"a\n^b\n c";
31 char* testfile =
"/tmp/monkftest";
33 FILE* file = fopen(testfile,
"w");
34 CU_ASSERT_PTR_NOT_NULL(file);
35 fprintf(file,
"%s", teststring);
39 CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens,
"\n\t\r^ "));
41 FO_ASSERT_EQUAL_FATAL(tokens->len, 3);
42 Token token0 = g_array_index(tokens,
Token, 0);
43 Token token1 = g_array_index(tokens,
Token, 1);
44 Token token2 = g_array_index(tokens,
Token, 2);
45 CU_ASSERT_EQUAL(token0.length, 1);
46 CU_ASSERT_EQUAL(token1.length, 1);
47 CU_ASSERT_EQUAL(token2.length, 1);
48 CU_ASSERT_EQUAL(token0.removedBefore, 0);
49 CU_ASSERT_EQUAL(token1.removedBefore, 2);
50 CU_ASSERT_EQUAL(token2.removedBefore, 2);
51 CU_ASSERT_EQUAL(token0.hashedContent, hash(
"a"));
52 CU_ASSERT_EQUAL(token1.hashedContent, hash(
"b"));
53 CU_ASSERT_EQUAL(token2.hashedContent, hash(
"c"));
55 g_array_free(tokens, TRUE);
58 void test_read_file_tokens2() {
59 char* teststring =
" * a\n *\n * b";
60 char* testfile =
"/tmp/monkftest";
62 FILE* file = fopen(testfile,
"w");
63 CU_ASSERT_PTR_NOT_NULL(file);
64 fprintf(file,
"%s", teststring);
68 FO_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens,
"\n\t\r^ "));
70 FO_ASSERT_EQUAL_FATAL(tokens->len, 2);
71 Token token0 = g_array_index(tokens,
Token, 0);
72 Token token1 = g_array_index(tokens,
Token, 1);
73 CU_ASSERT_EQUAL(token0.hashedContent, hash(
"a"));
74 CU_ASSERT_EQUAL(token1.hashedContent, hash(
"b"));
75 CU_ASSERT_EQUAL(token0.length, 1);
76 CU_ASSERT_EQUAL(token1.length, 1);
77 CU_ASSERT_EQUAL(token0.removedBefore, 3);
78 CU_ASSERT_EQUAL(token1.removedBefore, 7);
80 g_array_free(tokens, TRUE);
83 void test_read_file_tokens_error() {
84 GArray* tokens = (GArray*)0x17;
85 CU_ASSERT_FALSE(readTokensFromFile(
"not a file", &tokens,
"\n\t\r^ "));
86 CU_ASSERT_EQUAL((GArray*)0x17, tokens);
89 void test_read_file_tokens_binaries() {
90 char teststring[] =
"a\n^b\0 c";
91 char* testfile =
"/tmp/monkftest";
93 FILE* file = fopen(testfile,
"w");
94 CU_ASSERT_PTR_NOT_NULL(file);
95 fwrite(teststring, 1,
sizeof (teststring), file);
99 CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens,
"\n\t\r^ "));
101 FO_ASSERT_EQUAL_FATAL(tokens->len, 3);
102 Token token0 = g_array_index(tokens,
Token, 0);
103 Token token1 = g_array_index(tokens,
Token, 1);
104 Token token2 = g_array_index(tokens,
Token, 2);
105 CU_ASSERT_EQUAL(token0.length, 1);
106 CU_ASSERT_EQUAL(token1.length, 1);
107 CU_ASSERT_EQUAL(token2.length, 1);
108 CU_ASSERT_EQUAL(token0.removedBefore, 0);
109 CU_ASSERT_EQUAL(token1.removedBefore, 2);
110 CU_ASSERT_EQUAL(token2.removedBefore, 2);
111 CU_ASSERT_EQUAL(token0.hashedContent, hash(
"a"));
112 CU_ASSERT_EQUAL(token1.hashedContent, hash(
"b"));
113 CU_ASSERT_EQUAL(token2.hashedContent, hash(
"c"));
115 g_array_free(tokens, TRUE);
118 void binaryWrite(
const char* testfile,
const char* teststring)
120 FILE* file = fopen(testfile,
"w");
121 CU_ASSERT_PTR_NOT_NULL(file);
122 fwrite(teststring, 1, strlen(teststring), file);
126 void test_read_file_tokens_encodingConversion() {
127 char* testfile =
"/tmp/monkftest";
130 binaryWrite(testfile,
"a\n ß é c");
131 CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens,
"\n\t\r^ "));
134 binaryWrite(testfile,
"a\n \xdf\x0a \xe9\x0a c");
135 CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens1,
"\n\t\r^ "));
137 FO_ASSERT_FATAL(tokens->len > 0);
139 FO_ASSERT_EQUAL_FATAL(tokens->len, tokens1->len);
142 g_array_index(tokens,
Token, 0).hashedContent,
143 g_array_index(tokens1,
Token, 0).hashedContent
146 g_array_index(tokens,
Token, 1).hashedContent,
147 g_array_index(tokens1,
Token, 1).hashedContent
150 g_array_index(tokens,
Token, 2).hashedContent,
151 g_array_index(tokens1,
Token, 2).hashedContent
154 g_array_free(tokens, TRUE);
155 g_array_free(tokens1, TRUE);
159 CU_TestInfo file_operations_testcases[] = {
160 {
"Testing reading file tokens:", test_read_file_tokens},
161 {
"Testing reading file tokens2:", test_read_file_tokens2},
162 {
"Testing reading file tokens with a binary file:", test_read_file_tokens_binaries},
163 {
"Testing reading file tokens with two different encodings return same token contents:", test_read_file_tokens_encodingConversion},
164 {
"Testing reading file tokens from wrong file:", test_read_file_tokens_error},