66#include <token/lib.h>
77
88
9- int are_tok_equal (struct Token * t1 , struct Token * t2 ) {
10- if (strcmp (t1 -> literal , t2 -> literal ) != 0 ) {
9+ int tokencmp (struct Token * f_token , struct Token * s_token ) {
10+ if (strcmp (f_token -> literal , s_token -> literal ) != 0 ) {
1111 return 0 ;
1212 }
13- if (t1 -> code != t2 -> code ) {
13+ if (f_token -> code != s_token -> code ) {
1414 return 0 ;
1515 }
16- if (t1 -> line != t2 -> line ) {
16+ if (f_token -> line != s_token -> line ) {
1717 return 0 ;
1818 }
19- if (t1 -> column != t2 -> column ) {
19+ if (f_token -> column != s_token -> column ) {
2020 return 0 ;
2121 }
2222
2323 return 1 ;
2424}
2525
26- void comp_tok (char * input , struct Token expected_tok [], int num_expected_tok , char * test_name ) {
27- struct Lexer * lex = new_lexer (input );
28- struct Tokens * toks = init_token_arr ();
26+ void are_tokens_equal (char * input , struct Token expected_tokens [], int number_of_expected_tokens , char * test_name ) {
27+ struct Lexer * lexer = new_lexer (input );
28+ struct Tokens * tokens = new_tokens_array ();
2929
30- int num_tok = 0 ;
30+ int number_of_tokens = 0 ;
3131
3232 struct Token * tok ;
3333 for (int i = 0 ; tok -> code != END_OF_FILE ; i ++ ) {
34- tok = lex -> next_token (lex );
35- push_token (toks , tok );
36- ++ num_tok ;
34+ tok = lexer -> next_token (lexer );
35+ push_token (tokens , tok );
36+ ++ number_of_tokens ;
3737 }
3838
3939 // printf("\n");
40- // for (int i = 0; i < num_tok ; i++) {
40+ // for (int i = 0; i < number_of_tokens ; i++) {
4141 // printf("i[%d] ", i);
42- // print_token(toks ->tokens[i]);
42+ // print_token(tokens ->tokens[i]);
4343 // }
4444
45- free (lex );
45+ free (lexer );
4646
47- if (num_tok != num_expected_tok ) {
48- printf ("\x1b[35m%s:\x1b[0m \x1b[31mNumber of tokens mismatch want=%d got=%d.\x1b[0m\n" , test_name , num_expected_tok , num_tok );
49- pop_tokens (toks );
47+ if (number_of_tokens != number_of_expected_tokens ) {
48+ printf ("\x1b[35m%s:\x1b[0m \x1b[31mNumber of tokens mismatch want=%d got=%d.\x1b[0m\n" , test_name , number_of_expected_tokens , number_of_tokens );
49+ pop_tokens (tokens );
5050 exit (1 );
5151 }
5252
53- for (int i = 0 ; i < num_tok ; i ++ ) {
54- if (!are_tok_equal ( toks -> tokens [i ], & expected_tok [i ])) {
55- printf ("\x1b[35m%s:\x1b[0m \x1b[31mInvalid token[%d] want=%d got=%d.\x1b[0m\n" , test_name , i , expected_tok [i ].code , toks -> tokens [i ]-> code );
56- pop_tokens (toks );
53+ for (int i = 0 ; i < number_of_tokens ; i ++ ) {
54+ if (!tokencmp ( tokens -> tokens [i ], & expected_tokens [i ])) {
55+ printf ("\x1b[35m%s:\x1b[0m \x1b[31mInvalid token[%d] want=%d got=%d.\x1b[0m\n" , test_name , i , expected_tokens [i ].code , tokens -> tokens [i ]-> code );
56+ pop_tokens (tokens );
5757 exit (1 );
5858 }
5959 }
6060
6161 printf ("\x1b[36m%s:\x1b[0m \x1b[32mPass.\x1b[0m\n" , test_name );
6262
63- pop_tokens (toks );
63+ pop_tokens (tokens );
6464}
6565
6666
67- void test_number_assignment () {
68- char * input = "def my_int = 3000;\n" // int
69- "def my_hex = 0xFF;\n" // hex
70- "def my_bin = 0b00;\n" // binary
71- "def my_oct = 0o01;\n" // octal
72- "def my_flt = 3.14;\n" // float
73- "def my_int_with_ = 3_00;\n" // int with _
74- "def my_neg_int = -1321;" ;
67+ void test_should_pass_if_number_assignment_is_equal_to_expected () {
68+ char * input = "var my_int = 3000;\n" // int
69+ "var my_hex = 0xFF;\n" // hex
70+ "var my_bin = 0b00;\n" // binary
71+ "var my_oct = 0o01;\n" // octal
72+ "var my_flt = 3.14;\n" // float
73+ "var my_int_with_ = 3_00;\n" // int with _
74+ "var my_neg_int = -1321;" ;
7575
76- struct Lexer * lex = new_lexer (input );
77- struct Tokens * toks = init_token_arr ();
76+ struct Lexer * lexer = new_lexer (input );
77+ struct Tokens * tokens = new_tokens_array ();
7878
79- struct Token expected_tok [] = {
79+ struct Token expected_tokens [] = {
8080 // literal | code | line | column
81- { "def" , 36 , 1 , 1 },
82- { "my_int" , 3 , 1 , 5 },
83- { "=" , 5 , 1 , 12 },
84- { "3000" , 3 , 1 , 14 },
85- { ";" , 30 , 1 , 18 },
86- { "def" , 36 , 2 , 2 },
87- { "my_hex" , 3 , 2 , 6 },
88- { "=" , 5 , 2 , 13 },
89- { "0xFF" , 3 , 2 , 15 },
90- { ";" , 30 , 2 , 19 },
91- { "def" , 36 , 3 , 2 },
92- { "my_bin" , 3 , 3 , 6 },
93- { "=" , 5 , 3 , 13 },
94- { "0b00" , 3 , 3 , 15 },
95- { ";" , 30 , 3 , 19 },
96- { "def" , 36 , 4 , 2 },
97- { "my_oct" , 3 , 4 , 6 },
98- { "=" , 5 , 4 , 13 },
99- { "0o01" , 3 , 4 , 15 },
100- { ";" , 30 , 4 , 19 },
101- { "def" , 36 , 5 , 2 },
102- { "my_flt" , 3 , 5 , 6 },
103- { "=" , 5 , 5 , 13 },
104- { "3" , 3 , 5 , 15 },
105- { "." , 15 , 5 , 16 },
106- { "14" , 3 , 5 , 17 },
107- { ";" , 30 , 5 , 19 },
108- { "def" , 36 , 6 , 2 },
109- { "my_int_with_" , 3 , 6 , 6 },
110- { "=" , 5 , 6 , 19 },
111- { "3_00" , 3 , 6 , 21 },
112- { ";" , 30 , 6 , 25 },
113- { "def" , 36 , 7 , 2 },
114- { "my_neg_int" , 3 , 7 , 6 },
115- { "=" , 5 , 7 , 17 },
116- { "-1321" , 3 , 7 , 19 },
117- { ";" , 30 , 7 , 24 },
118- { "" , 2 , 7 , 25 },
81+ { "var" , 36 , 1 , 1 },
82+ { "my_int" , 3 , 1 , 5 },
83+ { "=" , 5 , 1 , 12 },
84+ { "3000" , 4 , 1 , 14 },
85+ { ";" , 30 , 1 , 18 },
86+ { "var" , 36 , 2 , 2 },
87+ { "my_hex" , 3 , 2 , 6 },
88+ { "=" , 5 , 2 , 13 },
89+ { "0xFF" , 4 , 2 , 15 },
90+ { ";" , 30 , 2 , 19 },
91+ { "var" , 36 , 3 , 2 },
92+ { "my_bin" , 3 , 3 , 6 },
93+ { "=" , 5 , 3 , 13 },
94+ { "0b00" , 4 , 3 , 15 },
95+ { ";" , 30 , 3 , 19 },
96+ { "var" , 36 , 4 , 2 },
97+ { "my_oct" , 3 , 4 , 6 },
98+ { "=" , 5 , 4 , 13 },
99+ { "0o01" , 4 , 4 , 15 },
100+ { ";" , 30 , 4 , 19 },
101+ { "var" , 36 , 5 , 2 },
102+ { "my_flt" , 3 , 5 , 6 },
103+ { "=" , 5 , 5 , 13 },
104+ { "3.14" , 4 , 5 , 15 },
105+ { ";" , 30 , 5 , 19 },
106+ { "var" , 36 , 6 , 2 },
107+ { "my_int_with_" , 3 , 6 , 6 },
108+ { "=" , 5 , 6 , 19 },
109+ { "3_00" , 4 , 6 , 21 },
110+ { ";" , 30 , 6 , 25 },
111+ { "var" , 36 , 7 , 2 },
112+ { "my_neg_int" , 3 , 7 , 6 },
113+ { "=" , 5 , 7 , 17 },
114+ { "-1321" , 4 , 7 , 19 },
115+ { ";" , 30 , 7 , 24 },
116+ { "" , 2 , 7 , 25 },
119117 };
120118
121- comp_tok (input , expected_tok , sizeof (expected_tok )/sizeof (struct Token ), "test_number_assignment " );
119+ are_tokens_equal (input , expected_tokens , sizeof (expected_tokens )/sizeof (struct Token ), "test_should_pass_if_number_assignment_is_equal_to_expected " );
122120}
123121
124- void test_bool_assignment () {
125- char * input = "def my_true = true;\n"
126- "def my_false = false;" ;
122+ void test_should_pass_if_bool_assignments_are_equal_to_expected () {
123+ char * input = "var my_true = true;\n"
124+ "var my_false = false;" ;
127125
128- struct Lexer * lex = new_lexer (input );
129- struct Tokens * toks = init_token_arr ();
126+ struct Lexer * lexer = new_lexer (input );
127+ struct Tokens * tokens = new_tokens_array ();
130128
131- struct Token expected_tok [] = {
129+ struct Token expected_tokens [] = {
132130 // literal | code | line | column
133- { "def " , 36 , 1 , 1 },
134- { "my_true" , 3 , 1 , 5 },
135- { "=" , 5 , 1 , 13 },
136- { "true" , 41 , 1 , 15 },
137- { ";" , 30 , 1 , 19 },
138- { "def " , 36 , 2 , 2 },
139- { "my_false" , 3 , 2 , 6 },
140- { "=" , 5 , 2 , 15 },
141- { "false" , 42 , 2 , 17 },
142- { ";" , 30 , 2 , 22 },
143- { "" , 2 , 2 , 23 },
131+ { "var " , 36 , 1 , 1 },
132+ { "my_true" , 3 , 1 , 5 },
133+ { "=" , 5 , 1 , 13 },
134+ { "true" , 41 , 1 , 15 },
135+ { ";" , 30 , 1 , 19 },
136+ { "var " , 36 , 2 , 2 },
137+ { "my_false" , 3 , 2 , 6 },
138+ { "=" , 5 , 2 , 15 },
139+ { "false" , 42 , 2 , 17 },
140+ { ";" , 30 , 2 , 22 },
141+ { "" , 2 , 2 , 23 },
144142 };
145143
146- comp_tok (input , expected_tok , sizeof (expected_tok )/sizeof (struct Token ), "test_bool_assignment " );
144+ are_tokens_equal (input , expected_tokens , sizeof (expected_tokens )/sizeof (struct Token ), "test_should_pass_if_bool_assignments_are_equal_to_expected " );
147145}
148146
149147// Tests all delimiters at once
150- void test_delimiters () {
148+ void test_should_pass_if_delimiters_are_equal_to_expected () {
151149 char * input = "() {};" ;
152150
153- struct Lexer * lex = new_lexer (input );
154- struct Tokens * toks = init_token_arr ();
151+ struct Lexer * lexer = new_lexer (input );
152+ struct Tokens * tokens = new_tokens_array ();
155153
156- struct Token expected_tok [] = {
154+ struct Token expected_tokens [] = {
157155 // literal | code | line | column
158156 { "(" , 31 , 1 , 1 },
159157 { ")" , 32 , 1 , 2 },
@@ -163,17 +161,17 @@ void test_delimiters() {
163161 { "" , 2 , 1 , 7 },
164162 };
165163
166- comp_tok (input , expected_tok , sizeof (expected_tok )/sizeof (struct Token ), "test_delimiters " );
164+ are_tokens_equal (input , expected_tokens , sizeof (expected_tokens )/sizeof (struct Token ), "test_should_pass_if_delimiters_are_equal_to_expected " );
167165}
168166
169167// Tests all single symbols at once
170- void test_single_symbol () {
168+ void test_should_pass_if_single_symbols_are_equal_to_expected () {
171169 char * input = "+ - * ! = < > / ? : , .;" ;
172170
173- struct Lexer * lex = new_lexer (input );
174- struct Tokens * toks = init_token_arr ();
171+ struct Lexer * lexer = new_lexer (input );
172+ struct Tokens * tokens = new_tokens_array ();
175173
176- struct Token expected_tok [] = {
174+ struct Token expected_tokens [] = {
177175 { "+" , 6 , 1 , 1 },
178176 { "-" , 7 , 1 , 3 },
179177 { "*" , 9 , 1 , 5 },
@@ -190,18 +188,18 @@ void test_single_symbol() {
190188 { "" , 2 , 1 , 25 },
191189 };
192190
193- comp_tok (input , expected_tok , sizeof (expected_tok )/sizeof (struct Token ), "test_single_symbol " );
191+ are_tokens_equal (input , expected_tokens , sizeof (expected_tokens )/sizeof (struct Token ), "test_should_pass_if_single_symbols_are_equal_to_expected " );
194192}
195193
196194// Tests all compound symbols at once
197- void test_compound_symbol () {
195+ void test_should_pass_if_compound_symbols_are_equal_to_expected () {
198196 char * input = "++ -- ** !! ==;\n"
199197 "+= -= *= /= <= >= !=;" ;
200198
201- struct Lexer * lex = new_lexer (input );
202- struct Tokens * toks = init_token_arr ();
199+ struct Lexer * lexer = new_lexer (input );
200+ struct Tokens * tokens = new_tokens_array ();
203201
204- struct Token expected_tok [] = {
202+ struct Token expected_tokens [] = {
205203 { "++" , 16 , 1 , 1 },
206204 { "--" , 17 , 1 , 4 },
207205 { "**" , 22 , 1 , 7 },
@@ -222,18 +220,18 @@ void test_compound_symbol() {
222220
223221 int num_tok = 0 ;
224222
225- comp_tok (input , expected_tok , sizeof (expected_tok )/sizeof (struct Token ), "test_compound_symbol " );
223+ are_tokens_equal (input , expected_tokens , sizeof (expected_tokens )/sizeof (struct Token ), "test_should_pass_if_compound_symbols_are_equal_to_expected " );
226224}
227225
228226// Should pass if ";" don't concatenate with other characters
229- void test_semicolon_symbol_independence () {
227+ void test_should_pass_if_semicolon_assingment_are_equal_to_expected () {
230228 char * input = ";* ;+ ;- ;= ;> ;<\n"
231229 "*; +; -; =; <; >;" ;
232230
233- struct Lexer * lex = new_lexer (input );
234- struct Tokens * toks = init_token_arr ();
231+ struct Lexer * lexer = new_lexer (input );
232+ struct Tokens * tokens = new_tokens_array ();
235233
236- struct Token expected_tok [] = {
234+ struct Token expected_tokens [] = {
237235 { ";" , 30 , 1 , 1 },
238236 { "*" , 9 , 1 , 2 },
239237 { ";" , 30 , 1 , 4 },
@@ -261,6 +259,6 @@ void test_semicolon_symbol_independence() {
261259 { "" , 2 , 2 , 19 },
262260 };
263261
264- comp_tok (input , expected_tok , sizeof (expected_tok )/sizeof (struct Token ), "test_semicolon_symbol_independence " );
262+ are_tokens_equal (input , expected_tokens , sizeof (expected_tokens )/sizeof (struct Token ), "test_should_pass_if_semicolon_assingment_are_equal_to_expected " );
265263}
266264
0 commit comments