PreviousUpNext

15.4.1020  src/lib/src/unit-test.pkg

## unit-test.pkg

# Compiled by:
#     src/lib/std/standard.lib


# Unit testing support.


stipulate
    package mtx =  winix_file_io_mutex;                                         # winix_file_io_mutex           is from   src/lib/std/src/io/winix-file-io-mutex.pkg
    package hth =  hostthread;                                                  # hostthread                    is from   src/lib/std/src/hostthread.pkg
herein

    package   unit_test
    :         Unit_Test                                                         # Unit_Test                     is from   src/lib/src/unit-test.api
    {
        total_units = REF 0;
        total_tests = REF 0;
        total_flaws = REF 0;

        tests  = REF 0;
        flaws  = REF 0;

        # Tally number of succeeding and failing tests:
        #
        fun assert' TRUE                                                        # We make assert' and assert hostthread-safe for the sake of   src/lib/std/src/hostthread-unit-test.pkg
                =>
                hth::with_mutex_do  mtx::mutex  {.
                    #
                    tests := *tests + 1;
                };

            assert' FALSE
                =>
                hth::with_mutex_do  mtx::mutex  {.
                    #
                    tests := *tests + 1;
                    flaws := *flaws + 1;
                    ();
                };
        end;

        # Same as above, but with narration:
        #
        fun assert TRUE
                =>
                {
                    hth::with_mutex_do  mtx::mutex  {.
                        #
                        tests := *tests + 1;
                    };
                    printf ".";                                                 # We used to do these printf()s while holding the mutex -- a Bad Idea that results in lockups
                    if (*tests & 077 == 0)   printf "\n";   fi;                 # when printf() requires a microthread switch, since runtime::microthread_switch_lock_refcell__global
                };                                                              # is set whenever the primary hostthread is holding a mutex.

            assert FALSE
                =>
                {
                    hth::with_mutex_do  mtx::mutex  {.
                        #
                        tests := *tests + 1;
                        flaws := *flaws + 1;
                    };

                    printf "\nTest %3d FAILED.\n" *tests;
                    ();
                };
        end;

        fun summarize_unit_tests unit_name
            =
            {    if  (*flaws == 0)   printf "\n%4d tests done,  no flaws detected by %s\n" *tests        unit_name;
                 else                printf "\n%4d tests done, %3d flaws detected by %s\n" *tests *flaws unit_name;    fi;


                 total_tests :=  *total_tests + *tests;
                 total_flaws :=  *total_flaws + *flaws;
                 total_units :=  *total_units + 1;

                 tests  :=   0;
                 flaws  :=   0;
            };

        fun summarize_all_tests ()
            =
            {    print "\n=========================\n";

                 if  (*total_flaws == 0)   printf "           No flaws seen.\n";
                 else                      printf    "      %7d flaws seen.\n"  *total_flaws;    fi;

                 printf "      %7d tests done.\n"     *total_tests;
                 printf "      %7d units done.\n"     *total_units;

                 print "=========================\n\n";
            };

        fun unit_tests_tried   () =  *tests;
        fun unit_flaws_found   () =  *flaws;

        fun total_tests_tried  () =  *total_tests;
        fun total_flaws_found  () =  *total_flaws;

    };
end;

## COPYRIGHT (c) 2008 Jeffrey S Prothero
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext