Browse Source

Added a test for parsing and expression creation

master
Cameron Weinfurt 1 year ago
parent
commit
d4bd3c7940
  1. 4
      expr.c
  2. 5
      expr.h
  3. 1
      main.c
  4. 42
      parse.c
  5. 7
      parse.h

4
expr.c

@ -46,7 +46,7 @@ void print_expr_tree(expr_t *t, int depth) {
print_expr_tree(t->left.expr, depth + 1);
}
else {
fprintf(stderr, "%lu\n", t->left.data);
fprintf(stderr, "%hhu\n", t->left.data);
}
for(register int i = depth; i > 0; --i) fputs(" ", stderr);
fprintf(stderr, "");
@ -54,7 +54,7 @@ void print_expr_tree(expr_t *t, int depth) {
print_expr_tree(t->right.expr, depth + 1);
}
else {
fprintf(stderr, "%lu\n", t->right.data);
fprintf(stderr, "%hhu\n", t->right.data);
}
}

5
expr.h

@ -2,6 +2,7 @@
#define EXPRH
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define EXFLAG_LEXP 0x1 // There is an expression on the left.
@ -25,11 +26,11 @@ typedef struct expr_t {
int opcode;
struct expr_t *par;
union {
unsigned long data;
uint8_t data;
struct expr_t *expr;
} left;
union {
unsigned long data;
uint8_t data;
struct expr_t *expr;
} right;
} expr_t;

1
main.c

@ -10,6 +10,7 @@
int main() {
test_infint();
test_expr_and_parse();
printf("**********************\n| All tests passed |\n**********************\n");
}

42
parse.c

@ -47,11 +47,9 @@ expr_t *parse_decimal_int() {
num->flags = num->left.data = 0;
num->par = NULL;
num->right.data = *ptr - '0';
for (int d = 0; d < 19; ++d) {
ptr++;
if (ptr == input_str) return num;
num->right.data = num->right.data * 10 + *ptr - '0';
}
ptr++;
if (ptr == input_str) return num;
num->right.data = num->right.data * 10 + *ptr - '0';
ptr++;
while(ptr != input_str) {
@ -60,11 +58,9 @@ expr_t *parse_decimal_int() {
more_num->flags = EXFLAG_MOD;
more_num->left.data = 0;
more_num->right.data = *ptr - '0';
for (int d = 0; d < 19; ++d) {
ptr++;
if (ptr == input_str) return bubble_expr(num, more_num);
more_num->right.data = more_num->right.data * 10 + *ptr - '0';
}
ptr++;
if (ptr == input_str) return bubble_expr(num, more_num);
more_num->right.data = more_num->right.data * 10 + *ptr - '0';
ptr++;
num = bubble_expr(num, more_num);
}
@ -271,3 +267,29 @@ expr_t *parse(char expect_rparen) {
}
}
}
#ifdef UNIT_TESTS
void test_expr_and_parse() {
const char *test_input = "15^2*6000+20-400/2";
input_str = test_input;
expr_t *e = parse(0);
assert(e->flags == 0x3);
assert(e->left.expr->flags == 0x3);
assert(e->left.expr->left.expr->flags == 0);
assert(e->left.expr->left.expr->left.data == 15);
assert(e->left.expr->left.expr->right.data == 2);
assert(e->left.expr->right.expr->flags == 0x30);
assert(e->left.expr->right.expr->left.data == 60);
assert(e->left.expr->right.expr->right.data == 0);
assert(e->right.expr->flags == 0xA);
assert(e->right.expr->left.data == 20);
assert(e->right.expr->right.expr->flags == 0x21);
assert(e->right.expr->right.expr->left.expr->flags == 0x30);
assert(e->right.expr->right.expr->left.expr->left.data == 40);
assert(e->right.expr->right.expr->left.expr->right.data == 0);
assert(e->right.expr->right.expr->right.data == 2);
destroy_expr(e);
}
#endif

7
parse.h

@ -8,4 +8,11 @@
expr_t *parse(char expect_rparen);
#ifdef UNIT_TESTS
#include <assert.h>
void test_expr_and_parse();
#endif
#endif
Loading…
Cancel
Save