summaryrefslogtreecommitdiff
path: root/lab04/tests/test.c
blob: c10b13000f768fa1c37c917eb77ec4b14870abd0 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

#include "container_of.h"
#include "expression.h"
#include "list.h"
#include "polynomial.h"
#include "vec.h"

#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define CHECK(condition)                                                   \
    do {                                                                    \
        if (!(condition)) {                                                 \
            fprintf(stderr, "  failed: %s:%d: %s\n",                      \
                    __FILE__, __LINE__, #condition);                       \
            return false;                                                   \
        }                                                                   \
    } while (0)

static int compare_int(const void *lhs, const void *rhs)
{
    int left = *(const int *)lhs;
    int right = *(const int *)rhs;
    return (left > right) - (left < right);
}

static bool test_vec_lifecycle_and_access(void)
{
    struct vec vector;
    vec_init(&vector, int);
    CHECK(vec_empty(&vector));
    CHECK(vec_elem_size(&vector) == sizeof(int));

    int values[] = { 10, 20, 30 };
    for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
        CHECK(vec_push_back(&vector, &values[i]));
    }
    CHECK(vec_size(&vector) == 3);
    CHECK(vec_capacity(&vector) >= 3);
    CHECK(*(int *)vec_front(&vector) == 10);
    CHECK(*(int *)vec_back(&vector) == 30);
    CHECK(*(int *)vec_at_checked(&vector, 1) == 20);
    CHECK(vec_at_checked(&vector, 3) == NULL);

    vec_destroy(&vector);
    CHECK(vector.data == NULL);
    CHECK(vector.size == 0);
    CHECK(vector.capacity == 0);
    CHECK(vector.elem_size == 0);
    return true;
}

static bool test_vec_insert_erase_and_assign(void)
{
    struct vec vector;
    vec_init(&vector, int);
    int initial[] = { 1, 3 };
    CHECK(vec_append(&vector, initial, 2));

    int two = 2;
    CHECK(vec_insert(&vector, 1, &two));
    int tail[] = { 4, 5 };
    CHECK(vec_insert_range(&vector, vector.size, tail, 2));
    CHECK(vector.size == 5);
    CHECK(*(int *)vec_at(&vector, 0) == 1);
    CHECK(*(int *)vec_at(&vector, 4) == 5);

    vec_erase(&vector, 1);
    CHECK(*(int *)vec_at(&vector, 1) == 3);
    vec_erase_n(&vector, 1, 2);
    CHECK(vector.size == 2);
    CHECK(*(int *)vec_at(&vector, 0) == 1);
    CHECK(*(int *)vec_at(&vector, 1) == 5);

    int replacement[] = { 7, 8, 9 };
    CHECK(vec_assign_range(&vector, replacement, 3));
    CHECK(vector.size == 3);
    int repeated = 4;
    CHECK(vec_assign_n(&vector, 2, &repeated));
    CHECK(vector.size == 2);
    CHECK(*(int *)vec_at(&vector, 0) == 4);
    CHECK(*(int *)vec_at(&vector, 1) == 4);

    vec_destroy(&vector);
    return true;
}

static bool test_vec_resize_capacity_and_swap(void)
{
    struct vec vector;
    vec_init(&vector, int);
    CHECK(vec_reserve(&vector, 16));
    CHECK(vector.capacity >= 16);
    CHECK(vec_resize_zero(&vector, 4));
    for (size_t i = 0; i < vector.size; ++i) {
        CHECK(*(int *)vec_at(&vector, i) == 0);
    }

    int value = 11;
    CHECK(vec_assign_n(&vector, 1, &value));
    CHECK(vec_resize(&vector, 4));
    CHECK(*(int *)vec_at(&vector, 0) == 11);
    CHECK(vec_resize(&vector, 1));
    CHECK(vec_shrink_to_fit(&vector));
    CHECK(vector.capacity == vector.size);

    struct vec other;
    vec_init(&other, int);
    int other_value = 22;
    CHECK(vec_push_back(&other, &other_value));
    vec_swap(&vector, &other);
    CHECK(*(int *)vec_front(&vector) == 22);
    CHECK(*(int *)vec_front(&other) == 11);
    CHECK(!vec_reserve(&vector, SIZE_MAX));

    vec_destroy(&vector);
    vec_destroy(&other);
    return true;
}

static bool test_vec_iterators_and_comparison(void)
{
    struct vec vector;
    vec_init(&vector, int);
    int values[] = { 1, 2, 3 };
    CHECK(vec_append(&vector, values, 3));

    int *begin = vec_begin_as(&vector, int);
    int *end = vec_end_as(&vector, int);
    CHECK(vec_iter_distance(begin, end) == 3);
    CHECK(vec_iter_distance((int *)NULL, (int *)NULL) == 0);
    CHECK(*begin == 1);
    CHECK(*(end - 1) == 3);

    struct vec_reverse_iterator reverse = vec_rbegin(&vector);
    struct vec_reverse_iterator reverse_end = vec_rend(&vector);
    CHECK(vec_reverse_distance(reverse, reverse_end) == 3);
    CHECK(*(int *)vec_reverse_deref(reverse) == 3);
    vec_reverse_next(&reverse);
    CHECK(*(int *)vec_reverse_deref(reverse) == 2);
    vec_reverse_next(&reverse);
    CHECK(*(int *)vec_reverse_deref(reverse) == 1);

    struct vec same;
    vec_init(&same, int);
    CHECK(vec_append(&same, values, 3));
    CHECK(vec_equal(&vector, &same, compare_int));
    CHECK(vec_compare(&vector, &same, compare_int) == 0);
    vec_pop_back(&same);
    CHECK(vec_compare(&same, &vector, compare_int) < 0);

    vec_destroy(&vector);
    vec_destroy(&same);
    return true;
}

struct list_test_item {
    int value;
    struct list_head link;
};

static bool test_list_and_container_of(void)
{
    LIST_HEAD(head);
    struct list_test_item first = { .value = 1 };
    struct list_test_item second = { .value = 2 };
    struct list_test_item third = { .value = 3 };
    INIT_LIST_HEAD(&first.link);
    INIT_LIST_HEAD(&second.link);
    INIT_LIST_HEAD(&third.link);

    list_add(&first.link, &head);
    list_add_tail(&second.link, &head);
    list_add_tail(&third.link, &head);
    CHECK(!list_empty(&head));
    CHECK(list_is_first(&first.link, &head));
    CHECK(list_is_last(&third.link, &head));
    CHECK(list_entry(head.next, struct list_test_item, link)->value == 1);
    CHECK(list_entry(head.prev, struct list_test_item, link)->value == 3);

    struct list_test_item *cursor;
    int expected = 1;
    list_for_each_entry(cursor, &head, link) {
        CHECK(cursor->value == expected);
        ++expected;
    }
    list_move_tail(&first.link, &head);
    CHECK(list_entry(head.prev, struct list_test_item, link)->value == 1);
    list_del(&second.link);
    CHECK(list_entry(head.next, struct list_test_item, link)->value == 3);
    CHECK(list_entry_is_head(list_entry(head.next, struct list_test_item, link),
                             &head, link) == false);
    return true;
}

static bool test_poly_vec_normalization(void)
{
    struct poly_vec poly;
    poly_vec_init(&poly);
    CHECK(poly_vec_add_term(&poly, 3.0, 2));
    CHECK(poly_vec_add_term(&poly, 1.0, 5));
    CHECK(poly_vec_add_term(&poly, 2.0, 2));
    CHECK(poly_vec_size(&poly) == 2);
    CHECK(poly_vec_at_const(&poly, 0)->exp == 5);
    CHECK(poly_vec_at_const(&poly, 0)->coef == 1.0);
    CHECK(poly_vec_at_const(&poly, 1)->exp == 2);
    CHECK(poly_vec_at_const(&poly, 1)->coef == 5.0);
    CHECK(poly_vec_set_term(&poly, 0.0, 5));
    CHECK(!poly_vec_get_term(&poly, 5, &(double){ 0.0 }));
    CHECK(poly_vec_remove_term(&poly, 2));
    CHECK(poly_vec_empty(&poly));
    poly_vec_destroy(&poly);
    return true;
}

static bool test_poly_vec_add_sub_and_alias(void)
{
    struct poly_vec a;
    struct poly_vec b;
    struct poly_vec result;
    poly_vec_init(&a);
    poly_vec_init(&b);
    poly_vec_init(&result);
    CHECK(poly_vec_add_term(&a, 3.0, 2));
    CHECK(poly_vec_add_term(&a, 2.0, 1));
    CHECK(poly_vec_add_term(&a, 1.0, 0));
    CHECK(poly_vec_add_term(&b, 1.0, 2));
    CHECK(poly_vec_add_term(&b, -2.0, 1));
    CHECK(poly_vec_add_term(&b, 4.0, 0));

    CHECK(poly_vec_add(&result, &a, &b));
    CHECK(poly_vec_equal(&result, &b, 0.0) == false);
    CHECK(poly_vec_get_term(&result, 2, &(double){ 0.0 }));
    CHECK(result.terms.size == 2);
    CHECK(poly_vec_at_const(&result, 0)->coef == 4.0);
    CHECK(poly_vec_at_const(&result, 1)->exp == 0);
    CHECK(poly_vec_at_const(&result, 1)->coef == 5.0);

    CHECK(poly_vec_sub(&result, &a, &b));
    CHECK(poly_vec_at_const(&result, 0)->coef == 2.0);
    CHECK(poly_vec_at_const(&result, 1)->coef == 4.0);
    CHECK(poly_vec_at_const(&result, 2)->coef == -3.0);

    CHECK(poly_vec_add(&a, &a, &b));
    CHECK(a.terms.size == 2);
    CHECK(poly_vec_at_const(&a, 0)->coef == 4.0);
    CHECK(poly_vec_at_const(&a, 1)->coef == 5.0);

    poly_vec_destroy(&a);
    poly_vec_destroy(&b);
    poly_vec_destroy(&result);
    return true;
}

static bool test_poly_vec_mul_derivative_and_eval(void)
{
    struct poly_vec x_plus_one;
    struct poly_vec x_minus_one;
    struct poly_vec result;
    struct poly_vec derivative;
    poly_vec_init(&x_plus_one);
    poly_vec_init(&x_minus_one);
    poly_vec_init(&result);
    poly_vec_init(&derivative);
    CHECK(poly_vec_add_term(&x_plus_one, 1.0, 1));
    CHECK(poly_vec_add_term(&x_plus_one, 1.0, 0));
    CHECK(poly_vec_add_term(&x_minus_one, 1.0, 1));
    CHECK(poly_vec_add_term(&x_minus_one, -1.0, 0));
    CHECK(poly_vec_mul(&result, &x_plus_one, &x_minus_one));
    CHECK(result.terms.size == 2);
    CHECK(poly_vec_at_const(&result, 0)->exp == 2);
    CHECK(poly_vec_at_const(&result, 0)->coef == 1.0);
    CHECK(poly_vec_at_const(&result, 1)->exp == 0);
    CHECK(poly_vec_at_const(&result, 1)->coef == -1.0);
    CHECK(poly_vec_derivative(&derivative, &result, 1));
    CHECK(derivative.terms.size == 1);
    CHECK(poly_vec_at_const(&derivative, 0)->coef == 2.0);
    CHECK(poly_vec_eval(&result, 3.0) == 8.0);

    poly_vec_destroy(&x_plus_one);
    poly_vec_destroy(&x_minus_one);
    poly_vec_destroy(&result);
    poly_vec_destroy(&derivative);
    return true;
}

static bool test_poly_list_normalization_and_conversion(void)
{
    struct poly_list list;
    struct poly_vec vector;
    struct poly_list copy;
    poly_list_init(&list);
    poly_vec_init(&vector);
    poly_list_init(&copy);
    CHECK(poly_list_add_term(&list, 2.0, 1));
    CHECK(poly_list_add_term(&list, 5.0, 4));
    CHECK(poly_list_add_term(&list, 3.0, 1));
    CHECK(poly_list_size(&list) == 2);
    CHECK(poly_list_get_term(&list, 1, &(double){ 0.0 }));
    CHECK(list_entry(list.head.next, struct poly_node, link)->term.exp == 4);
    CHECK(poly_vec_from_list(&vector, &list));
    CHECK(poly_vec_size(&vector) == 2);
    CHECK(poly_vec_at_const(&vector, 0)->exp == 4);
    CHECK(poly_vec_at_const(&vector, 1)->coef == 5.0);
    CHECK(poly_list_from_vec(&copy, &vector));
    CHECK(poly_list_equal(&list, &copy, 0.0));
    CHECK(poly_list_remove_term(&list, 4));
    CHECK(poly_list_size(&list) == 1);

    poly_list_destroy(&list);
    poly_vec_destroy(&vector);
    poly_list_destroy(&copy);
    return true;
}

static bool test_poly_list_arithmetic_and_alias(void)
{
    struct poly_list a;
    struct poly_list b;
    struct poly_list result;
    struct poly_list derivative;
    poly_list_init(&a);
    poly_list_init(&b);
    poly_list_init(&result);
    poly_list_init(&derivative);
    CHECK(poly_list_add_term(&a, 3.0, 2));
    CHECK(poly_list_add_term(&a, 1.0, 0));
    CHECK(poly_list_add_term(&b, 1.0, 2));
    CHECK(poly_list_add_term(&b, -1.0, 0));
    CHECK(poly_list_add(&result, &a, &b));
    double coefficient;
    CHECK(poly_list_get_term(&result, 2, &coefficient));
    CHECK(coefficient == 4.0);
    CHECK(!poly_list_get_term(&result, 0, &coefficient));
    CHECK(poly_list_sub(&result, &a, &b));
    CHECK(poly_list_get_term(&result, 0, &coefficient));
    CHECK(coefficient == 2.0);
    CHECK(poly_list_derivative(&derivative, &a, 1));
    CHECK(poly_list_get_term(&derivative, 1, &coefficient));
    CHECK(coefficient == 6.0);
    CHECK(poly_list_eval(&a, 2.0) == 13.0);
    CHECK(poly_list_add(&a, &a, &b));
    CHECK(poly_list_get_term(&a, 2, &coefficient));
    CHECK(coefficient == 4.0);

    poly_list_destroy(&a);
    poly_list_destroy(&b);
    poly_list_destroy(&result);
    poly_list_destroy(&derivative);
    return true;
}

static bool test_expression_nested_precedence_and_variables(void)
{
    struct expression_context context;
    struct expression_error error;
    double result;
    expression_init(&context);
    CHECK(expression_collect_variables(&context,
        "-(A + 2.5) * (B - (-C + 4)) / 2", &error));
    CHECK(context.variables.size == 3);
    CHECK(expression_set_variable(&context, "A", 3.0));
    CHECK(expression_set_variable(&context, "B", 10.0));
    CHECK(expression_set_variable(&context, "C", 2.0));
    CHECK(expression_eval(&context,
        "-(A + 2.5) * (B - (-C + 4)) / 2", &result, &error));
    CHECK(fabs(result + 22.0) < 1e-12);
    expression_destroy(&context);
    return true;
}

static bool test_expression_variable_dedup_and_update(void)
{
    struct expression_context context;
    struct expression_error error;
    double result;
    expression_init(&context);
    CHECK(expression_collect_variables(&context,
        "A1 + A1 * B2 - (B2 / 2) + C", &error));
    CHECK(context.variables.size == 3);
    CHECK(expression_set_variable(&context, "A1", 4.0));
    CHECK(expression_set_variable(&context, "B2", 6.0));
    CHECK(expression_set_variable(&context, "C", 1.0));
    CHECK(expression_eval(&context,
        "A1 + A1 * B2 - (B2 / 2) + C", &result, &error));
    CHECK(fabs(result - 26.0) < 1e-12);
    CHECK(expression_set_variable(&context, "B2", 10.0));
    CHECK(expression_eval(&context,
        "A1 + A1 * B2 - (B2 / 2) + C", &result, &error));
    CHECK(result == 40.0);
    expression_destroy(&context);
    return true;
}

static bool test_expression_unary_and_decimal_values(void)
{
    struct expression_context context;
    struct expression_error error;
    double result;
    expression_init(&context);
    CHECK(expression_collect_variables(&context,
        "--5 + -(+2.5) * (3 - -1) + .5", &error));
    CHECK(expression_eval(&context,
        "--5 + -(+2.5) * (3 - -1) + .5", &result, &error));
    CHECK(fabs(result + 4.5) < 1e-12);
    expression_destroy(&context);
    return true;
}

static bool test_expression_deep_parentheses_and_associativity(void)
{
    struct expression_context context;
    struct expression_error error;
    double result;
    expression_init(&context);
    CHECK(expression_collect_variables(&context,
        "1 - 2 - 3 + 4 * (5 + (6 - (7 - 8)))", &error));
    CHECK(expression_eval(&context,
        "1 - 2 - 3 + 4 * (5 + (6 - (7 - 8)))", &result, &error));
    CHECK(fabs(result - 44.0) < 1e-12);
    expression_destroy(&context);
    return true;
}

static bool test_expression_error_paths(void)
{
    struct expression_context context;
    struct expression_error error;
    double result;
    expression_init(&context);
    CHECK(expression_collect_variables(&context, "X + 1", &error));
    CHECK(!expression_eval(&context, "X + 1", &result, &error));
    CHECK(error.code == EXPRESSION_UNSET_VARIABLE);
    CHECK(expression_set_variable(&context, "X", 2.0));
    CHECK(!expression_eval(&context, "X / (3 - 3)", &result, &error));
    CHECK(error.code == EXPRESSION_DIVISION_BY_ZERO);
    CHECK(!expression_eval(&context, "X + (2 * 3", &result, &error));
    CHECK(error.code == EXPRESSION_PAREN_ERROR);
    CHECK(!expression_collect_variables(&context, "X @ 2", &error));
    CHECK(error.code == EXPRESSION_SYNTAX_ERROR);
    expression_destroy(&context);
    return true;
}

struct test_case {
    const char *name;
    bool (*function)(void);
};

int main(void)
{
    const struct test_case tests[] = {
        { "vec lifecycle and access", test_vec_lifecycle_and_access },
        { "vec insert, erase and assign", test_vec_insert_erase_and_assign },
        { "vec resize, capacity and swap", test_vec_resize_capacity_and_swap },
        { "vec iterators and comparison", test_vec_iterators_and_comparison },
        { "list and container_of", test_list_and_container_of },
        { "poly_vec normalization", test_poly_vec_normalization },
        { "poly_vec add, sub and alias", test_poly_vec_add_sub_and_alias },
        { "poly_vec multiply, derivative and eval",
          test_poly_vec_mul_derivative_and_eval },
        { "poly_list normalization and conversion",
          test_poly_list_normalization_and_conversion },
        { "poly_list arithmetic and alias",
          test_poly_list_arithmetic_and_alias },
        { "expression nested precedence and variables",
          test_expression_nested_precedence_and_variables },
        { "expression variable dedup and update",
          test_expression_variable_dedup_and_update },
        { "expression unary and decimal values",
          test_expression_unary_and_decimal_values },
        { "expression deep parentheses and associativity",
          test_expression_deep_parentheses_and_associativity },
        { "expression error paths", test_expression_error_paths },
    };
    size_t failures = 0;

    for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
        printf("[%zu/%zu] %s\n", i + 1,
               sizeof(tests) / sizeof(tests[0]), tests[i].name);
        if (tests[i].function()) {
            puts("  passed");
        } else {
            ++failures;
            puts("  failed");
        }
    }
    printf("\n%zu/%zu tests passed\n",
           sizeof(tests) / sizeof(tests[0]) - failures,
           sizeof(tests) / sizeof(tests[0]));
    return failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}