示例#1
0
int test_3()
{
    int size = 10000;
    int max = 50000;
    int i;
    float val[size];
    struct stack st;
    
    printf("Test 3: %d floats and destroy...", size);
    
    for (i = 0; i < size; i++) {
        val[i] = (float)((rand() % max) + 1);
    }
    
    stack_init(&st, sizeof(float));
    
    for (i = 0; i < size; i++) {
        if (stack_push(&st, &val[i]) < 0) {
            printf("error: push\n");
            return -1;
        }
    }
    
    stack_destroy(&st);

    if (stack_get_size(&st) != 0) {
        printf("size %d\n", stack_get_size(&st));
        printf("error: size should be 0\n");
        return -1;
    }

    printf(" OK!\n");
    return 0;
}
int calculation(struct stack_t *stack,char exp_string[EXPRESSION_SIZE])
{
    int counter, value, result, first_value, second_value;
    for(counter = 0; exp_string[counter] != '\0'; counter++)
    {
        if(exp_string[counter] >= '0' && exp_string[counter] <= '9') //checks if the string element is a value
        {
            value = exp_string[counter] - '0';
            stack_push_back(stack,value);
        }
        else if(exp_string[counter] == '*' || exp_string[counter] == '/' || exp_string[counter] == '+' || exp_string[counter] == '-')
        {
            if(stack_get_size(stack) < 2)
            {
                return INT_MIN;
            }
            else
            {
                second_value = stack_pop_back(stack);
                first_value = stack_pop_back(stack);
                switch(exp_string[counter])
                {
                    case '*' :
                        result = first_value * second_value;
                        break;
                        
                    case '/' :
                        result = first_value / second_value;
                        break;
                        
                    case '+' :
                        result = first_value + second_value;
                        break;
                        
                    case '-' :
                        result = first_value - second_value;
                        break;   
                }
                stack_push_back(stack,result);
            }
        }
    }
    if(stack_get_size(stack) == 1)
    {
        return result;
    }
    else
    {
        return INT_MAX;
    }
}
示例#3
0
int test_2()
{
    int size = 10000;
    int max = 50000;
    int i, j;
    float aux;
    float val[size];
    struct stack st;
    
    printf("Test 2: %d floats...", size);
    
    for (i = 0; i < size; i++) {
        val[i] = (float)((rand() % max) + 1);
    }
    
    stack_init(&st, sizeof(float));
    
    for (i = 0; i < size; i++) {
        if (stack_push(&st, &val[i]) < 0) {
            printf("error: push\n");
            return -1;
        }
    }
    
    for (j = size - 1; j >= 0; j--) {
        
        if (stack_get_size(&st) == 0) {
            printf("error: size should not be 0\n");
            return -1;
        }
        
        if (stack_pop(&st, &aux) < 0) {
            printf("error: pop\n");
            return -1;
        }
        
        if (aux != val[j]) {
            printf("error: aux (%f) != val[%d] (%f)\n", aux, j, val[j]);
            return -1;
        }
    }
    
    if (stack_get_size(&st) != 0) {
        printf("size %d\n", stack_get_size(&st));
        printf("error: size should be 0\n");
        return -1;
    }
    
    printf(" OK!\n");
    return 0;
}
示例#4
0
int main()
{
    struct stack st;
    int i;
    float x = 1.48;
    float y;
    int val = 100;
    
    stack_init(&st, sizeof(float));

    for (i = 0; i < 10; i++) {
        stack_push(&st, &x);
        x++;
    }

    while (stack_get_size(&st)) {
        stack_pop(&st, &y);
        printf("%f\n", y);
    }
    
    for (i = 0; i < 10; i++) {
        stack_push(&st, &x);
        x++;
    }

    while (stack_get_size(&st)) {
        stack_pop(&st, &y);
        printf("%f\n", y);
    }

    stack_destroy(&st);

    stack_init(&st, sizeof(int));

    for (i = 0; i < 10; i++) {
        stack_push(&st, &val);
        val++;
    }

    while (stack_get_size(&st)) {
        stack_pop(&st, &val);
        printf("%d\n", val);
    }

    test_1();
    test_2();
    test_3();

    return 0;
}
示例#5
0
void testStack() {
    // Create
    stack* my_stack = NULL;
    stack* dummy_stack = NULL;

    assert(stack_create(&my_stack) == 0); // Successfully create
    assert(stack_create(&dummy_stack) == 0); // Successfully create
    assert(stack_get_size(my_stack) == 0); // 0 size

    int a = 3;
    int b = 4;
    double c = 5.12;
    char d = 'c';
    char str[20] = "Hello World";

    // Successful pushes
    assert(stack_push(my_stack, &a, sizeof(a)) == 0);
    assert(stack_push(my_stack, &b, sizeof(b)) == 0);
    assert(stack_push(my_stack, &c, sizeof(c)) == 0);
    assert(stack_push(my_stack, &d, sizeof(d)) == 0);
    assert(stack_push(my_stack, str, strlen(str)) == 0);

    // Bad pushes:
    assert(stack_push(NULL, &a, sizeof(a)) != 0);
    assert(stack_push(my_stack, NULL, sizeof(a)) != 0);
    assert(stack_push(my_stack, &a, 0) != 0);

    assert(stack_get_size(my_stack) == 5); // 5 items

    int a2;
    int b2;
    double c2;
    char d2;
    char str2[20];

    // Bad pop:
    assert(stack_pop(NULL, &a) != 0);
    assert(stack_pop(dummy_stack, &a) != 0);
    assert(stack_pop(my_stack, NULL) != 0);

    // Successful pop:
    assert(stack_get_data_size(my_stack) <= 20);
    assert(stack_pop(my_stack, str2) == 0 && strcmp(str,str)==0);

    assert(stack_get_data_size(my_stack) == sizeof(d2));
    assert(stack_pop(my_stack, &d2) == 0 && d==d2);

    assert(stack_get_data_size(my_stack) == sizeof(c2));
    assert(stack_pop(my_stack, &c2) == 0 && c==c2);

    assert(stack_get_data_size(my_stack) == sizeof(b2));
    assert(stack_pop(my_stack, &b2) == 0 && b==b2);

    assert(stack_get_data_size(my_stack) == sizeof(a2));
    assert(stack_pop(my_stack, &a2) == 0 && a==a2);

    // Bad pop
    assert(stack_pop(my_stack, &a2) != 0);

    // Cleanup
    assert(stack_delete(my_stack) == 0); // Successfully create
    assert(stack_delete(dummy_stack) == 0); // Successfully create


    printf("Stack: Success\n");
}