Infinity Calculator implementation for Assignment 2 in Computer Organization.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

207 lines
4.1 KiB

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);
}
void test_mult2() {
infint_t *ii1, *ii2;
ii1 = infint_new(1);
ii1->data[0] = 25;
ii2 = infint_new(1);
ii2->data[0] = 50;
infint_mult2(ii1,ii2);
assert(ii1->size == 2);
assert(ii1->data[0] == 50);
assert(ii1->data[1] == 12);
infint_mult2(ii1,ii2);
assert(ii1->size == 3);
assert(ii1->data[0] == 0);
assert(ii1->data[1] == 25);
assert(ii1->data[2] == 6);
infint_mult2(ii1,ii1);
assert(ii1->size == 5);
assert(ii1->data[0] == 0);
assert(ii1->data[1] == 0);
assert(ii1->data[2] == 25);
assert(ii1->data[3] == 6);
assert(ii1->data[4] == 39);
infint_free(ii1);
infint_free(ii2);
}