void run_linked_list_tests() {
  char* inputs[] = {
    "item1",
    "item2",
    "item3",
  };
  int   num_inputs = sizeof(inputs) / sizeof(char*);
  char* data;

  // after init
  LinkedList *ll = ll_new();
  assert("Starts empty",      ll_is_empty(ll));
  assert("Initial size is 0", ll_size(ll)==0);
  assert("Peek returns null", ll_peek(ll)==NULL);

  // after pushing one item
  ll_push(ll, inputs[0]);
  assert("Is not empty after pushing an item",  !ll_is_empty(ll));
  assert("Has size of 1 after pushing an item", ll_size(ll)==1);
  assert("Peeking returns the item we pushed",  ll_peek(ll)==inputs[0]);

  // after two items
  ll_push(ll, inputs[1]);
  assert("Is not empty after pushing a second item",  !ll_is_empty(ll));
  assert("Has size of 2 after pushing a second item", ll_size(ll)==2);
  assert("Peeking returns the second item",           ll_peek(ll)==inputs[1]);

  // after three items
  ll_push(ll, inputs[2]);
  assert("Is not empty after pushing a third item",  !ll_is_empty(ll));
  assert("Has size of 3 after pushing a third item", ll_size(ll)==3);
  assert("Peeking returns the third item",           ll_peek(ll)==inputs[2]);

  // iterating through the items
  int index     = num_inputs;
  int all_match = 1;
  ll_each(ll, char* input, ({
    all_match &= (inputs[--index] == input);
  }));
  assert("It iterates the correct number of times", index==0);
  assert("The item provided matched each time",     all_match);

  // popping an item
  data = ll_pop(ll);
  assert("It is not empty after popping the third item", !ll_is_empty(ll));
  assert("Has size of 2 after popping the third item",   ll_size(ll)==2);
  assert("Peeking returns the second item",              ll_peek(ll)==inputs[1]);

  // cleanup
  ll_free(ll);
}
Exemple #2
0
int main(int argc, char** argv){
    if(argc == 1){ 
        printf("Please specify input file\n");
        exit(1);
    }

    FILE* f = fopen(argv[1], "r");
    
    if(!f){
        printf("File not found: %s\n", argv[1]);
        exit(1);
    }

    int pc = 0; /* Position in stream of current instruction */
    
    char mem[1024]; /* Memory array */
    char* dp = mem; /* Data pointer */

    bool debug = false;
    int line = 1;

    for(int i=0; i < 1024; ++i) mem[i] = 0;
    int c;
    do{
        c = getc(f); pc++;
        if (debug) printf("Character: %c, line: %d\n", c, line);
        switch(c){
            case '\n':
                if(debug) printf("Line %d\n", line);
                line += 1; break;
            case '>':
                dp++; break;
            case '<':
                dp--; break;
            case '+':
                (*dp)++; break;
            case '-':
                (*dp)--; break;
            case '.': 
                if(debug) printf("Print statement: %d\n", line);
                printf("%c", *dp); break;
            case ',': 
                (*dp) = getchar(); break;
            case '[': 
                if(*dp == 0){ /* If pointer is zero, find matching closing bracket */
                    int depth = 1;
                    do{
                        c = getc(f); pc++;
                        if(debug){ 
                            printf("depth = %d\n", depth);
                            printf("%c", c);
                        }
                        if(c == 91) depth += 1;
                        else if(c == 93) depth -=1;
                        else if(c == 10) line += 1;
                        else if(c == EOF){
                            printf("Unmatched bracket at %d", top->pos);
                            exit(1);
                        }
                    } while(depth != 0);
                }
                else{ /* else push the position onto the stack and move to next instruction */
                    ll_push(pc, line);
                    if(debug){
                        printf("[ not taken\n");
                        print_stack();
                    }
                }
                break;
            case ']':
                if(*dp){ /* Loop back to beginning, but don't remove loop start point from stack */
                    struct ll* st = ll_peek();
                    pc = st->pos;
                    line = st->line;
                    fseek(f, pc, SEEK_SET);
                }
                else{ /* Finished looping, remove loop start from stack */
                    ll_del();
                    if(debug){
                        printf("Popped stack");
                        print_stack();
                    }
                }
                break;
        }

    } while (c!=EOF);
    printf("\n");
}