PreviousUpNext

15.4.968  src/lib/src/red-black-tagged-numbered-list-unit-test.pkg

## red-black-tagged-numbered-list-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-tagged-numbered-list.pkg


package red_black_tagged_numbered_list_unit_test {

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

    name =  "src/lib/src/red-black-tagged-numbered-list-unit-test.pkg unit tests";

    fun run ()
        =
        {
            printf "\nDoing %s:\n" name;

            my limit = 100;

            # Create a tagged sequence:
            #
            my seq: Tagged_Numbered_List( Int )
                =
                make ();

            assert (all_invariants_hold  seq);

            # Prepend some values into sequence,
            # collecting the resulting tags:
            #
            my tags
                =
                for (tags = sequence::empty, i = 0;  i < limit;  ++i;  tags) {

                    tag  = insert (seq, 0, i);

                    tags = sequence::set (tags, 0, tag);

                    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 that the tags have the expected values:
            #
#           for (i = 0;  i < limit;  ++i) {
#
#               assert (
#                   (tag_value (the (sequence::find (tags, i))))
#                   ==
#                   (limit - (i+1))
#               );
#           };


            # Check that the tags have the expected locations:
            #
#           for (i = 0;  i < limit;  ++i) {
#
#               assert (
#                   (find_tag (the (sequence::find (tags, i))))
#                   ==
#                   i
#               );
#           };


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

        #    # Create a sequence by successive appends:
        #    #
        #    my sequence
        #        =
        #        for (seq = empty, i = 0;  i < limit;  ++i; seq) {
        #
        #           seq = insert (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:
        #    for (i = 0;   i < limit;   ++i) {
        
        #       my (seq', val) = remove (seq, i);
        
        #        assert (all_invariants_hold seq');
        #    };
        
            # Try removing all values in pseudo-random order:
            #
            for (rng = random::make_random_number_generator (123, 73256), i = 0;   i < limit;   ++i) {
        
                to_remove = random::range (0, ((vals_count seq) - 1)) rng;

                remove (seq, to_remove);
        
                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) {
        #           my (seq, val) = the (pop seq);
        #           assert (val == 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) {
        #           my (seq, val) = the (shift seq);
        #           assert (val == 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
        #    );


            assert TRUE;

            # Still need to write code to exercise the
            # union, intersection, merge, apply and
            # map functions.           XXX BUGGO FIXME.


            summarize_unit_tests  name;
        };
};


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext