aboutsummaryrefslogtreecommitdiffhomepage
path: root/src-cslul/unittest/test_exprchk.c
blob: 7435e405822dcccbede19f7f555ca52e81d90624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

/*

  Unit tests of exprchk.c

  Copyright © 2022-2023 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#include "../exprchk.c"
#define SLULPARSE
#include "parsecommon.h"
#include "chkcommon.h"
#include "unittest.h"
#include "alltests.h"

#include <string.h>

static void test_exprchk_badconstexpr(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    TEST_SOURCE("data int x = 1\n"
                "data int y = x=2\n", 1);
    expect_error(CSLUL_E_CONSTEXPRBADOP, 2, 15);
    DO_VERIFY(ctx);
    free_ctx(ctx);
}

static void test_exprchk_cyclic(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    TEST_SOURCE("data int x = y\n"
                "data int y = x\n", 1);
    /* The error message could happen on line 1 or 2, column 10 or 14.
       This line/column is simply for the current implementation: */
    expect_error(CSLUL_E_CYCLICDECL, 2, 10);
    DO_VERIFY(ctx);
    free_ctx(ctx);
}

static void verify_recursion_test(int depth)
{
    static const char prefix[] = "data int x = 1";
    char buff[1024];
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    int i;
    char *cp = &buff[sizeof(prefix)-1];
    memcpy(buff, prefix, sizeof(prefix)-1);
    for (i = 0; i < depth; i++) {
        *(cp++) = '+';
        *(cp++) = '1';
    }
    *(cp++) = '\n';
    test_source(ctx, buff, cp-buff, 1);
    if (depth >= MAX_VFY_RECURSION_DEPTH) {
        expect_error(CSLUL_E_VERIFYTOODEEP, 1, 15);
    }
    DO_VERIFY(ctx);
    free_ctx(ctx);
}

static void test_exprchk_recursiondepth_ok(void)
{
    sourceline = __LINE__;
    verify_recursion_test(MAX_VFY_RECURSION_DEPTH-1);
}

static void test_exprchk_recursiondepth_bad(void)
{
    sourceline = __LINE__;
    verify_recursion_test(MAX_VFY_RECURSION_DEPTH);
}

/** Tests referencing of an identifier that is constant but not known at
    compile-time (to clients of the module). This should give an error */
static void test_exprchk_ident_noncompiletime(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_IFACE);
    /* y is run-time constant, but not compile-time constant */
    cslul_ll_set_current_filename(ctx, "main.slul");
    TEST_SOURCE("since 1.0\n"
                "data int x = y\n"
                "since 1.1\n"
                "data int y\n", 1);
    tassert(cslul_ll_start_phase(ctx, CSLUL_P_IMPL));
    cslul_ll_set_current_filename(ctx, "impl.slul");
    TEST_SOURCE("data int y = 123\n", 1);
    errors_in("main.slul");
    expect_error(CSLUL_E_IDENTNOTCOMPILETIME, 2, 14);
    DO_VERIFY(ctx);
    free_ctx(ctx);
}

/** Checks the error handling of type identifiers without a "target type".
    Type identifiers can only appear where there is a "target type"
    (i.e. when the type can be detected from the containing expression). */
static void test_exprchk_typeident_notarget1(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    TEST_SOURCE("data bool b = .typeident == 2\n", 1);
    expect_error(CSLUL_E_TYPESCOPENOTARGET, 1, 16);
    DO_VERIFY(ctx);
    free_ctx(ctx);
}

static void test_exprchk_typeident_notarget2(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    expect_error(CSLUL_E_BADSTMTLINESTART, 2, 5); /* error from parser */
    TEST_SOURCE("func f() {\n"
                "    .typeident\n"
                "}\n", 1);
    DO_VERIFY(ctx);
    free_ctx(ctx);
}

static void test_exprchk_typeident_notarget3(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    TEST_SOURCE("func f() {\n"
                "    bool b = (.typeident == 1)\n"
                "}\n", 1);
    expect_error(CSLUL_E_TYPESCOPENOTARGET, 2, 16);
    DO_VERIFY(ctx);
    free_ctx(ctx);
}

static void test_exprchk_typeident_notypescope(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    TEST_SOURCE("data int x = .thing\n"
                "data [2]int y = .thing\n", 1);
    expect_error(CSLUL_E_NOTYPESCOPE, 1, 15);
    expect_error(CSLUL_E_NOTYPESCOPE, 2, 18);
    DO_VERIFY(ctx);
    free_ctx(ctx);
}

static void test_exprchk_typeident_notfound(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    TEST_SOURCE("type T = struct { int x }\n"
                "data T y = .thing\n", 1);
    expect_error(CSLUL_E_TYPEIDENTNOTFOUND, 2, 13);
    DO_VERIFY(ctx);
    free_ctx(ctx);
}

static void assert_uint_decl(struct IdentDecl *decl, uint64 expectednum,
                             unsigned type, unsigned exprtype, int line)
{
    struct ExprNode *e;
    if (tsoftassert_line(line, decl != NULL) &&
            tsoftassert_line(line, decl->type.type == T_ELMNTRY) &&
            tsoftassert_line(line, decl->type.u.builtin == type) &&
            tsoftassert_line(line, decl->u.initval != NULL) &&
            tsoftassert_line(line, (e = decl->u.initval->root) != NULL) &&
            tsoftassert_line(line, e->exprtype == exprtype)) {
        tsoftassert_line(line, e->a.intval==expectednum);
        tsoftassert_line(line, decl->u.initval->rpn == e);
        tsoftassert_line(line, e->rpnnext == NULL);
    }
}

static void assert_uint(struct CSlul *ctx, const char *ident,
                        uint64 expectednum, unsigned type, unsigned exprtype,
                        int line)
{
    struct IdentDecl *decl = (struct IdentDecl *)lookup(
            ctx, ctx->impl.idents_root, ident);
    assert_uint_decl(decl, expectednum, type, exprtype, line);
}

static void assert_uint_local(struct CSlul *ctx,
        struct FuncBody *func, const char *ident,
        uint64 expectednum, unsigned type, unsigned exprtype,
        int line)
{
    struct IdentDecl *decl = (struct IdentDecl *)lookup(
            ctx, func->stmtblock.idents, ident);
    assert_uint_decl(decl, expectednum, type, exprtype, line);
}

#define ASSERT_BOOL(ident, expectednum) \
        assert_uint(ctx, ident, expectednum, BT_Bool, E_BOOL, __LINE__)
#define ASSERT_L_BOOL(func, ident, expectednum) \
        assert_uint_local(ctx, func, ident, \
                          expectednum, BT_Bool, E_BOOL, __LINE__)
#define ASSERT_L_INT(func, ident, expectednum) \
        assert_uint_local(ctx, func, ident, \
                          expectednum, BT_Int, E_INTEGER, __LINE__)

static void test_exprchk_constexpr_bool(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    TEST_SOURCE("data bool tr = true\n"
                "data bool fa = false\n"
                "data bool fa1 = not true\n"
                "data bool fa2 = not tr\n"
                "data bool fa3 = false or fa6\n"
                "data bool fa4 = false or not true\n"
                "data bool fa5 = fa4 and true\n"
                "data bool fa6 = (true or fa4) and fa\n"
                "data bool tr1 = tr and tr\n"
                "data bool tr2 = fa2 or not fa3\n", 1);
    DO_VERIFY(ctx);
    ASSERT_BOOL("tr", 1);
    ASSERT_BOOL("fa", 0);
    ASSERT_BOOL("fa1", 0);
    ASSERT_BOOL("fa2", 0);
    ASSERT_BOOL("fa3", 0);
    ASSERT_BOOL("fa4", 0);
    ASSERT_BOOL("fa5", 0);
    ASSERT_BOOL("fa6", 0);
    ASSERT_BOOL("tr1", 1);
    ASSERT_BOOL("tr2", 1);
    free_ctx(ctx);
}

static void test_exprchk_rpnfixup_full(void)
{
    struct IdentDecl *f;
    struct FuncBody *fb;
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    TEST_SOURCE("func rpnfixup() {\n"
                "    int fully11 = -1\n"
                "    int fully12 = (1 + 2) + 3\n"
                "    int fully13 = -1 * (2 + 3)\n"
                "    bool fully21 = not false\n"
                "    bool fully22 = false and true\n"
                "    bool fully23 = not (true and false)\n"
                "    bool fully24 = (not true) and false\n"
                "    bool fully25 = true and not false\n"
                "    bool fully26 = not (true and not false)\n"
                "    int fully31 = 2*-fully13 + 5\n"
                "}\n", 1);
    DO_VERIFY(ctx);
    f = (struct IdentDecl *)lookup(ctx, ctx->impl.idents_root, "rpnfixup");
    tassert(f != NULL);
    fb = f->u.funcbody;
    tassert(fb != NULL);
    ASSERT_L_INT(fb, "fully11", 1); /* stored as amplitude + sign */
    ASSERT_L_INT(fb, "fully12", 6);
    ASSERT_L_INT(fb, "fully13", 5); /* stored as amplitude + sign */
    ASSERT_L_BOOL(fb, "fully21", 1);
    ASSERT_L_BOOL(fb, "fully22", 0);
    ASSERT_L_BOOL(fb, "fully23", 1);
    ASSERT_L_BOOL(fb, "fully24", 0);
    ASSERT_L_BOOL(fb, "fully25", 1);
    ASSERT_L_BOOL(fb, "fully26", 0);
    ASSERT_L_INT(fb, "fully31", 15);
    free_ctx(ctx);
}

static void test_exprchk_rpnfixup_partial(void)
{
    struct IdentDecl *f;
    struct Stmt *s;
    struct ExprRoot *e;
    struct ExprNode *rpn;
    struct CSlul *ctx = create_ctx(CSLUL_P_IMPL);
    /* multiplication is evaluated 10, and the expression becomes:
        10 + x, and in RPN form:  10 x + */
    TEST_SOURCE("func rpnfixup(int x) -> int {\n"
                "    return -2*5 + x\n"
                "}\n", 1);
    DO_VERIFY(ctx);
    f = (struct IdentDecl *)lookup(ctx, ctx->impl.idents_root, "rpnfixup");
    tassert(f != NULL);
    tassert(f->u.funcbody != NULL);
    s = &f->u.funcbody->stmtblock.stmt;
    tassert(s->type == S_RETURN);
    e = s->u.expr;
    tassert(e != NULL);
    tassert(e->root != NULL);
    rpn = e->rpn;
    tassert(rpn != NULL);
    tassert(rpn->exprtype == E_INTEGER);
    tsoftassert(rpn->a.intval == 10);
    tsoftassert(INTLITERAL_IS_NEGATIVE(*rpn));
    rpn = rpn->rpnnext;
    tassert(rpn != NULL);
    tassert(rpn->exprtype == E_IDENT);
    rpn = rpn->rpnnext;
    tassert(rpn != NULL);
    tassert(rpn->exprtype == E_BINARYOP);
    tsoftassert(rpn->op == OP_ADD);
    tsoftassert(rpn->rpnnext == NULL);
    free_ctx(ctx);
}

const TestFunctionInfo tests_exprchk[] = {
    TEST_INFO(test_exprchk_badconstexpr)
    TEST_INFO(test_exprchk_cyclic)
    TEST_INFO(test_exprchk_recursiondepth_ok)
    TEST_INFO(test_exprchk_recursiondepth_bad)
    TEST_INFO(test_exprchk_ident_noncompiletime)
    TEST_INFO(test_exprchk_typeident_notarget1)
    TEST_INFO(test_exprchk_typeident_notarget2)
    TEST_INFO(test_exprchk_typeident_notarget3)
    TEST_INFO(test_exprchk_typeident_notypescope)
    TEST_INFO(test_exprchk_typeident_notfound)
    TEST_INFO(test_exprchk_constexpr_bool)
    TEST_INFO(test_exprchk_rpnfixup_full)
    TEST_INFO(test_exprchk_rpnfixup_partial)
    TEST_END
};