Ejemplo n.º 1
0
bool test_big_int_copy_invalid_argument_2 (Test *test)
{
        BigInt a;

        TITLE ();
        CATCH (big_int_copy (&a, NULL));
        CATCH (error_at (0).error != ErrorInvalidArgument);
        CATCH (error_at (0).code != 2);
        PASS ();
}
Ejemplo n.º 2
0
bool test_big_int_copy_invalid_argument_1 (Test *test)
{
        BigInt to;

        TITLE ();
        CATCH (big_int_copy (NULL, &to));
        CATCH (error_at (0).error != ErrorInvalidArgument);
        CATCH (error_at (0).code != 1);
        PASS ();
}
Ejemplo n.º 3
0
bool test_big_int_copy_invalid_operation (Test *test)
{
        BigInt *a, *to;

        TITLE ();
        CATCH (!(a = big_int_create (0)));
        CATCH (!(to = big_int_create (0)));
        a->digits = 100;
        CATCH (big_int_copy (a, to));
        CATCH (error_at (0).error != ErrorInvalidOperation);
        big_int_destroy (a);
        big_int_destroy (to);
        PASS ();
}
Ejemplo n.º 4
0
bool test_big_int_copy (Test *test)
{
        BigInt *a, *b, *to;

        TITLE ();
        CATCH (!(a = big_int_create (123451234512345)));
        CATCH (!(b = big_int_create (123451234512345)));
        CATCH (!(to = big_int_create (0)));
        CATCH (!big_int_mul (a, b, to));
        CATCH (!big_int_copy (to, b));
        CATCH (!big_int_equals_string (b, "15240207302622001229957399025"));
        big_int_destroy (a);
        big_int_destroy (b);
        big_int_destroy (to);
        PASS ();
}
Ejemplo n.º 5
0
bool test_big_int_copy_function_call (Test *test)
{
        BigInt *a, *b, *to;

        TITLE ();
        CATCH (!(a = big_int_create (123451234512345)));
        CATCH (!(b = big_int_create (123451234512345)));
        CATCH (!(to = big_int_create (0)));
        CATCH (!big_int_mul (a, b, to));
        CATCH (!big_int_mul (a, to, b));
        memory_commit_limit (memory_commit_size ());
        CATCH (big_int_copy (b, a));
        CATCH (error_at (0).error != ErrorFunctionCall);
        big_int_destroy (a);
        big_int_destroy (b);
        big_int_destroy (to);
        PASS ();
}
Ejemplo n.º 6
0
int factor(big_int *a)
{
    int result = 0;
    int is_prime, n;
    big_int_str str;
    big_int *func_args[FUNC_ARGS_SIZE];
    int func_args_cnt = 0;
    int i;

    str.str = curr_lex.str;
    str.len = str.len_allocated = strlen(str.str);
    switch ((int) curr_lex.token) {
        case '(' :
            match('(');
            if (expr(a)) {
                result = 2;
                goto done;
            }
            if (!match(')')) {
                printf("wrong syntax. Expected ')', found %s\n", str.str);
                result = 3;
                goto done;
            }
            break;
        case DEC_NUM :
            if (big_int_from_str(&str, 10, a)) {
                printf("cannot convert string [%s] to decimal number\n", str.str);
                result = 4;
                goto done;
            }
            match(DEC_NUM);
            break;
        case HEX_NUM :
            if (big_int_from_str(&str, 16, a)) {
                printf("cannot convert string [%s] to hexadecimal number\n", str.str);
                result = 5;
                goto done;
            }
            match(HEX_NUM);
            break;
        case OCT_NUM :
            if (big_int_from_str(&str, 8, a)) {
                printf("cannot convert string [%s] to octal number\n", str.str);
                result = 6;
                goto done;
            }
            match(OCT_NUM);
            break;
        case BIN_NUM :
            if (big_int_from_str(&str, 2, a)) {
                printf("cannot convert string [%s] to binary number\n", str.str);
                result = 7;
                goto done;
            }
            match(BIN_NUM);
            break;
        case NEXT_PRIME :
            match(NEXT_PRIME);
            if (get_func_args("next_prime", func_args, 1, 1, &func_args_cnt)) {
                result = 8;
                goto done;
            }
            if (big_int_next_prime(func_args[0], a)) {
                printf("error in big_int_next_prime()\n");
                result = 9;
                goto done;
            }
            break;
        case IS_PRIME :
            match(IS_PRIME);
            if (get_func_args("is_prime", func_args, 1, 1, &func_args_cnt)) {
                result = 10;
                goto done;
            }
            if (big_int_is_prime(func_args[0], 100, 1, &is_prime)) {
                printf("error in big_int_is_prime()\n");
                result = 11;
                goto done;
            }
            if (big_int_from_int(is_prime, a)) {
                printf("error in big_int_from_int()\n");
                result = 12;
                goto done;
            }
            break;
        case GCD1 :
            match(GCD1);
            if (get_func_args("gcd", func_args, 2, 2, &func_args_cnt)) {
                result = 13;
                goto done;
            }
            if (big_int_gcd(func_args[0], func_args[1], a)) {
                printf("error in big_int_gcd()\n");
                result = 14;
                goto done;
            }
            break;
        case SQRT :
            match(SQRT);
            if (get_func_args("sqrt", func_args, 1, 1, &func_args_cnt)) {
                result = 15;
                goto done;
            }
            if (func_args[0]->sign == MINUS) {
                printf("cannot calculate square root from negative number\n");
                result = 16;
                goto done;
            }
            if (big_int_sqrt(func_args[0], a)) {
                printf("error in big_int_sqrt()\n");
                result = 17;
                goto done;
            }
            break;
        case SQR :
            match(SQR);
            if (get_func_args("sqr", func_args, 1, 1, &func_args_cnt)) {
                result = 18;
                goto done;
            }
            if (big_int_sqr(func_args[0], a)) {
                printf("error in big_int_sqr()\n");
                result = 19;
                goto done;
            }
            break;
        case SQRT_REM :
            match(SQRT_REM);
            if (get_func_args("sqrt_rem", func_args, 1, 1, &func_args_cnt)) {
                result = 19;
                goto done;
            }
            if (func_args[0]->sign == MINUS) {
                printf("cannot calculate square root from negative number\n");
                result = 16;
                goto done;
            }
            if (big_int_sqrt_rem(func_args[0], a)) {
                printf("error in big_int_sqrt_rem()\n");
                result = 20;
                goto done;
            }
            break;
        case RANDOM :
            match(RANDOM);
            if (get_func_args("random", func_args, 1, 1, &func_args_cnt)) {
                result = 21;
                goto done;
            }
            if (big_int_to_int(func_args[0], &n)) {
                printf("error in big_int_to_int()\n");
                result = 22;
                goto done;
            }
            if (n < 0) {
                printf("argument of random() must be positive\n");
                result = 23;
                goto done;
            }
            if (big_int_rand(rand, (size_t) n, a)) {
                printf("error in big_int_rand()\n");
                result = 24;
                goto done;
            }
            break;
        case ABS :
            match(ABS);
            if (get_func_args("abs", func_args, 1, 1, &func_args_cnt)) {
                result = 25;
                goto done;
            }
            if (big_int_abs(func_args[0], a)) {
                printf("error in big_int_abs()\n");
                result = 26;
                goto done;
            }
            break;
        case BIT_LEN :
            match(BIT_LEN);
            if (get_func_args("bit_len", func_args, 1, 1, &func_args_cnt)) {
                result = 27;
                goto done;
            }
            big_int_bit_length(func_args[0], (unsigned int *) &n);
            if (big_int_from_int(n, a)) {
                printf("error in big_int_from_int()\n");
                result = 28;
                goto done;
            }
            break;
        case BIT1_CNT :
            match(BIT1_CNT);
            if (get_func_args("bit1_cnt", func_args, 1, 1, &func_args_cnt)) {
                result = 29;
                goto done;
            }
            big_int_bit1_cnt(func_args[0], (unsigned int *) &n);
            if (big_int_from_int(n, a)) {
                printf("error in big_int_from_int()\n");
                result = 30;
                goto done;
            }
            break;
        case HAMMING_DIST :
            match(HAMMING_DIST);
            if (get_func_args("hamming_dist", func_args, 2, 2, &func_args_cnt)) {
                result = 31;
                goto done;
            }
            if (big_int_hamming_distance(func_args[0], func_args[1], (unsigned int *) &n)) {
                printf("error in big_int_hamming_distance()\n");
                result = 32;
                goto done;
            }
            if (big_int_from_int(n, a)) {
                printf("error in big_int_from_int()\n");
                result = 33;
                goto done;
            }
            break;
        case SET_BIT :
            match(SET_BIT);
            if (get_func_args("set_bit", func_args, 2, 2, &func_args_cnt)) {
                result = 34;
                goto done;
            }
            if (big_int_to_int(func_args[1], &n)) {
                printf("error in big_int_to_int()\n");
                result = 35;
                goto done;
            }
            if (big_int_set_bit(func_args[0], (size_t) n, a)) {
                printf("error in big_int_set_bit()\n");
                result = 36;
                goto done;
            }
            break;
        case CLR_BIT :
            match(CLR_BIT);
            if (get_func_args("clr_bit", func_args, 2, 2, &func_args_cnt)) {
                result = 37;
                goto done;
            }
            if (big_int_to_int(func_args[1], &n)) {
                printf("error in big_int_to_int()\n");
                result = 38;
                goto done;
            }
            if (big_int_clr_bit(func_args[0], (size_t) n, a)) {
                printf("error in big_int_clr_bit()\n");
                result = 39;
                goto done;
            }
            break;
        case INV_BIT :
            match(INV_BIT);
            if (get_func_args("inv_bit", func_args, 2, 2, &func_args_cnt)) {
                result = 40;
                goto done;
            }
            if (big_int_to_int(func_args[1], &n)) {
                printf("error in big_int_to_int()\n");
                result = 41;
                goto done;
            }
            if (big_int_inv_bit(func_args[0], (size_t) n, a)) {
                printf("error in big_int_inv_bit()\n");
                result = 42;
                goto done;
            }
            break;
        case TEST_BIT :
            match(TEST_BIT);
            if (get_func_args("test_bit", func_args, 2, 2, &func_args_cnt)) {
                result = 43;
                goto done;
            }
            if (big_int_to_int(func_args[1], &n)) {
                printf("error in big_int_to_int()\n");
                result = 44;
                goto done;
            }
            if (big_int_test_bit(func_args[0], (size_t) n, &n)) {
                printf("error in big_int_test_bit()\n");
                result = 45;
                goto done;
            }
            if (big_int_from_int(n, a)) {
                printf("error in big_int_from_int()\n");
                result = 46;
                goto done;
            }
            break;
        case JACOBI :
            match(JACOBI);
            if (get_func_args("jacobi", func_args, 2, 2, &func_args_cnt)) {
                result = 47;
                goto done;
            }
            if ((func_args[1]->num[0] & 1) == 0) {
                printf("second argument of jacobi() must be odd\n");
                result = 48;
                goto done;
            }
            if (big_int_jacobi(func_args[0], func_args[1], &n)) {
                printf("error in big_int_jacobi()\n");
                result = 49;
                goto done;
            }
            if (big_int_from_int(n, a)) {
                printf("error in big_int_from_int()\n");
                result = 50;
                goto done;
            }
            break;
        case SUBINT :
            match(SUBINT);
            if (get_func_args("subint", func_args, 3, 4, &func_args_cnt)) {
                result = 53;
                goto done;
            }
            {
                size_t bit_start, bit_len;
                int is_invert;
                if (big_int_to_int(func_args[1], (int *) &bit_start)) {
                    printf("error in big_int_to_int()\n");
                    result = 54;
                    goto done;
                }
                if (big_int_to_int(func_args[2], (int *) &bit_len)) {
                    printf("error in big_int_to_int()\n");
                    result = 55;
                    goto done;
                }
                if (func_args_cnt == 4) {
                    if (big_int_to_int(func_args[3], &is_invert)) {
                        printf("error in big_int_to_int()\n");
                        result = 56;
                        goto done;
                    }
                } else {
                    is_invert = 0;
                }
                if (big_int_subint(func_args[0], bit_start, bit_len, is_invert, a)) {
                    printf("error in big_int_subint()\n");
                    result = 57;
                    goto done;
                }
            }
            break;
        case DONE :
            printf("unexpected end of line.\n");
            result = 58;
            goto done;
            break;
        case '$' :
            match('$');
            if (curr_lex.token != DEC_NUM && curr_lex.token != '$') {
                printf("expected decimal number greater than zero or '$' after symbol '$'\n");
                result = 59;
                goto done;
            }
            if (curr_lex.token == '$') {
                /* return last number from history */
                match('$');
                n = history_pos;
                if (n == 0) {
                    n = HISTORY_SIZE;
                }
                if (big_int_copy(history[n - 1], a)) {
                    printf("error when copying variable $%d to [a]\n", n);
                    result = 60;
                    goto done;
                }
                goto done;
            }
            str.str = curr_lex.str;
            str.len = str.len_allocated = strlen(str.str);
            if (big_int_from_str(&str, 10, a)) {
                printf("error whe converting string [%s] to number\n", str.str);
                result = 61;
                goto done;
            }
            if (big_int_to_int(a, &n)) {
                printf("error when converting number [a] to int [n]\n");
                result = 62;
                goto done;
            }
            if (!n || n > HISTORY_SIZE) {
                printf("there is no variable $%d\n", n);
                result = 63;
                goto done;
            }
            if (big_int_copy(history[n - 1], a)) {
                printf("error when copying variable $%d to [a]\n", n);
                result = 64;
                goto done;
            }
            match(DEC_NUM);
            break;
        default :
            printf("syntax error. Unexpected lexeme [%s]. Token id = %d\n", str.str, curr_lex.token);
            result = 65;
            goto done;
    }

done:
    /* free func_args array */
    for (i = 0; i < func_args_cnt; i++) {
        big_int_destroy(func_args[i]);
    }
    return result;
}
Ejemplo n.º 7
0
int parse(void)
{
    big_int *a = NULL;
    big_int_str *str = NULL;
    int result = 0;
    unsigned int out_base = 10;

    a = big_int_create(1);
    str = big_int_str_create(1);
    if (a == NULL || str == NULL) {
        printf("error when creating [a] or [str]\n");
        result = 1;
        goto done;
    }

    while (1) {
        if (read_line(CALC_PROMPT)) {
            break;
        }
        if (line_next_char() == '\\') {
            /* command */
            switch (line_next_char()) {
                case 'q' : /* quit */
                    printf("quit\n");
                    goto done;
                case '?' : case 'h' : /* help */
                    help();
                    continue;
                case 'b' : /* change output base. Default is 10 */
                    if (sscanf(line.s, "%u", &out_base) != 1) {
                        printf("cannot recogize new base\n");
                    }
                    if (out_base < 2 || out_base > 36) {
                        printf("wrong value of base. Acceptible value is [2 - 36]\n");
                        out_base = 10;
                    } else {
                        printf("new base is %u\n", out_base);
                    }
                    continue;
                case 'm' : /* switch to modular arithmetic */
                    /* calculate module */
                    if (lexan()) {
                        continue;
                    }
                    is_mod = 0; /* disable module arithmetic */
                    if (expr(a)) {
                        /* error when calculating module */
                        continue;
                    }
                    if (big_int_copy(a, module)) {
                        printf("error when copying number [a] to [module]\n");
                        continue;
                    }
                    module->sign = PLUS;
                    if (module->len == 1 && module->num[0] < 2) {
                        printf("module must be greater than 1\n");
                        continue;
                    }
                    is_mod = 1; /* enable module arithmetic */
                    if (big_int_to_str(module, out_base, str)) {
                        printf("error during converting number to string\n");
                        continue;
                    }
                    printf("Switching to modular arithmetic. Module is %s\n", str->str);
                    continue;
                case 'i' : /* switch to integer arithmetic */
                    is_mod = 0;
                    printf("Switching to integer arithmetic\n");
                    continue;
                case 's' : /* show current settings */
                    puts("Current settings:");
                    printf("Base: %u\n", out_base);
                    printf("Mode: %s arithmetic\n", is_mod ? "modular (\\m)" : "integer (\\i)");
                    continue;
                default :
                    /* unknown command */
                    printf("unknown command\n");
                    continue;
            }
        }
        line_prev_char();
        /* parse line */
        if (lexan() || curr_lex.token == DONE) {
            continue;
        }
        while (!expr(a)) {
            if (curr_lex.token != ',' && curr_lex.token != DONE) {
                printf("wrong lexeme [%s]. Token id=%d. Expected ',' or \\n\n",
                    curr_lex.str, curr_lex.token);
                break;
            }
            match(','); /* go to the next expression */
            if (is_mod) {
                if (big_int_absmod(a, module, a)) {
                    printf("error in big_int_absmod()\n");
                    continue;
                }
            }
            if (big_int_to_str(a, out_base, str)) {
                printf("error during converting number to string\n");
                continue;
            }
            /* save result to the history */
            if (big_int_copy(a, history[history_pos])) {
                printf("error when copying number [a] to the history\n");
                continue;
            }
            /* print result to the display */
            history_pos++;
            printf("$%d = %s\n", history_pos, str->str);
            if (history_pos >= HISTORY_SIZE) {
                history_pos = 0;
            }
            if (curr_lex.token == DONE) {
                break;
            }
        }
    }

done:
    big_int_str_destroy(str);
    big_int_destroy(a);
    return result;
}
static int big_int_add_same_sign(big_int* dest, 
								const big_int* bigger, 
								const big_int* smaller)
{

	int ret, i, remain, new_digit,
		bigger_num_digit, smaller_num_digit, 
		bigger_i, smaller_i, dest_i;

	remain = 0;
	dest_i = bigger->digit_count;
	bigger_i = bigger->digit_count;
	smaller_i = smaller->digit_count;
	ret = big_int_copy(dest, bigger);
	if (ret < 0)
		return ret;

	for (i = 0; i < smaller->digit_count; ++i) {
		smaller_num_digit = bigger->num_buffer[--bigger_i] - '0';
		bigger_num_digit = smaller->num_buffer[--smaller_i] - '0';
		new_digit = smaller_num_digit + bigger_num_digit + remain;
		if (new_digit >= 10) {
			new_digit -= 10;
			remain = 1;
		} else {
			remain = 0;
		}

		dest->num_buffer[--dest_i] = new_digit + '0';
	}

	while(remain != 0) {
		if (dest_i == 0) {
			// need to resize
			char* realloc_tmp;
			realloc_tmp = (char*)realloc(dest->num_buffer, 
				dest->digit_count * sizeof(char) + 2);
			if (!realloc_tmp) {
				// This is a bad problem, which should happen only
				// if the system can't reallocate 2 more bytes.
				free(dest->num_buffer);
				return BI_MEMALLOC_ERROR;
			}

			dest->num_buffer = realloc_tmp;
			memmove(dest->num_buffer + 1, dest->num_buffer, dest->digit_count);
			dest->digit_count++;
			dest->_num_buffer_len = dest->digit_count + 1; 
			dest->num_buffer[0] = '1';
			break;
		} else {
			new_digit = dest->num_buffer[--dest_i] + remain - '0';
			if (new_digit >= 10) {
				new_digit -= 10;
				remain = 1;
			} else {
				remain = 0;
			}

			dest->num_buffer[dest_i] = new_digit + '0';
		}
	}

	dest->num_buffer[dest->digit_count] = '\0';
	return EXIT_SUCCESS;
}