Browse Source

Unit testing for infint

long_to_char
Cameron Weinfurt 3 years ago
parent
commit
109359f042
  1. 5
      Makefile
  2. 50
      infint.c
  3. 4
      infint.h
  4. 2
      infint_add.c
  5. 2
      infint_util.c
  6. 7
      main.c

5
Makefile

@ -1,9 +1,10 @@
objs = main.o token.o expr.o parse.o infint.o
flags = -g
flags = -g -D UNIT_TESTS
build: $(objs)
clang -o infcalc $(objs) $(flags)
./infcalc
clean:
rm *.o infcalc
@ -20,5 +21,5 @@ expr.o: expr.c expr.h
parse.o: parse.c parse.h token.h expr.h
clang -c parse.c -o parse.o $(flags)
infint.o: infint.c infint.h
infint.o: infint*.c
clang -c infint.c -o infint.o $(flags)

50
infint.c

@ -1,2 +1,52 @@
#include "infint.h"
#include "infint_util.c"
#include "infint_add.c"
#ifdef UNIT_TESTS
#define NDEBUG
#include <assert.h>
void test_utils() {
infint_t *ii1, *ii2;
ii1 = infint_new(1);
assert(ii1->data[0] = 0);
assert(ii1->size == ii1->capacity == 1);
assert(ii1->sign == 0);
ii2 = infint_clone(ii1);
assert(ii2->data[0] = 0);
assert(ii2->size == ii1->capacity == 1);
assert(ii2->sign == 0);
infint_grow_to(ii1, 8);
assert(ii2->data[0] = 0);
assert(ii2->size == ii1->capacity == 8);
assert(ii2->sign == 0);
}
void test_add() {
infint_t *ii = infint_new(2);
infint_add(ii, 50);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
infint_add(ii, 90);
assert(ii->data[0] == 40);
assert(ii->data[1] == 1);
ii->data[1] = 99;
infint_add(ii, 80);
assert(ii->data[0] == 20);
assert(ii->data[1] == 0);
assert(ii->data[2] == 1);
assert(ii2->size == 3);
assert(ii1->capacity == 4);
}
void test_infint() {
test_utils();
test_add();
}
#endif

4
infint.h

@ -11,6 +11,10 @@ typedef struct infint_t {
char sign;
} infint_t;
#ifdef UNIT_TESTS
void test_infint();
#endif
// Utility functions
infint_t *infint_new(size_t init_cap);
infint_t *infint_clone(infint_t *ii);

2
infint_add.c

@ -1,5 +1,3 @@
#include "infint.h"
void infint_add(infint_t *ii, uint8_t n) {
if(ii->sign) {
ii->sign = 0;

2
infint_util.c

@ -1,5 +1,3 @@
#include "infint.h"
infint_t *infint_new(size_t init_cap) {
infint_t *ii = (infint_t *) malloc(sizeof(infint_t));
ii->capacity = init_cap;

7
main.c

@ -4,12 +4,7 @@
#include "infint.h"
int main() {
infint_t *test1;
test1 = infint_new(2);
infint_add(test1, 20);
printf("%d, %d\n", test1->data[0], test1->data[1]);
infint_add(test1, 90);
printf("%d, %d\n", test1->data[0], test1->data[1]);
test_infint();
}
/*#include "expr.h"

Loading…
Cancel
Save