Browse Source

Implemented infint subtraction and move four function tests into its own file

long_to_char
Cameron Weinfurt 3 years ago
parent
commit
d4cc01795a
  1. 154
      infint.c
  2. 4
      infint.h
  3. 14
      infint_four_func.c
  4. 177
      infint_test_ff.c
  5. 4
      infint_util.c

154
infint.c

@ -28,161 +28,35 @@ void test_utils() {
infint_comp(ii2);
assert(ii2->data[0] == 25);
assert(infint_less_than(ii2, 50));
assert(infint_abs_less_than(ii2, 50));
ii2->data[0] = 75;
assert(!infint_less_than(ii2, 50));
assert(!infint_abs_less_than(ii2, 50));
ii1->data[7] = 1;
assert(!infint_less_than(ii1, 50));
assert(!infint_abs_less_than(ii1, 50));
assert(infint_less_than2(ii2,ii1));
assert(!infint_less_than2(ii1,ii2));
assert(infint_abs_less_than2(ii2,ii1));
assert(!infint_abs_less_than2(ii1,ii2));
ii1->size = 1;
ii1->data[0] = 50;
assert(infint_less_than2(ii1,ii2));
assert(infint_abs_less_than2(ii1,ii2));
infint_free(ii1);
infint_free(ii2);
}
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(ii->size == 3);
assert(ii->capacity == 4);
infint_free(ii);
ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 1;
ii->size = 2;
ii->sign = 1;
infint_add(ii, 50);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->sign == 1);
infint_free(ii);
}
void test_sub() {
infint_t *ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 1;
ii->size = 2;
infint_sub(ii, 75);
assert(ii->data[0] == 25);
assert(ii->data[1] == 0);
assert(ii->sign == 0);
infint_sub(ii, 75);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->sign == 1);
infint_sub(ii, 75);
assert(ii->data[0] == 25);
assert(ii->data[1] == 1);
assert(ii->sign == 1);
infint_free(ii);
}
void test_mult() {
infint_t *ii = infint_new(2);
ii->data[0] = 1;
infint_mult(ii, 20);
assert(ii->data[0] == 20);
assert(ii->data[1] == 0);
assert(ii->size == 1);
infint_mult(ii, 20);
assert(ii->data[0] == 0);
assert(ii->data[1] == 4);
assert(ii->size == 2);
infint_mult(ii, 0);
assert(ii->data[0] == 0);
assert(ii->data[1] == 0);
assert(ii->size == 1);
infint_free(ii);
}
void test_div() {
infint_t *ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 10;
ii->size = 2;
assert(infint_div(ii, 2) == 0);
assert(ii->data[0] == 0);
assert(ii->data[1] == 5);
assert(infint_div(ii, 10) == 0);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->size == 1);
assert(infint_div(ii, 3) == 2);
assert(ii->data[0] == 16);
assert(ii->data[1] == 0);
assert(ii->size == 1);
infint_free(ii);
}
void test_add2() {
infint_t *ii1, *ii2;
ii1 = infint_new(4);
ii2 = infint_new(2);
ii2->data[0] = 75;
ii2->data[1] = 2;
ii2->size = 2;
infint_add2(ii1, ii2);
assert(ii1->data[0] == 75);
assert(ii1->data[1] == 2);
assert(ii1->size == 2);
infint_add2(ii1, ii2);
assert(ii1->data[0] == 50);
assert(ii1->data[1] == 5);
assert(ii1->size == 2);
ii2->data[1] = 94;
infint_add2(ii1, ii2);
assert(ii1->data[0] == 25);
assert(ii1->data[1] == 0);
assert(ii1->data[2] == 1);
assert(ii1->size == 3);
infint_free(ii1);
infint_free(ii2);
}
void test_infint() {
test_utils();
#include "infint_test_ff.c"
void test_four_function() {
test_add();
test_sub();
test_mult();
test_div();
test_add2();
test_sub2();
}
void test_infint() {
test_utils();
test_four_function();
}
#endif

4
infint.h

@ -21,8 +21,8 @@ infint_t *infint_clone(infint_t *ii);
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);
char infint_abs_less_than(infint_t *ii, uint8_t n);
char infint_abs_less_than2(infint_t *ii1, infint_t *ii2);
// Four functions
void infint_add(infint_t *ii, uint8_t n);

14
infint_four_func.c

@ -22,7 +22,7 @@ void infint_sub(infint_t *ii, uint8_t n) {
ii->sign = 0;
infint_add(ii, n);
ii->sign = 1;
} else if (infint_less_than(ii, n)) {
} else if (infint_abs_less_than(ii, n)) {
ii->data[0] = n - ii->data[0];
ii->sign = 1;
} else {
@ -92,7 +92,17 @@ void infint_sub2(infint_t *ii1, infint_t *ii2) {
ii1->sign = ii2->sign;
infint_add2(ii1, ii2);
ii1->sign = !ii2->sign;
} else if(infint_abs_less_than2(ii1,ii2)) {
infint_comp(ii1);
infint_add2(ii1,ii2);
ii1->data[ii1->size - 1] = 0;
while (ii1->data[ii1->size - 1] == 0) ii1->size -= 1;
ii1->sign ^= 1;
} else {
infint_comp(ii1);
infint_add2(ii1,ii2);
ii1->data[ii1->size - 1] = 0;
while (ii1->data[ii1->size - 1] == 0) ii1->size -= 1;
infint_comp(ii1);
}
}

177
infint_test_ff.c

@ -0,0 +1,177 @@
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(ii->size == 3);
assert(ii->capacity == 4);
infint_free(ii);
ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 1;
ii->size = 2;
ii->sign = 1;
infint_add(ii, 50);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->sign == 1);
infint_free(ii);
}
void test_sub() {
infint_t *ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 1;
ii->size = 2;
infint_sub(ii, 75);
assert(ii->data[0] == 25);
assert(ii->data[1] == 0);
assert(ii->sign == 0);
infint_sub(ii, 75);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->sign == 1);
infint_sub(ii, 75);
assert(ii->data[0] == 25);
assert(ii->data[1] == 1);
assert(ii->sign == 1);
infint_free(ii);
}
void test_mult() {
infint_t *ii = infint_new(2);
ii->data[0] = 1;
infint_mult(ii, 20);
assert(ii->data[0] == 20);
assert(ii->data[1] == 0);
assert(ii->size == 1);
infint_mult(ii, 20);
assert(ii->data[0] == 0);
assert(ii->data[1] == 4);
assert(ii->size == 2);
infint_mult(ii, 0);
assert(ii->data[0] == 0);
assert(ii->data[1] == 0);
assert(ii->size == 1);
infint_free(ii);
}
void test_div() {
infint_t *ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 10;
ii->size = 2;
assert(infint_div(ii, 2) == 0);
assert(ii->data[0] == 0);
assert(ii->data[1] == 5);
assert(infint_div(ii, 10) == 0);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->size == 1);
assert(infint_div(ii, 3) == 2);
assert(ii->data[0] == 16);
assert(ii->data[1] == 0);
assert(ii->size == 1);
infint_free(ii);
}
void test_add2() {
infint_t *ii1, *ii2;
ii1 = infint_new(4);
ii2 = infint_new(2);
ii2->data[0] = 75;
ii2->data[1] = 2;
ii2->size = 2;
infint_add2(ii1, ii2);
assert(ii1->data[0] == 75);
assert(ii1->data[1] == 2);
assert(ii1->size == 2);
infint_add2(ii1, ii2);
assert(ii1->data[0] == 50);
assert(ii1->data[1] == 5);
assert(ii1->size == 2);
ii2->data[1] = 94;
infint_add2(ii1, ii2);
assert(ii1->data[0] == 25);
assert(ii1->data[1] == 0);
assert(ii1->data[2] == 1);
assert(ii1->size == 3);
infint_free(ii1);
infint_free(ii2);
}
void test_sub2() {
infint_t *ii1, *ii2;
ii1 = infint_new(2);
ii1->size = 2;
ii1->data[0] = 0;
ii1->data[1] = 1;
ii2 = infint_new(1);
ii2->data[0] = 75;
infint_sub2(ii1, ii2);
assert(ii1->data[0] == 25);
assert(ii1->data[1] == 0);
assert(ii1->size == 1);
assert(ii1->sign == 0);
infint_sub2(ii1, ii2);
assert(ii1->data[0] == 50);
assert(ii1->data[1] == 0);
assert(ii1->size == 1);
assert(ii1->sign == 1);
infint_sub2(ii1, ii2);
assert(ii1->data[0] == 25);
assert(ii1->data[1] == 1);
assert(ii1->size == 2);
assert(ii1->sign == 1);
ii2->sign = 1;
infint_sub2(ii1, ii2);
assert(ii1->data[0] == 50);
assert(ii1->data[1] == 0);
assert(ii1->size == 1);
assert(ii1->sign == 1);
infint_sub2(ii1, ii2);
assert(ii1->data[0] == 25);
assert(ii1->data[1] == 0);
assert(ii1->size == 1);
assert(ii1->sign == 0);
infint_free(ii1);
infint_free(ii2);
}

4
infint_util.c

@ -44,11 +44,11 @@ void infint_comp(infint_t *ii) {
ii->data[i] = 100 - ii->data[i];
}
char infint_less_than(infint_t *ii, uint8_t n) {
char infint_abs_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) {
char infint_abs_less_than2(infint_t *ii1, infint_t *ii2) {
if (ii1->size < ii2->size) {
return 1;
} else if (ii1->size > ii2->size) {

Loading…
Cancel
Save