/**************************************************************************** Kalkulon - A programmable calculator for programmers This is an example Kalkulon script. To load from within Kalkulon: Load("examples/scriptname.k") then you can call functions like: funcname() or funcname(arg0, ...) Author: Juergen Holetzeck 2003 - 2008 e-mail: contact@kalkulon.de homepage: www.kalkulon.de ****************************************************************************/ // Version ::regtest_ver = "20080426"; Load("examples/func.k"); Load("examples/prim.k"); Load("examples/bignum_test.k"); Load("examples/bigint_test.k"); Load("examples/list.k"); /////////////////////////////////////////////////////////////////////////////// // regression test regtest() = ( // constants primetable1000 = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}, fak100 = {93, 326215, 443944, 152681, 699238, 856266, 700490, 715968, 264381, 621468, 592963, 895217, 599993, 229915, 608941, 463976, 156518, 286253, 697920, 827223, 758251, 185210, 916864, 0, 0, 0, 0}, ::nerr = 0, // number of errors ::ntest= 0, // index of test PrintLn("Regtest Version "+regtest_ver), // first tests: test functionality of == and != and DOTEST PrintLn("The first test should fail!!!"), PTEST(), DOTEST(7/8==5/8), PrintLn("--- equality numbers"), PTEST(), DOTEST(7/8==5/8+2/8), PTEST(), DOTEST(7/8!=5/8), PTEST(), DOTEST(0x10==0x10), PTEST(), DOTEST(!(0x10!=0x10)), PTEST(), DOTEST(16==0x10), PTEST(), DOTEST(0x10==16), PTEST(), DOTEST(!(16!=0x10)), PTEST(), DOTEST(!(0x10!=16)), PrintLn("--- equality strings"), PTEST(), DOTEST("abc"+"xyz"=="abcxyz"), PTEST(), DOTEST(!("abc"+"xyz"!="abcxyz")), PTEST(), DOTEST("abc"+"xyz"!="abcxyza"), PTEST(), DOTEST(!("abc"+"xyz"=="abcxyza")), PrintLn("--- equality lists"), PTEST(), DOTEST({1,2,3}!={1,2}), PTEST(), DOTEST(!({1,2,3}=={1,2})), PTEST(), DOTEST({1,2,3}=={1,2}+{3}), PTEST(), DOTEST(!({1,2,3}!={1,2}+{3})), PTEST(), DOTEST({1,{2,4},3}=={1,{2,4}}+{3}), PTEST(), DOTEST({1,{2,4},3}!={1,{2,5}}+{3}), PTEST(), DOTEST(!({1,{2,4},3}!={1,{2,4}}+{3})), PTEST(), DOTEST(!({1,{2,4},3}=={1,{2,5}}+{3})), // // put your tests here: PTEST(), DOTEST(1-2*3==-5), PrintLn("--- number formats"), PTEST(), DOTEST(0xffffffff==0b11111111111111111111111111111111), PTEST(), DOTEST(0xfffffffe==0b11111111111111111111111111111110), PTEST(), DOTEST(0xfffeffff==0b11111111111111101111111111111111), PTEST(), DOTEST(0xffffffff==2**32-1), PTEST(), DOTEST(Sprintf("%08x", 0x00ff00ff)=="00ff00ff"), PTEST(), DOTEST(Sprintf("%X", 2**31+2)=="80000002"), PTEST(), DOTEST(Sprintf("%X", -1)=="FFFFFFFF"), PTEST(), DOTEST(Sprintf("%i", -1)=="-1"), PTEST(), DOTEST(Sprintf("%u", -1)=="4294967295"), PTEST(), DOTEST(Sprintf("%032b", 0x00ff00ff)=="00000000111111110000000011111111"), PTEST(), DOTEST(Sprintf("%032b", 0xfffffffe)=="11111111111111111111111111111110"), PTEST(), DOTEST(Sprintf("%032b", -1)=="11111111111111111111111111111111"), PrintLn("--- cast operations"), PTEST(), DOTEST((u8)~0 == 0xff), PTEST(), DOTEST((u16)~0 == 0xffff), PTEST(), DOTEST((u32)~0 == 0xffffffff), PTEST(), DOTEST((s8)~0 == -1), PTEST(), DOTEST((s16)~0 == -1), PTEST(), DOTEST((s32)~0 == -1), PTEST(), DOTEST((u8)-1 == 0xff), PTEST(), DOTEST((u16)-1 == 0xffff), PTEST(), DOTEST((u32)-1 == 0xffffffff), PTEST(), DOTEST((u8)~1 == 0xfe), PTEST(), DOTEST((u16)~2 == 0xfffd), PTEST(), DOTEST((u32)~3 == 0xfffffffc), PTEST(), DOTEST((s8)~1 == -2), PTEST(), DOTEST((s16)~2 == -3), PTEST(), DOTEST((s32)~3 == -4), PrintLn("--- shift operations"), PTEST(), DOTEST(1<<8 == 256), PTEST(), DOTEST(1<<16 == 65536), PTEST(), DOTEST(1<<24 == 2**24), PTEST(), DOTEST(1<<32 == 1), PrintLn("--- assignment and pre/post inc/dec for global/local"), int1 = 123, PTEST(), DOTEST((::int1=10, (::int1++==10) && ::int1==11)), PTEST(), DOTEST((::int1=20, (++::int1==21) && ::int1==21)), PTEST(), DOTEST((::int1=30, (::int1--==30) && ::int1==29)), PTEST(), DOTEST((::int1=40, (--::int1==39) && ::int1==39)), PTEST(), DOTEST((::int1*=2, ::int1==78)), PTEST(), DOTEST(int1==123), PTEST(), DOTEST((int1++, int1==124)), PTEST(), DOTEST((int1<<=1, int1-=10, int1==238)), // PrintLn("--- index operations"), array1 = ::array1 = {10,20,{30,40}}, PTEST(), DOTEST(array1[2,1]==array1[2][1]), PTEST(), DOTEST((array1[2,1]=50, array1[2][1]==50)), PTEST(), DOTEST(++array1[1]==21), PTEST(), DOTEST(array1[1]--==21), PTEST(), DOTEST(--array1[1]==19), PTEST(), DOTEST(array1[1]++==19), PTEST(), DOTEST(!(array1==::array1)), PTEST(), DOTEST(array1[2,1]--==50), PTEST(), DOTEST((::array1[2,1]=50,--::array1[2,1]==49)), PTEST(), DOTEST(array1==::array1), PTEST(), DOTEST((array1[1]/=10, ::array1[1]/=10, array1==::array1)), // PrintLn("--- Table operations"), PTEST(), DOTEST(Table(3,'#') == {1,2,3}), PTEST(), DOTEST(Table(3,5,'#') == {3,4,5}), PTEST(), DOTEST(Table(5,3,'#') == {5,4,3}), PTEST(), DOTEST(Table(3,5,0.5,'#') == {3,3.5,4,4.5,5}), PTEST(), DOTEST(Table(-3,'#') == {-1,-2,-3}), PTEST(), DOTEST(Table(-3,-5,'#') == {-3,-4,-5}), PTEST(), DOTEST(Table(-5,-3,'#') == {-5,-4,-3}), PTEST(), DOTEST(Table(-3,-5,0.5,'#') == {-3,-3.5,-4,-4.5,-5}), PTEST(), DOTEST(Table(0,'#') == {}), PrintLn("--- Built-in functions"), PTEST(), DOTEST(Back({1,2,3}) == 3), PTEST(), DOTEST(Back("Hallo World") == "d"), PTEST(), DOTEST(Erase({1,2,3,4,5}, 2) == {1, 2, 4, 5}), PTEST(), DOTEST(Erase({1,2,3,4,5}, 2, 4) == {1, 2, 5}), PTEST(), DOTEST(Erase("Hallo World", 6) == "Hallo orld"), PTEST(), DOTEST(Erase("Hallo World", 6, 11) == "Hallo "), PTEST(), DOTEST((expr="3+4*sqrt(9)", Eval(expr) == 15)), PTEST(), DOTEST(Front({1,2,3}) == 1), PTEST(), DOTEST(Front("Hallo World") == "H"), PTEST(), DOTEST(Insert({1,2,3,4,5}, 2, 33) == {1, 2, 33, 3, 4, 5}), PTEST(), DOTEST(Insert({1,2,3,4,5}, 2, {33, 44, 55}) == {1, 2, 33, 44, 55, 3, 4, 5}), PTEST(), DOTEST(Insert({1,2,3,4,5}, 2, {{33, 44, 55}}) == {1, 2, {33, 44, 55}, 3, 4, 5}), PTEST(), DOTEST(Insert("Hallo World", 5, " big") == "Hallo big World"), PTEST(), DOTEST(PopBack({1,2,3}) == {1, 2}), PTEST(), DOTEST(PopBack("Hallo") == "Hall"), PTEST(), DOTEST(PopFront({1,2,3}) == {2, 3}), PTEST(), DOTEST(PopFront("Hallo") == "allo"), PTEST(), DOTEST(PushBack({1,2,3}, 4) == {1, 2, 3, 4}), PTEST(), DOTEST(PushBack({1,2,3}, {4,5,6}) == {1, 2, 3, {4, 5, 6}}), PTEST(), DOTEST(PushBack("Hallo", " World") == "Hallo World"), PTEST(), DOTEST(PushFront({1,2,3}, 4) == {4, 1, 2, 3}), PTEST(), DOTEST(PushFront({1,2,3}, {4,5,6}) == {{4, 5, 6}, 1, 2, 3}), PTEST(), DOTEST(PushFront("Hallo", " World") == " WorldHallo"), PTEST(), DOTEST(Replace({1,2,3,4,5}, 2, 33) == {1, 2, 33, 4, 5}), PTEST(), DOTEST(Replace({1,2,3,4,5}, 2, {33,44}) == {1, 2, 33, 44, 5}), PTEST(), DOTEST(Replace({1,2,3,4,5}, 2, {{33,44}}) == {1, 2, {33, 44}, 4, 5}), PTEST(), DOTEST(Replace("Hallo World", 6, "Kalkulon") == "Hallo Kalkulon"), PTEST(), DOTEST(Size(3.141) == 1), PTEST(), DOTEST(Size("Hallo") == 5), PTEST(), DOTEST(Size({1,2,3,4}) == 4), PTEST(), DOTEST(Size({1,2,3,{4,5}}) == 4), PTEST(), DOTEST(Size({1,2,3,{4,5}}[3]) == 2), PTEST(), DOTEST(Sprintf("binary 0b%016b", 0xab) == "binary 0b0000000010101011"), PTEST(), DOTEST(Sprintf("%s: %i, %f, %o", {"Result", 15.6, 15.6, 15.6}) == "Result: 15, 15.600000, 17"), PTEST(), DOTEST(Sub({1,2,3,4,5}, 2) == {3, 4, 5}), PTEST(), DOTEST(Sub({1,2,3,4,5}, 2, 4) == {3, 4}), PTEST(), DOTEST(Sub("Hallo World", 6) == "World"), PTEST(), DOTEST(Sub("Hallo World", 0, 5) == "Hallo"), PTEST(), DOTEST(Type(0xabcd) == 2), PTEST(), DOTEST(Type(3.141) == 2), PTEST(), DOTEST(Type("Hallo") == 3), PTEST(), DOTEST(Type({1,2,3}) == 4), PTEST(), DOTEST(Type('1/#') == 5), PTEST(), DOTEST(Fold(Table(10, '#'), 0, '#1+#2') == 55), PTEST(), DOTEST(Fold(Table(10, '#'), 1, '#1*#2') == 3628800), PTEST(), DOTEST(FoldList(Table(10, '#'), 0, '#1+#2') == {0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55}), PTEST(), DOTEST(FoldList(Table(10, '#'), 1, '#1*#2') == {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}), PTEST(), DOTEST((tabsqr=Table(10, '#**2'), (tabseq=ForEach(tabsqr, 'sqrt(#)')) == {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})), PTEST(), DOTEST(ForEach(tabseq, tabsqr, '#1*#2') == {1, 8, 27, 64, 125, 216, 343, 512, 729, 1000}), PTEST(), DOTEST((SetNumFormat("%g = 0x%x"), (string){sqrt(2),0xabcd, 0b1100} == "{1.41421 = 0x1, 43981 = 0xabcd, 12 = 0xc}")), SetStdFormat(), PrintLn("--- tests for functions defined in 'examples'"), PTEST(), DOTEST(multT(primeDecomp(123456789))==123456789), PTEST(), DOTEST(primeTable(1000)==primetable1000), PTEST(), DOTEST(fak1(50)==fak2(50)), PTEST(), DOTEST(fak2(50)==fak3(50)), PTEST(), DOTEST(fak3(50)==fak4(50)), PTEST(), DOTEST(fak4(50)==fak5(50)), // PrintLn("--- tests for bignum/bigint"), ::BIG_NUM_BASE = 1e6, // dec base for bignum PTEST(), DOTEST(fakBn(100)==fak100), PTEST(), DOTEST(fak1Bn(100)==fak100), bignum_test(), i64_test(), // PrintLn("--- tests for ReadData/WriteData"), PTEST(), DOTEST(0==WriteData("examples/primes", primetable1000)), PTEST(), DOTEST(primetable1000 == ForEach(ReadData("examples/primes"), 'Eval(#)')), PTEST(), DOTEST((raw=ReadBinData("examples/primes"), WriteBinData("examples/primes2", raw) == 0)), PTEST(), DOTEST(primetable1000 == ForEach(ReadData("examples/primes2"), 'Eval(#)')), // Print("Number of errors (should be 1): "), PrintLn(::nerr), if(::nerr!=1; Beep(1000,500)), // benchmarks PrintLn("Starting benchmark..."), bench1(10), bench2(10), ::nerr ); PTEST() = (Print(Sprintf("Test %i", ++ntest))); DOTEST(ok) = (if(ok==1; PrintLn(" ok"); (PrintLn(" nok !!!!!!"), ++nerr)), ok); /////////////////////////////////////////////////////////////////////////////// // helper functions for regression test and benchmarking // benchmark1 bench1(n) = ( starttime = GetTicks(), // remember start ticks (in ms) while(n--; res=primeTable(1000)), // calculate n times some function PrintLn(Sprintf("Benchmark 1: %i", GetTicks()-starttime)), // print time consumed in ms res ); // benchmark2 bench2(n) = ( ::BIG_NUM_BASE = 1e6, // dec base for bignum starttime = GetTicks(), // remember start ticks (in ms) while(n--; res=fak1Bn(200)), // calculate n times some function PrintLn(Sprintf("Benchmark 2: %i", GetTicks()-starttime)), // print time consumed in ms res );