git @ Cat's Eye Technologies Collapsiv / master src / test_collapsiv.c
master

Tree @master (Download .tar.gz)

test_collapsiv.c @masterraw · history · blame

/*
 * SPDX-FileCopyrightText: (c) 2024 Chris Pressey, Cat's Eye Technologies
 *
 * SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Collapsiv
 */

#include <stdio.h>
#include <assert.h>

#define COLLAPSIV_TESTING
#include "collapsiv.h"

void test_push_atom() {
    reset_stack();
    push_atom('a');
    assert(get_stack_pointer() == 1);
    assert(is_atom(0));
    assert(get_stack_entry_atom(0) == 'a');
    printf("test_push_atom passed\n");
}

void test_make_ctor() {
    reset_stack();
    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    assert(get_stack_pointer() == 4);
    assert(get_stack_entry_type(3) == 2);  /* TERM_TYPE_CTOR */
    assert(get_stack_entry_arity(3) == 3);
    printf("test_make_term passed\n");
}

void test_term_size() {
    reset_stack();
    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    assert(term_size(0) == 1);
    assert(term_size(1) == 1);
    assert(term_size(2) == 1);
    assert(term_size(3) == 4);  /* (1 atom + 1 atom + 1 atom) + 1 ctor */
    printf("test_term_size passed\n");
}

void test_compare_terms() {
    reset_stack();
    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    int term1 = get_stack_pointer() - 1;

    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    int term2 = get_stack_pointer() - 1;

    assert(compare_terms(term1, term2) == 1);

    push_atom('x');
    push_atom('y');
    push_atom('z');
    make_ctor(3);
    int term3 = get_stack_pointer() - 1;

    assert(compare_terms(term1, term3) == 0);

    printf("test_compare_terms passed\n");
}

void test_compare_terms_deep() {
    reset_stack();

    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    int subterm = get_stack_pointer() - 1;
    push_atom('d');
    make_ctor(2);
    int term1 = get_stack_pointer() - 1;

    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    int term2 = get_stack_pointer() - 1;

    assert(compare_terms_deep(term2, term1) == subterm);
    printf("test_compare_terms_deep passed\n");
}

void test_compare_terms_deep_no_match() {
    reset_stack();

    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    push_atom('d');
    make_ctor(2);
    int term1 = get_stack_pointer() - 1;

    push_atom('x');
    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    make_ctor(2);
    int term2 = get_stack_pointer() - 1;

    assert(compare_terms_deep(term2, term1) == -1);
    printf("test_compare_terms_deep_no_match passed\n");
}

void test_pop() {
    reset_stack();
    push_atom('a');
    pop();
    assert(get_stack_pointer() == 0);

    push_atom('x');
    push_atom('y');
    push_atom('z');
    make_ctor(3);
    assert(get_stack_pointer() == 4);
    pop();
    assert(get_stack_pointer() == 0);
    printf("test_pop passed\n");
}

void test_deduplicate() {
    reset_stack();
    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    int term1 = get_stack_pointer() - 1;

    push_atom('a');
    push_atom('b');
    push_atom('c');
    make_ctor(3);
    int term2 = get_stack_pointer() - 1;

    /* term should have been deduplicated. */
    /* dump_stack(); */
    assert(get_stack_pointer() == 5);

    assert(is_pointer(term2));
    assert(get_stack_entry_pointer(term2) == term1);
    printf("test_deduplicate passed\n");
}

void test_dup() {
    reset_stack();
    push_atom('a');
    int term1 = get_stack_pointer() - 1;
    dup();
    int term2 = get_stack_pointer() - 1;
    assert(get_stack_pointer() == 2);
    assert(is_pointer(1));
    assert(get_stack_entry_pointer(1) == 0);
    assert(compare_terms(term1, term2) == 1);
    printf("test_dup passed\n");
}

int main() {
    printf("Running unit tests for term manipulation functions:\n");
    test_push_atom();
    test_make_ctor();
    test_term_size();
    test_compare_terms();
    test_compare_terms_deep();
    test_compare_terms_deep_no_match();
    test_pop();
    test_deduplicate();
    test_dup();
    printf("All tests passed!\n");
    return 0;
}