Branch
Hash :
29e7a7e6
Author :
Thomas de Grivel
Date :
2025-03-14T23:40:07
struct_type -> pstruct_type
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
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <string.h>
#include "../libkc3/callable.h"
#include "../libkc3/op.h"
#include "../libkc3/ops.h"
#include "../libkc3/pcallable.h"
#include "../libkc3/struct.h"
#include "../libkc3/sym.h"
#include "../libkc3/tag.h"
#include "test.h"
TEST_CASE_PROTOTYPE(ops_init_clean);
TEST_CASE_PROTOTYPE(ops_new_delete);
TEST_CASE_PROTOTYPE(ops_add);
TEST_CASE_PROTOTYPE(ops_get);
void ops_test (void)
{
TEST_CASE_RUN(ops_init_clean);
TEST_CASE_RUN(ops_new_delete);
TEST_CASE_RUN(ops_add);
TEST_CASE_RUN(ops_get);
}
TEST_CASE(ops_init_clean)
{
s_ops ops;
test_context("ops_init_clean");
TEST_EQ(ops_init(&ops), &ops);
ops_clean(&ops);
test_context(NULL);
}
TEST_CASE_END(ops_init_clean)
TEST_CASE(ops_new_delete)
{
s_ops *ops;
test_context("ops_new_delete");
TEST_ASSERT((ops = ops_new()));
ops_delete(ops);
test_context(NULL);
}
TEST_CASE_END(ops_init_clean)
TEST_CASE(ops_add)
{
s_ops *ops;
s_op *op;
s_tag op_tag = {0};
test_context("ops_add");
TEST_ASSERT((ops = ops_new()));
TEST_EQ(tag_init_pstruct(&op_tag, &g_sym_KC3_Op), &op_tag);
struct_allocate(op_tag.data.pstruct);
op = op_tag.data.pstruct->data;
op->sym = sym_1("+");
op->arity = 2;
op->precedence = 1;
op->associativity = 1;
TEST_ASSERT(pcallable_init(&op->pcallable));
TEST_ASSERT(ops_add(ops, &op_tag));
TEST_EQ(ops->ht.count, 1);
tag_clean(&op_tag);
ops_delete(ops);
test_context(NULL);
}
TEST_CASE_END(ops_add)
TEST_CASE(ops_get)
{
s_ops *ops;
s_op *op;
s_tag op_tag = {0};
test_context("ops_add");
TEST_ASSERT((ops = ops_new()));
TEST_EQ(tag_init_pstruct(&op_tag, &g_sym_KC3_Op), &op_tag);
struct_allocate(op_tag.data.pstruct);
op = op_tag.data.pstruct->data;
op->sym = sym_1("+");
op->arity = 2;
op->precedence = 3;
op->associativity = 1;
TEST_ASSERT(pcallable_init(&op->pcallable));
TEST_ASSERT(ops_add(ops, &op_tag));
TEST_EQ(ops->ht.count, 1);
tag_clean(&op_tag);
op_tag = (s_tag) {0};
TEST_ASSERT(ops_get(ops, sym_1("+"), 2, &op_tag));
TEST_EQ(op_tag.type, TAG_PSTRUCT);
TEST_ASSERT(op_tag.data.pstruct);
TEST_ASSERT(op_tag.data.pstruct->pstruct_type);
TEST_EQ(op_tag.data.pstruct->pstruct_type->module, sym_1("KC3.Op"));
TEST_ASSERT((op = op_tag.data.pstruct->data));
TEST_EQ(op->sym, sym_1("+"));
TEST_EQ(op->arity, 2);
TEST_EQ(op->precedence, 3);
TEST_EQ(op->associativity, 1);
tag_clean(&op_tag);
ops_delete(ops);
test_context(NULL);
}
TEST_CASE_END(ops_get)