Browse Source

Introduced valgrind into testing

long_to_char
Cameron Weinfurt 3 years ago
parent
commit
c151cf1011
  1. 2
      Makefile
  2. 20
      infint.c
  3. 2
      infint.h
  4. 14
      infint_four_func.c
  5. 12
      infint_util.c

2
Makefile

@ -2,7 +2,7 @@ objs = main.o token.o expr.o parse.o infint.o
dev: FLAGS= -g -D UNIT_TESTS
dev: build
./infcalc
valgrind ./infcalc
release: FLAGS= -O2
release: clean build

20
infint.c

@ -33,6 +33,15 @@ void test_utils() {
assert(!infint_less_than(ii2, 50));
ii1->data[7] = 1;
assert(!infint_less_than(ii1, 50));
assert(infint_less_than2(ii2,ii1));
assert(!infint_less_than2(ii1,ii2));
ii1->size = 1;
ii1->data[0] = 50;
assert(infint_less_than2(ii1,ii2));
infint_free(ii1);
infint_free(ii2);
}
void test_add() {
@ -64,6 +73,8 @@ void test_add() {
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->sign == 1);
infint_free(ii);
}
void test_sub() {
@ -86,6 +97,8 @@ void test_sub() {
assert(ii->data[0] == 25);
assert(ii->data[1] == 1);
assert(ii->sign == 1);
infint_free(ii);
}
void test_mult() {
@ -106,6 +119,8 @@ void test_mult() {
assert(ii->data[0] == 0);
assert(ii->data[1] == 0);
assert(ii->size == 1);
infint_free(ii);
}
void test_div() {
@ -127,6 +142,8 @@ void test_div() {
assert(ii->data[0] == 16);
assert(ii->data[1] == 0);
assert(ii->size == 1);
infint_free(ii);
}
void test_add2() {
@ -154,6 +171,9 @@ void test_add2() {
assert(ii1->data[1] == 0);
assert(ii1->data[2] == 1);
assert(ii1->size == 3);
infint_free(ii1);
infint_free(ii2);
}
void test_infint() {

2
infint.h

@ -22,11 +22,13 @@ void infint_free(infint_t *ii);
char infint_grow_to(infint_t *ii, size_t size);
void infint_comp(infint_t *ii);
char infint_less_than(infint_t *ii, uint8_t n);
char infint_less_than2(infint_t *ii1, infint_t *ii2);
// Four functions
void infint_add(infint_t *ii, uint8_t n);
void infint_add2(infint_t *ii1, infint_t *ii2);
void infint_sub(infint_t *ii, uint8_t n);
void infint_sub2(infint_t *ii1, infint_t *ii2);
void infint_mult(infint_t *ii, uint8_t n);
uint8_t infint_div(infint_t *ii, uint8_t n);

14
infint_four_func.c

@ -66,7 +66,9 @@ uint8_t infint_div(infint_t *ii, uint8_t n) {
void infint_add2(infint_t *ii1, infint_t *ii2) {
if(ii1->sign ^ ii2->sign) {
// Sub2 required first.
ii1->sign = ii2->sign;
infint_sub2(ii1, ii2);
ii1->sign ^= 1;
} else {
uint8_t acc = 0;
infint_grow_to(ii1, ii2->size);
@ -84,3 +86,13 @@ void infint_add2(infint_t *ii1, infint_t *ii2) {
}
}
}
void infint_sub2(infint_t *ii1, infint_t *ii2) {
if(ii1->sign ^ ii2->sign) {
ii1->sign = ii2->sign;
infint_add2(ii1, ii2);
ii1->sign = !ii2->sign;
} else {
}
}

12
infint_util.c

@ -11,7 +11,7 @@ infint_t *infint_clone(infint_t *ii) {
infint_t *iic = (infint_t *) malloc(sizeof(infint_t));
iic->capacity = ii->capacity;
iic->size = ii->size;
iic->data = (uint8_t *) malloc(ii->capacity * sizeof(uint8_t));
iic->data = (uint8_t *) calloc(ii->capacity, sizeof(uint8_t));
iic->sign = ii->sign;
memcpy(ii->data, iic->data, ii->capacity);
return iic;
@ -47,3 +47,13 @@ void infint_comp(infint_t *ii) {
char infint_less_than(infint_t *ii, uint8_t n) {
return ii->size == 1 && ii->data[0] < n;
}
char infint_less_than2(infint_t *ii1, infint_t *ii2) {
if (ii1->size < ii2->size) {
return 1;
} else if (ii1->size > ii2->size) {
return 0;
} else {
return ii1->data[ii1->size-1] < ii2->data[ii1->size-1];
}
}
Loading…
Cancel
Save