PreviousUpNext

15.4.886  src/lib/src/eval-unit-test.pkg

# eval-unit-test.pkg 

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

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

# Unit tests for:
#     Basic 'eval' functionality, basic Mythryl constructs.

stipulate
    package f8b =  eight_byte_float;                                    # eight_byte_float      is from   src/lib/std/eight-byte-float.pkg
herein

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

        eval   = makelib::scripting_globals::eval;

        evali  = makelib::scripting_globals::evali;
        evalf  = makelib::scripting_globals::evalf;
        evals  = makelib::scripting_globals::evals;

        evalli = makelib::scripting_globals::evalli;
        evallf = makelib::scripting_globals::evallf;
        evalls = makelib::scripting_globals::evalls;

        name = "src/lib/src/eval-unit-test.pkg tests";

        infix my --> ;

        fun a --> b
            =
            (a, b);


        int_tests
            =
            [   "2 + 2"              --> 4,
                "1 + 2"              --> 3,
                "6 & 3"              --> 2,
                "7 ^ 2"              --> 5,
                "010"                --> 8,
                "0x10"               --> 16,
                "strlen \"bloated\"" --> 7,
    #       "6!"                 --> 720
                "round 2.1"          --> 2,
                "round 1.9"          --> 2,
                "1 < 2 ?? 3 :: 4"    --> 3,

                "{ for (i = 6, factorial=1; i >= 1; --i; factorial) { factorial = factorial * i; }; }"
                    -->
                    720,

                "{ factorial = REF 1; foreach (1..6) {. factorial := *factorial * #i; }; *factorial; }" 
                    -->
                    720
            ];

        float_tests
            =
            [   "2.0 + 2.0"          --> 4.0
            ];

        string_tests
            =
            [   "\"abc\" + \"def\""  --> "abcdef",
                "toupper   \"abc\""  --> "ABC"
            ];

        int_list_tests
            =
            [   "[2, 2]"             --> 4,
                "[1, 2]"             --> 3
            ];

        float_list_tests
            =
            [   "[2.0, 2.0]"             --> 4.0,
                "[4.0, 4.0]"             --> 8.0
            ];

        string_list_tests
            =
            [   "[\"abc\", \"def\"]"  --> "abcdef"
            ];

        fun run_int_test (question, answer)
            =
            {   eval   ("makelib::scripting_globals::eval_kludge_ref_int := (" + question + ")");
                assert (*makelib::scripting_globals::eval_kludge_ref_int == answer);
            };

        fun run_float_test (question, answer)
            =
            {   ==== = f8b::(====);
                infix my  ==== ;
                eval   ("makelib::scripting_globals::eval_kludge_ref_float := (" + question + ")");
                assert (*makelib::scripting_globals::eval_kludge_ref_float ==== answer);
            };

        fun run_string_test (question, answer)
            =
            {   eval   ("makelib::scripting_globals::eval_kludge_ref_string := (" + question + ")");
                assert (*makelib::scripting_globals::eval_kludge_ref_string == answer);
            };

        fun run_int_test' (question, answer)
            =
            assert (evali question == answer);

        fun run_float_test' (question, answer)
            =
            {   ==== = f8b::(====);
                infix my  ==== ;
                assert (evalf question ==== answer);
            };

        fun run_string_test' (question, answer)
            =
            assert (evals question == answer);


        fun run_int_list_test (question, answer)
            =
            assert (int_sum (evalli question) == answer)
            where
                fun int_sum int_list
                    =
                    sum (int_list, 0)
                    where
                        fun sum ([],       result) => result;
                            sum ((i ! is), result) => sum (is, i + result);
                        end;
                    end;
            end;

        fun run_float_list_test (question, answer)
            =
            assert (float_sum (evallf question) ==== answer)
            where

                ==== = f8b::(====);

                infix my  ==== ;

                fun float_sum float_list
                    =
                    sum (float_list, 0.0)
                    where
                        fun sum ([],       result) => result;
                            sum ((i ! is), result) => sum (is, i + result);
                        end;
                    end;
            end;

        fun run_string_list_test (question, answer)
            =
            assert (string::cat (evalls question) == answer);

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

                apply    run_int_test     int_tests;
                apply  run_float_test   float_tests;
                apply run_string_test  string_tests;

                apply    run_int_test'    int_tests;
                apply  run_float_test'  float_tests;
                apply run_string_test' string_tests;

                apply    run_int_list_test    int_list_tests;
                apply  run_float_list_test  float_list_tests;
                apply run_string_list_test string_list_tests;

                summarize_unit_tests  name;
            };
    };
end;

## Code by Jeff Prothero: Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext