PreviousUpNext

15.4.989  src/lib/src/red-black-sequence-unit-test.pkg

## red-black-sequence-unit-test.pkg

# Compiled by:
#     src/lib/test/unit-tests.lib

# Run by:
#     src/lib/test/all-unit-tests.pkg

# Unit test code for:
#     src/lib/src/red-black-numbered-list.pkg


package red_black_numbered_list_unit_test {

    include package   sequence;                                         # sequence                      is from   src/lib/src/sequence.pkg
    include package   unit_test;                                                # unit_test                     is from   src/lib/src/unit-test.pkg

    name =  "src/lib/src/red-black-sequence-unit-test.pkg unit tests";

    fun run ()
        =
        {

            printf "\nDoing %s:\n" name;


            my limit = 100;

            # Create a sequence by successive prepends:
            #
            my sequence
                =
                for (seq = empty, i = 0;  i < limit;  ++i; seq) {

                    seq = set (seq, 0, i);

                    assert (all_invariants_hold   seq);
                    assert (the (min_key    seq) == 0);
                    assert (the (max_key    seq) == i);
                    assert (     vals_count seq  == i+1);

                    assert (#1 (the (first_keyval_else_null seq)) == 0);
                    assert (#2 (the (first_keyval_else_null seq)) == i);

                    assert (#1 (the ( last_keyval_else_null seq)) == i);
                    assert (#2 (the ( last_keyval_else_null seq)) == 0);
                };

            # Check resulting sequence's contents:
                                                                                                my _ =
            for (i = 0;  i < limit;  ++i) {
                assert ((the (find (sequence, i))) == limit - (i+1));
                assert (sequence[i] == limit - (i+1));
            };

            # Create a sequence by successive appends:
            #
            my sequence
                =
                for (seq = empty, i = 0;  i < limit;  ++i; seq) {

                    seq = set (seq, i, i);

                    assert (all_invariants_hold   seq);
                    assert (the (min_key    seq) == 0);
                    assert (the (max_key    seq) == i);
                    assert (     vals_count seq  == i+1);

                    assert (not (contains_key (seq, -1  )));
                    assert (    (contains_key (seq,  0  )));
                    assert (    (contains_key (seq,  i  )));
                    assert (not (contains_key (seq,  i+1)));

                    assert (the (first_val_else_null seq) == 0);
                    assert (the ( last_val_else_null seq) == i);
                };

            # Check resulting sequence's contents:
                                                                                                my _ =
            for (i = 0;  i < limit;  ++i) {
                assert ((the (find (sequence, i))) == i);
                assert (sequence[i] == i);
            };


            # Try removing at all possible positions in sequence:
                                                                                                my _ =
            for (seq = sequence, i = 0;   i < limit;   ++i) {
                #
                seq' = remove (seq, i);

                assert (all_invariants_hold seq');
            };

            # Try removing all values in pseudo-random order:
                                                                                                my _ =
            for (rng = random::make_random_number_generator (123, 73256), seq = sequence, i = 0;   i < limit;   ++i) {
                #
                seq = remove (seq, random::range (0, the (max_key seq)) rng);

                assert (all_invariants_hold  seq);
            };

            # Test push and pop:
            #
            my sequence
                =
                for (seq = empty, i = 0;   i < limit;   ++i;  seq) {
                    seq = push (seq, i);
                };
            my seq
                =
                for (seq = sequence, i = limit - 1;   i >= 0;   --i;   seq) {
                    value = the (last_val_else_null seq);
                    seq   = the (pop seq);
                    assert (value == i);
                };
                                                                                                my _ =
            assert (is_empty seq);

            # Test unshift and shift:
            #
            my sequence
                =
                for (seq = empty, i = 0;   i < limit;   ++i;  seq) {
                    seq = unshift (seq, i);
                };
            my seq
                =
                for (seq = sequence, i = limit - 1;   i >= 0;   --i;   seq) {
                    value = the (first_val_else_null seq);
                    seq   = the (shift seq);
                    assert (value == i);
                };
                                                                                                my _ =
            assert (is_empty seq);

            # Some very cursory iterator tests:
            #
                                                                                                my _ =
            assert (6 == (fold_forward  {. #a + #b; } 0 (from_list (0..3))));                   my _ =
            assert (6 == (fold_backward {. #a + #b; } 0 (from_list (0..3))));                   my _ =
            assert (keyed_fold_forward  {. #a == #b and #c; } TRUE (from_list (0..16)));                my _ =
            assert (keyed_fold_backward {. #a == #b and #c; } TRUE (from_list (0..16)));                my _ =

            # Excercise 'compare_sequences':
            #
            assert (
                (compare_sequences
                    tagged_int::compare
                    ( from_list [ 0, 1, 2 ],
                      from_list [ 0, 1, 2 ]
                    )
                ) 
                ==
                EQUAL
            );                                                                                  my _ =

            assert (
                (compare_sequences
                    tagged_int::compare
                    ( from_list [ ],
                      from_list [ ]
                    )
                ) 
                ==
                EQUAL
            );                                                                                  my _ =

            assert (
                (compare_sequences
                    tagged_int::compare
                    ( from_list [ 0, 1, 3 ],
                      from_list [ 0, 1, 2 ]
                    )
                ) 
                ==
                GREATER
            );                                                                                  my _ =

            assert (
                (compare_sequences
                    tagged_int::compare
                    ( from_list [ 0, 1, 2 ],
                      from_list [ 0, 1, 3 ]
                    )
                ) 
                ==
                LESS
            );                                                                                  my _ =

            assert (
                (compare_sequences
                    tagged_int::compare
                    ( from_list [ 0, 1, 2 ],
                      from_list [ 0, 1    ]
                    )
                ) 
                ==
                GREATER
            );                                                                                  my _ =

            assert (
                (compare_sequences
                    tagged_int::compare
                    ( from_list [ 0, 1    ],
                      from_list [ 0, 1, 2 ]
                    )
                ) 
                ==
                LESS
            );

            # Still need to write code to exercise the
            # union, intersection, merge, apply and
            # map functions.           XXX SUCKO FIXME.
                                                                                                my _ =

            summarize_unit_tests  name;
        };
};


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext