Second test, just pop, almost identical to first put important for asserting pop works because it is very needed
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
// For identifing
|
// For identifing
|
||||||
typedef enum {
|
typedef enum {
|
||||||
NODE_NUMBER,
|
NODE_NUMBER,
|
||||||
NODE_OPERATOR,
|
NODE_BINARY_OP,
|
||||||
} ASTNodeType;
|
} ASTNodeType;
|
||||||
|
|
||||||
// For classify operators
|
// For classify operators
|
||||||
@@ -41,10 +41,12 @@ typedef struct ASTNode {
|
|||||||
ASTNodeType type;
|
ASTNodeType type;
|
||||||
union {
|
union {
|
||||||
double number;
|
double number;
|
||||||
|
struct {
|
||||||
struct ASTNode *left;
|
struct ASTNode *left;
|
||||||
struct ASTNode *right;
|
struct ASTNode *right;
|
||||||
Operator op;
|
Operator op;
|
||||||
} operator;
|
} binary;
|
||||||
|
} data;
|
||||||
} ASTNode;
|
} ASTNode;
|
||||||
|
|
||||||
// I prefer ot have a dynamic array for storing the "tokens"
|
// I prefer ot have a dynamic array for storing the "tokens"
|
||||||
|
|||||||
@@ -9,20 +9,21 @@
|
|||||||
static void test_array_push(void **state) {
|
static void test_array_push(void **state) {
|
||||||
(void) state;
|
(void) state;
|
||||||
|
|
||||||
|
// We use 2 to force resize and checking anything wrong with malloc
|
||||||
ASTNodeArray arr = ASTNodeArray_init(2);
|
ASTNodeArray arr = ASTNodeArray_init(2);
|
||||||
ASTNode node1 = {
|
ASTNode node1 = {
|
||||||
.type = NODE_NUMBER,
|
.type = NODE_NUMBER,
|
||||||
.operator = { .number = 90 }
|
.data = { .number = 90 }
|
||||||
};
|
};
|
||||||
|
|
||||||
ASTNode node2 = {
|
ASTNode node2 = {
|
||||||
.type = NODE_NUMBER,
|
.type = NODE_NUMBER,
|
||||||
.operator = { .number = 80 }
|
.data = { .number = 80 }
|
||||||
};
|
};
|
||||||
|
|
||||||
ASTNode node3 = {
|
ASTNode node3 = {
|
||||||
.type = NODE_NUMBER,
|
.type = NODE_NUMBER,
|
||||||
.operator = { .number = 70 }
|
.data = { .number = 70 }
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_int_equal(ASTNodeArray_push(&arr, node1), ARRAY_OK);
|
assert_int_equal(ASTNodeArray_push(&arr, node1), ARRAY_OK);
|
||||||
@@ -35,11 +36,44 @@ static void test_array_push(void **state) {
|
|||||||
assert_int_equal(ASTNodeArray_len(&arr), 3);
|
assert_int_equal(ASTNodeArray_len(&arr), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_array_pop(void **state) {
|
||||||
|
(void) state;
|
||||||
|
|
||||||
|
ASTNodeArray arr = ASTNodeArray_init(2);
|
||||||
|
ASTNode node1 = {
|
||||||
|
.type = NODE_NUMBER,
|
||||||
|
.data = { .number = 90 }
|
||||||
|
};
|
||||||
|
|
||||||
|
ASTNode node2 = {
|
||||||
|
.type = NODE_NUMBER,
|
||||||
|
.data = { .number = 80 }
|
||||||
|
};
|
||||||
|
|
||||||
|
ASTNode node3 = {
|
||||||
|
.type = NODE_NUMBER,
|
||||||
|
.data = { .number = 70 }
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_int_equal(ASTNodeArray_push(&arr, node1), ARRAY_OK);
|
||||||
|
assert_int_equal(ASTNodeArray_len(&arr), 1);
|
||||||
|
|
||||||
|
assert_int_equal(ASTNodeArray_push(&arr, node2), ARRAY_OK);
|
||||||
|
assert_int_equal(ASTNodeArray_len(&arr), 2);
|
||||||
|
|
||||||
|
assert_int_equal(ASTNodeArray_push(&arr, node3), ARRAY_OK);
|
||||||
|
assert_int_equal(ASTNodeArray_len(&arr), 3);
|
||||||
|
|
||||||
|
ASTNode node4;
|
||||||
|
assert_int_equal(ASTNodeArray_pop(&arr, 1, &node4), ARRAY_OK);
|
||||||
|
assert_int_equal(node4.type, NODE_NUMBER);
|
||||||
|
assert_int_equal(node4.data.number, 80);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
const struct CMUnitTest tests[] = {
|
const struct CMUnitTest tests[] = {
|
||||||
cmocka_unit_test(test_array_push),
|
cmocka_unit_test(test_array_push),
|
||||||
|
cmocka_unit_test(test_array_pop),
|
||||||
};
|
};
|
||||||
|
|
||||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||||
|
|||||||
Reference in New Issue
Block a user