10 #include <CUnit/CUnit.h>
11 #include <string_operations.h>
13 #include "file_operations.h"
14 #include "string_operations.h"
16 #include "libfocunit.h"
18 void test_read_file_tokens() {
19 char* teststring =
"a\n^b\n c";
20 char* testfile =
"/tmp/monkftest";
22 FILE* file = fopen(testfile,
"w");
23 CU_ASSERT_PTR_NOT_NULL(file);
24 fprintf(file,
"%s", teststring);
28 CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens,
"\n\t\r^ "));
30 FO_ASSERT_EQUAL_FATAL(tokens->len, 3);
31 Token token0 = g_array_index(tokens,
Token, 0);
32 Token token1 = g_array_index(tokens,
Token, 1);
33 Token token2 = g_array_index(tokens,
Token, 2);
34 CU_ASSERT_EQUAL(token0.length, 1);
35 CU_ASSERT_EQUAL(token1.length, 1);
36 CU_ASSERT_EQUAL(token2.length, 1);
37 CU_ASSERT_EQUAL(token0.removedBefore, 0);
38 CU_ASSERT_EQUAL(token1.removedBefore, 2);
39 CU_ASSERT_EQUAL(token2.removedBefore, 2);
40 CU_ASSERT_EQUAL(token0.hashedContent, hash(
"a"));
41 CU_ASSERT_EQUAL(token1.hashedContent, hash(
"b"));
42 CU_ASSERT_EQUAL(token2.hashedContent, hash(
"c"));
44 g_array_free(tokens, TRUE);
47 void test_read_file_tokens2() {
48 char* teststring =
" * a\n *\n * b";
49 char* testfile =
"/tmp/monkftest";
51 FILE* file = fopen(testfile,
"w");
52 CU_ASSERT_PTR_NOT_NULL(file);
53 fprintf(file,
"%s", teststring);
57 FO_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens,
"\n\t\r^ "));
59 FO_ASSERT_EQUAL_FATAL(tokens->len, 2);
60 Token token0 = g_array_index(tokens,
Token, 0);
61 Token token1 = g_array_index(tokens,
Token, 1);
62 CU_ASSERT_EQUAL(token0.hashedContent, hash(
"a"));
63 CU_ASSERT_EQUAL(token1.hashedContent, hash(
"b"));
64 CU_ASSERT_EQUAL(token0.length, 1);
65 CU_ASSERT_EQUAL(token1.length, 1);
66 CU_ASSERT_EQUAL(token0.removedBefore, 3);
67 CU_ASSERT_EQUAL(token1.removedBefore, 7);
69 g_array_free(tokens, TRUE);
72 void test_read_file_tokens_error() {
73 GArray* tokens = (GArray*)0x17;
74 CU_ASSERT_FALSE(readTokensFromFile(
"not a file", &tokens,
"\n\t\r^ "));
75 CU_ASSERT_EQUAL((GArray*)0x17, tokens);
78 void test_read_file_tokens_binaries() {
79 char teststring[] =
"a\n^b\0 c";
80 char* testfile =
"/tmp/monkftest";
82 FILE* file = fopen(testfile,
"w");
83 CU_ASSERT_PTR_NOT_NULL(file);
84 fwrite(teststring, 1,
sizeof (teststring), file);
88 CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens,
"\n\t\r^ "));
90 FO_ASSERT_EQUAL_FATAL(tokens->len, 3);
91 Token token0 = g_array_index(tokens,
Token, 0);
92 Token token1 = g_array_index(tokens,
Token, 1);
93 Token token2 = g_array_index(tokens,
Token, 2);
94 CU_ASSERT_EQUAL(token0.length, 1);
95 CU_ASSERT_EQUAL(token1.length, 1);
96 CU_ASSERT_EQUAL(token2.length, 1);
97 CU_ASSERT_EQUAL(token0.removedBefore, 0);
98 CU_ASSERT_EQUAL(token1.removedBefore, 2);
99 CU_ASSERT_EQUAL(token2.removedBefore, 2);
100 CU_ASSERT_EQUAL(token0.hashedContent, hash(
"a"));
101 CU_ASSERT_EQUAL(token1.hashedContent, hash(
"b"));
102 CU_ASSERT_EQUAL(token2.hashedContent, hash(
"c"));
104 g_array_free(tokens, TRUE);
107 void binaryWrite(
const char* testfile,
const char* teststring)
109 FILE* file = fopen(testfile,
"w");
110 CU_ASSERT_PTR_NOT_NULL(file);
111 fwrite(teststring, 1, strlen(teststring), file);
115 void test_read_file_tokens_encodingConversion() {
116 char* testfile =
"/tmp/monkftest";
119 binaryWrite(testfile,
"a\n ß é c");
120 CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens,
"\n\t\r^ "));
123 binaryWrite(testfile,
"a\n \xdf\x0a \xe9\x0a c");
124 CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens1,
"\n\t\r^ "));
126 FO_ASSERT_FATAL(tokens->len > 0);
128 FO_ASSERT_EQUAL_FATAL(tokens->len, tokens1->len);
131 g_array_index(tokens,
Token, 0).hashedContent,
132 g_array_index(tokens1,
Token, 0).hashedContent
135 g_array_index(tokens,
Token, 1).hashedContent,
136 g_array_index(tokens1,
Token, 1).hashedContent
139 g_array_index(tokens,
Token, 2).hashedContent,
140 g_array_index(tokens1,
Token, 2).hashedContent
143 g_array_free(tokens, TRUE);
144 g_array_free(tokens1, TRUE);
148 CU_TestInfo file_operations_testcases[] = {
149 {
"Testing reading file tokens:", test_read_file_tokens},
150 {
"Testing reading file tokens2:", test_read_file_tokens2},
151 {
"Testing reading file tokens with a binary file:", test_read_file_tokens_binaries},
152 {
"Testing reading file tokens with two different encodings return same token contents:", test_read_file_tokens_encodingConversion},
153 {
"Testing reading file tokens from wrong file:", test_read_file_tokens_error},