Skip to content

Commit 2ae646f

Browse files
committed
tests rewrited for gtest framework & added trick for exporting static functions for testing (see nnc_static.h)
1 parent 7ae57b0 commit 2ae646f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1210
-1227
lines changed

.gitignore

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ Module.symvers
4242
Mkfile.old
4343
dkms.conf
4444
.vscode
45+
4546
tests/googletest
46-
tests2/googletest
47-
48-
src/CMakeCache.txt
49-
src/cmake_install.cmake
50-
src/Makefile
51-
src/CMakeFiles
52-
src/nnc
53-
src/tests
47+
tests/build
48+
src/build

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "src/googletest"]
2-
path = tests2/googletest
2+
path = tests/googletest
33
url = https://github.com/google/googletest

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
22
PROJECT(nnc)
33
ADD_COMPILE_OPTIONS(-Wall)
44

5+
SET(CMAKE_BUILD_TYPE Debug)
56
FILE(GLOB nnc_star_c ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
67

78
ADD_EXECUTABLE(${PROJECT_NAME} ${nnc_star_c})

src/nnc_alloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "nnc_typedefs.h"
88

9-
#define anew(type) (type*)nnc_alloc(sizeof(type))
9+
#define anew(type) (type*)nnc_alloc(sizeof(type))
1010
#define cnew(type, size) (type*)nnc_alloc(sizeof(type) * (size))
1111
#define rem(ptr) nnc_dispose(ptr)
1212

src/nnc_arena.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @param ptr Pointer to allocted block.
77
* @return Pointer to allocated & initialized arena entry.
88
*/
9-
static nnc_arena_entry* nnc_arena_entry_init(nnc_u64 size, nnc_heap_ptr ptr) {
9+
nnc_static nnc_arena_entry* nnc_arena_entry_init(nnc_u64 size, nnc_heap_ptr ptr) {
1010
nnc_arena_entry* entry = (nnc_arena_entry*)
1111
calloc(1, sizeof(nnc_arena_entry));
1212
if (entry == NULL) {
@@ -21,7 +21,7 @@ static nnc_arena_entry* nnc_arena_entry_init(nnc_u64 size, nnc_heap_ptr ptr) {
2121
* @brief Finalizes arena entry instance.
2222
* @param entry Pointer to arena entry to be freed.
2323
*/
24-
static void nnc_arena_entry_fini(nnc_arena_entry* entry) {
24+
nnc_static void nnc_arena_entry_fini(nnc_arena_entry* entry) {
2525
if (entry != NULL) {
2626
free(entry->hptr);
2727
entry->hptr = NULL;
@@ -34,7 +34,7 @@ static void nnc_arena_entry_fini(nnc_arena_entry* entry) {
3434
* @param arena Pointer to arena instance.
3535
* @return `true` if arena needs compressing, otherwise `false`.
3636
*/
37-
static nnc_bool nnc_arena_need_zip(nnc_arena* arena) {
37+
nnc_static nnc_bool nnc_arena_need_zip(nnc_arena* arena) {
3838
assert(arena->metrics.disposed >= 0);
3939
if (arena->metrics.disposed == 0) {
4040
return false;
@@ -49,7 +49,7 @@ static nnc_bool nnc_arena_need_zip(nnc_arena* arena) {
4949
* of existing entry pointer array.
5050
* @param arena Pointer to arena instance.
5151
*/
52-
static void nnc_arena_zip(nnc_arena* arena) {
52+
nnc_static void nnc_arena_zip(nnc_arena* arena) {
5353
nnc_u64 initial = arena->metrics.len;
5454
// reset len metric, because it will be recalculated.
5555
arena->metrics.len = 0;
@@ -71,7 +71,7 @@ static void nnc_arena_zip(nnc_arena* arena) {
7171
* @brief Extends arena's max capacity.
7272
* @param arena Pointer to arena instance.
7373
*/
74-
static void nnc_arena_grow(nnc_arena* arena) {
74+
nnc_static void nnc_arena_grow(nnc_arena* arena) {
7575
arena->metrics.cap *= 2;
7676
nnc_u64 size = arena->metrics.cap
7777
* sizeof(nnc_arena_entry*);
@@ -88,7 +88,7 @@ static void nnc_arena_grow(nnc_arena* arena) {
8888
* @param arena Pointer to arena instance.
8989
* @param entry Pointer to entry that must be pushed.
9090
*/
91-
static void nnc_arena_push(nnc_arena* arena, nnc_arena_entry* entry) {
91+
nnc_static void nnc_arena_push(nnc_arena* arena, nnc_arena_entry* entry) {
9292
if (arena->metrics.cap == arena->metrics.len) {
9393
if (nnc_arena_need_zip(arena)) {
9494
nnc_arena_zip(arena);
@@ -106,7 +106,7 @@ static void nnc_arena_push(nnc_arena* arena, nnc_arena_entry* entry) {
106106
* @param arena Pointer to arena instance.
107107
* @param index Index of entry at `arena->entries` to be flushed.
108108
*/
109-
static void nnc_arena_flush(nnc_arena* arena, nnc_u64 index) {
109+
nnc_static void nnc_arena_flush(nnc_arena* arena, nnc_u64 index) {
110110
assert(arena->entries[index] != NULL);
111111
nnc_arena_entry* entry = arena->entries[index];
112112
// decrease number of allocated bytes

src/nnc_ast_dump.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ typedef struct _nnc_dump_data {
1414

1515
typedef void (*nnc_dump_fn)(nnc_dump_data);
1616

17-
static void nnc_dump_expr(nnc_dump_data data);
18-
static void nnc_dump_stmt(nnc_dump_data data);
19-
static void nnc_dump_compound_stmt(nnc_dump_data data);
17+
nnc_static void nnc_dump_expr(nnc_dump_data data);
18+
nnc_static void nnc_dump_stmt(nnc_dump_data data);
19+
nnc_static void nnc_dump_compound_stmt(nnc_dump_data data);
2020

21-
static nnc_bool nnc_is_escape(nnc_byte code) {
21+
nnc_static nnc_bool nnc_is_escape(nnc_byte code) {
2222
switch (code) {
2323
case '\0': case '\a':
2424
case '\b': case '\f':
@@ -31,7 +31,7 @@ static nnc_bool nnc_is_escape(nnc_byte code) {
3131
return false;
3232
}
3333

34-
static nnc_byte nnc_escape(nnc_byte code) {
34+
nnc_static nnc_byte nnc_escape(nnc_byte code) {
3535
switch (code) {
3636
case '\0': return '0';
3737
case '\a': return 'a';
@@ -49,13 +49,13 @@ static nnc_byte nnc_escape(nnc_byte code) {
4949
return '\0';
5050
}
5151

52-
static void nnc_dump_indent(nnc_i64 indent) {
52+
nnc_static void nnc_dump_indent(nnc_i64 indent) {
5353
for (nnc_i64 i = 0; i < indent; i++) {
5454
fprintf(stderr, "%s", " ");
5555
}
5656
}
5757

58-
static void nnc_dump_type(const nnc_type* type) {
58+
nnc_static void nnc_dump_type(const nnc_type* type) {
5959
if (type == NULL) {
6060
fprintf(stderr, _c(RED, "%s"), "?");
6161
}
@@ -64,7 +64,7 @@ static void nnc_dump_type(const nnc_type* type) {
6464
}
6565
}
6666

67-
static void nnc_dump_chr(nnc_dump_data data) {
67+
nnc_static void nnc_dump_chr(nnc_dump_data data) {
6868
const nnc_chr_literal* literal = data.exact;
6969
nnc_byte c_repr[3] = { 0 };
7070
c_repr[0] = literal->exact;
@@ -79,7 +79,7 @@ static void nnc_dump_chr(nnc_dump_data data) {
7979
fprintf(stderr, ">\n");
8080
}
8181

82-
static void nnc_dump_str(nnc_dump_data data) {
82+
nnc_static void nnc_dump_str(nnc_dump_data data) {
8383
const nnc_str_literal* literal = data.exact;
8484
fprintf(stderr, _c(BYEL, "str") " <val=");
8585
for (nnc_u64 i = 0; i < literal->length; i++) {
@@ -97,7 +97,7 @@ static void nnc_dump_str(nnc_dump_data data) {
9797
fprintf(stderr, ">\n");
9898
}
9999

100-
static void nnc_dump_ident(nnc_dump_data data) {
100+
nnc_static void nnc_dump_ident(nnc_dump_data data) {
101101
const nnc_ident* ident = data.exact;
102102
fprintf(stderr, _c(BCYN, "ident "));
103103
fprintf(stderr, "<val=\"%s\",", ident->name);
@@ -107,7 +107,7 @@ static void nnc_dump_ident(nnc_dump_data data) {
107107
fprintf(stderr, ">\n");
108108
}
109109

110-
static void nnc_dump_int(nnc_dump_data data) {
110+
nnc_static void nnc_dump_int(nnc_dump_data data) {
111111
const nnc_int_literal* literal = data.exact;
112112
fprintf(stderr, _c(BYEL, "int") " <");
113113
if (literal->is_signed) {
@@ -124,7 +124,7 @@ static void nnc_dump_int(nnc_dump_data data) {
124124
fprintf(stderr, ">\n");
125125
}
126126

127-
static void nnc_dump_dbl(nnc_dump_data data) {
127+
nnc_static void nnc_dump_dbl(nnc_dump_data data) {
128128
const nnc_dbl_literal* literal = data.exact;
129129
fprintf(stderr, _c(BYEL, "float "));
130130
fprintf(stderr, "%f", literal->exact);
@@ -134,7 +134,7 @@ static void nnc_dump_dbl(nnc_dump_data data) {
134134
fprintf(stderr, ">\n");
135135
}
136136

137-
static void nnc_dump_unary(nnc_dump_data data) {
137+
nnc_static void nnc_dump_unary(nnc_dump_data data) {
138138
const nnc_unary_expression* unary = data.exact;
139139
static const char* unary_str[] = {
140140
[UNARY_CAST] = "cast",
@@ -196,7 +196,7 @@ static void nnc_dump_unary(nnc_dump_data data) {
196196
}
197197
}
198198

199-
static void nnc_dump_binary(nnc_dump_data data) {
199+
nnc_static void nnc_dump_binary(nnc_dump_data data) {
200200
const nnc_binary_expression* binary = data.exact;
201201
static const char* binary_str[] = {
202202
[BINARY_ADD] = "+",
@@ -230,7 +230,7 @@ static void nnc_dump_binary(nnc_dump_data data) {
230230
nnc_dump_expr(DUMP_DATA(data.indent + 1, binary->rexpr));
231231
}
232232

233-
static void nnc_dump_ternary(nnc_dump_data data) {
233+
nnc_static void nnc_dump_ternary(nnc_dump_data data) {
234234
const nnc_ternary_expression* ternary = data.exact;
235235
fprintf(stderr, _c(BGRN, "ternary-expr "));
236236
fprintf(stderr, "<type=");
@@ -247,7 +247,7 @@ static void nnc_dump_ternary(nnc_dump_data data) {
247247
nnc_dump_expr(DUMP_DATA(data.indent + 1, ternary->rexpr));
248248
}
249249

250-
static void nnc_dump_expr(nnc_dump_data data) {
250+
nnc_static void nnc_dump_expr(nnc_dump_data data) {
251251
const nnc_expression* expr = data.exact;
252252
static const nnc_dump_fn dumpers[] = {
253253
[EXPR_DBL_LITERAL] = nnc_dump_dbl,
@@ -264,7 +264,7 @@ static void nnc_dump_expr(nnc_dump_data data) {
264264
}
265265
}
266266

267-
static void nnc_dump_if_stmt(nnc_dump_data data) {
267+
nnc_static void nnc_dump_if_stmt(nnc_dump_data data) {
268268
const nnc_if_stmt* if_stmt = data.exact;
269269
fprintf(stderr, _c(BMAG, "if-stmt\n"));
270270
nnc_dump_indent(data.indent + 1);
@@ -292,7 +292,7 @@ static void nnc_dump_if_stmt(nnc_dump_data data) {
292292
}
293293
}
294294

295-
static void nnc_dump_do_stmt(nnc_dump_data data) {
295+
nnc_static void nnc_dump_do_stmt(nnc_dump_data data) {
296296
const nnc_do_while_stmt* do_stmt = data.exact;
297297
fprintf(stderr, _c(BMAG, "do-stmt\n"));
298298
nnc_dump_indent(data.indent + 1);
@@ -303,7 +303,7 @@ static void nnc_dump_do_stmt(nnc_dump_data data) {
303303
nnc_dump_expr(DUMP_DATA(data.indent+1, do_stmt->cond));
304304
}
305305

306-
static void nnc_dump_for_stmt(nnc_dump_data data) {
306+
nnc_static void nnc_dump_for_stmt(nnc_dump_data data) {
307307
const nnc_for_stmt* for_stmt = data.exact;
308308
fprintf(stderr, _c(BMAG, "for-stmt\n"));
309309
nnc_dump_indent(data.indent + 1);
@@ -320,7 +320,7 @@ static void nnc_dump_for_stmt(nnc_dump_data data) {
320320
nnc_dump_stmt(DUMP_DATA(data.indent+1, for_stmt->body));
321321
}
322322

323-
static void nnc_dump_let_stmt(nnc_dump_data data) {
323+
nnc_static void nnc_dump_let_stmt(nnc_dump_data data) {
324324
const nnc_let_statement* let_stmt = data.exact;
325325
fprintf(stderr, _c(BMAG, "let-stmt "));
326326
fprintf(stderr, "<var=%s,", let_stmt->var->name);
@@ -334,14 +334,14 @@ static void nnc_dump_let_stmt(nnc_dump_data data) {
334334
}
335335
}
336336

337-
static void nnc_dump_goto_stmt(nnc_dump_data data) {
337+
nnc_static void nnc_dump_goto_stmt(nnc_dump_data data) {
338338
const nnc_goto_statement* goto_stmt = data.exact;
339339
fprintf(stderr, _c(BMAG, "goto-stmt ") "<label=");
340340
const nnc_expression_statement* body = goto_stmt->body->exact;
341341
nnc_dump_expr(DUMP_DATA(data.indent+1, body->expr));
342342
}
343343

344-
static void nnc_dump_type_stmt(nnc_dump_data data) {
344+
nnc_static void nnc_dump_type_stmt(nnc_dump_data data) {
345345
const nnc_type_statement* type_stmt = data.exact;
346346
fprintf(stderr, _c(BMAG, "type-stmt ") "<type=");
347347
nnc_dump_type(type_stmt->type);
@@ -350,7 +350,7 @@ static void nnc_dump_type_stmt(nnc_dump_data data) {
350350
fprintf(stderr, ">\n");
351351
}
352352

353-
static void nnc_dump_while_stmt(nnc_dump_data data) {
353+
nnc_static void nnc_dump_while_stmt(nnc_dump_data data) {
354354
const nnc_while_stmt* while_stmt = data.exact;
355355
fprintf(stderr, _c(BMAG, "while-stmt\n"));
356356
nnc_dump_indent(data.indent + 1);
@@ -361,31 +361,31 @@ static void nnc_dump_while_stmt(nnc_dump_data data) {
361361
nnc_dump_stmt(DUMP_DATA(data.indent+1, while_stmt->body));
362362
}
363363

364-
static void nnc_dump_empty_stmt(nnc_dump_data data) {
364+
nnc_static void nnc_dump_empty_stmt(nnc_dump_data data) {
365365
fprintf(stderr, _c(BMAG, "empty-stmt\n"));
366366
}
367367

368-
static void nnc_dump_break_stmt(nnc_dump_data data) {
368+
nnc_static void nnc_dump_break_stmt(nnc_dump_data data) {
369369
fprintf(stderr, _c(BMAG, "break-stmt\n"));
370370
}
371371

372-
static void nnc_dump_return_stmt(nnc_dump_data data) {
372+
nnc_static void nnc_dump_return_stmt(nnc_dump_data data) {
373373
const nnc_return_statement* ret_stmt = data.exact;
374374
fprintf(stderr, _c(BMAG, "return-stmt\n"));
375375
nnc_dump_indent(data.indent + 1);
376376
fprintf(stderr, TREE_BR "what=");
377377
nnc_dump_stmt(DUMP_DATA(data.indent+1, ret_stmt->body));
378378
}
379379

380-
static void nnc_dump_expr_stmt(nnc_dump_data data) {
380+
nnc_static void nnc_dump_expr_stmt(nnc_dump_data data) {
381381
const nnc_expression_statement* expr_stmt = data.exact;
382382
fprintf(stderr, _c(BMAG, "expr-stmt\n"));
383383
nnc_dump_indent(data.indent + 1);
384384
fprintf(stderr, TREE_BR "expr=");
385385
nnc_dump_expr(DUMP_DATA(data.indent+1, expr_stmt->expr));
386386
}
387387

388-
static void nnc_dump_compound_stmt(nnc_dump_data data) {
388+
nnc_static void nnc_dump_compound_stmt(nnc_dump_data data) {
389389
const nnc_compound_statement* compound = data.exact;
390390
fprintf(stderr, _c(BMAG, "compound-stmt"));
391391
fprintf(stderr, " <stmts=%lu>\n", buf_len(compound->stmts));
@@ -396,11 +396,11 @@ static void nnc_dump_compound_stmt(nnc_dump_data data) {
396396
}
397397
}
398398

399-
static void nnc_dump_continue_stmt(nnc_dump_data data) {
399+
nnc_static void nnc_dump_continue_stmt(nnc_dump_data data) {
400400
fprintf(stderr, _c(BMAG, "continue-stmt\n"));
401401
}
402402

403-
static void nnc_dump_namespace_stmt(nnc_dump_data data) {
403+
nnc_static void nnc_dump_namespace_stmt(nnc_dump_data data) {
404404
const nnc_namespace_statement* namespace_stmt = data.exact;
405405
fprintf(stderr, _c(BRED, "namespace-stmt ") "<name=%s>\n", namespace_stmt->var->name);
406406
nnc_statement** stmts = ((nnc_compound_statement*)(namespace_stmt->body->exact))->stmts;
@@ -411,7 +411,7 @@ static void nnc_dump_namespace_stmt(nnc_dump_data data) {
411411
}
412412
}
413413

414-
static void nnc_dump_fn_stmt(nnc_dump_data data) {
414+
nnc_static void nnc_dump_fn_stmt(nnc_dump_data data) {
415415
const nnc_fn_statement* fn_stmt = data.exact;
416416
fprintf(stderr, _c(BRED, "fn-stmt ") "<name=%s, paramc=%lu, proto=",
417417
fn_stmt->var->name, buf_len(fn_stmt->params));
@@ -432,7 +432,7 @@ static void nnc_dump_fn_stmt(nnc_dump_data data) {
432432
}
433433
}
434434

435-
static void nnc_dump_stmt(nnc_dump_data data) {
435+
nnc_static void nnc_dump_stmt(nnc_dump_data data) {
436436
const nnc_statement* stmt = data.exact;
437437
static const nnc_dump_fn dumpers[] = {
438438
[STMT_IF] = nnc_dump_if_stmt,

src/nnc_buf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ typedef struct _nnc_buf {
1616
#define nncbuf__len(buf) nncbuf__hdr(buf)->len
1717
#define nncbuf__cap(buf) nncbuf__hdr(buf)->cap
1818

19-
#define nncbuf__grow(buf) nncbuf_grow(buf, sizeof(*(buf)), NNC_BUF_INICAP)
19+
#define nncbuf__grow(buf) nncbuf_grow((void*)(buf), sizeof(*(buf)), NNC_BUF_INICAP)
2020
#define nncbuf__need(buf) ((buf) ? (nncbuf__cap(buf) <= nncbuf__len(buf)) : 1)
2121
#define nncbuf__fits(buf) ((nncbuf__need(buf)) ? ((buf) = nncbuf__grow(buf)) : 0)
2222

@@ -29,7 +29,7 @@ typedef struct _nnc_buf {
2929
#define buf_len(buf) ((buf) ? nncbuf__len(buf) : 0)
3030
#define buf_cap(buf) ((buf) ? nncbuf__cap(buf) : 0)
3131
#define buf_add(buf, item) (nncbuf__fits(buf), (buf)[nncbuf__len(buf)++] = (item))
32-
#define buf_free(buf) ((buf) ? nnc_dispose(nncbuf__hdr(buf)) : 0)
32+
#define buf_free(buf) ((buf) ? nnc_dispose(nncbuf__hdr(buf)) : (void)0)
3333

3434
void* nncbuf_grow(void* buf, nnc_u64 type, nnc_u16 inicap);
3535

src/nnc_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _NNC_CORE_H
22
#define _NNC_CORE_H
33

4+
#include "nnc_static.h"
45
#include "nnc_buf.h"
56
#include "nnc_map.h"
67

0 commit comments

Comments
 (0)