📄 regress.cal
字号:
/* * Copyright (c) 1994 David I. Bell * Permission is granted to use, distribute, or modify this source, * provided that this copyright notice remains intact. * * Test the correct execution of the calculator by reading this library file. * Errors are reported with '****' messages, or worse. :-) * * NOTE: Unlike most calc lib files, this one performs its work when * it is read. Normally one would just define functions and * values for later use. In the case of the regression test, * we do not want to do this. */print '000: Beginning regression tests';print '001: Beginning regression test suite read';print '002: Within each section, output should be numbered sequentially';static err;lib_debug = -1; /* disable lib startup messages */print '003: parsed variable definitions';define verify(test, str){ if (test != 1) { print '**** Non-true result (' : test : '): ' : str; ++err; return; } print str;}print '004: parsed verify()';define error(str){ print '****' , str; ++err;}print '005: parsed error(str)';define getglobalvar(){ global globalvar; return globalvar;}print '006: parsed getglobalvar()';/* * Test boolean operations and IF tests. * * Some of these tests are done twice, once to print the message and * once to count any errors. This means that some successful tests * will display a passing message twice. Oh well, no biggie. */define test_booleans(){ local x; local y; local t1, t2, t3; print '100: Beginning test_booleans'; if (0) print '**** if (0)'; if (0) err = err + 1; if (1) print '101: if (1)'; if (2) print '102: if (2)'; if (1) print '103: if (1) else'; else print '**** if (1) else'; if (1) print '104: if (1) else'; else err = err + 1; if (0) print '**** if (0) else'; else print '105: if (0) else'; if (0) err = err + 1; else print '106: if (0) else'; if (1 == 1) print '107: if 1 == 1'; else print '**** if 1 == 1'; if (1 == 1) print '108: if 1 == 1'; else err = err + 1; if (1 != 2) print '109: if 1 != 2'; else print '**** if 1 != 2'; if (1 != 2) print '110: if 1 != 2'; else err = err + 1; verify(1, '111: verify 1'); verify(2 == 2, '112: verify 2 == 2'); verify(2 != 3, '113: verify 2 != 3'); verify(2 < 3, '114: verify 2 < 3'); verify(2 <= 2, '115: verify 2 <= 2'); verify(2 <= 3, '116: verify 2 <= 3'); verify(3 > 2, '117: verify 3 > 2'); verify(2 >= 2, '118: verify 2 >= 2'); verify(3 >= 2, '119: verify 3 >= 2'); verify(!0, '120: verify !0'); verify(!1 == 0,'121: verify !1 == 0'); print '122: Ending test_booleans';}print '007: parsed test_booleans()';/* * Test variables and simple assignments. */define test_variables(){ local x1, x2, x3; global g1, g2; local t; global globalvar; print '200: Beginning test_variables'; x1 = 5; x3 = 7 * 2; x2 = 9 + 1; globalvar = 22; g1 = 19 - 3; g2 = 79; verify(x1 == 5, '201: x1 == 5'); verify(x2 == 10, '202: x2 == 10'); verify(x3 == 14, '203: x3 == 14'); verify(g1 == 16, '204: g1 == 16'); verify(g2 == 79, '205: g2 == 79'); verify(globalvar == 22, '204: globalvar == 22'); verify(getglobalvar() == 22, '205: getglobalvar() == 22'); x1 = x2 + x3 + g1; verify(x1 == 40, '206: x1 == 40'); g1 = x3 + g2; verify(g1 == 93, '207: g1 == 207'); x1 = 5; verify(x1++ == 5, '208: x1++ == 5'); verify(x1 == 6, '209: x1 == 6'); verify(++x1 == 7, '210: ++x1 == 7'); x1 += 3; verify(x1 == 10, '211: x1 == 10'); x1 -= 6; verify(x1 == 4, '212: x1 == 4'); x1 *= 3; verify(x1 == 12, '213: x1 == 12'); x1 /= 4; verify(x1 == 3, '214: x1 == 3'); x1 = x2 = x3; verify(x2 == 14, '215: x2 == 14'); verify(x1 == 14, '216: x1 == 14'); print '217: Ending test_variables';}print '008: parsed test_variables()';/* * Test logical AND and OR operators and short-circuit evaluation. */define test_logicals(){ local x; print '300: Beginning test_logicals'; if (2 && 3) { print '301: if (2 && 3)'; } else { print '**** if (2 && 3)'; ++err; } if (2 && 0) { print '**** if (2 && 0)'; ++err; } else { print '302: if (2 && 0)'; } if (0 && 2) { print '**** if (0 && 2)'; ++err; } else { print '303: if (0 && 2)'; } if (0 && 0) { print '**** if (0 && 0)'; ++err; } else { print '304: if (0 && 0)'; } if (2 || 0) { print '305: if (2 || 0)'; } else { print '**** if (2 || 0)'; ++err; } if (0 || 2) { print '306: if (0 || 2)'; } else { print '**** if (0 || 2)'; ++err; } if (0 || 0) { print '**** if (0 || 0)'; ++err; } else { print '307: if (0 || 0)'; } x = 2 || 3; verify(x == 2, '308: (2 || 3) == 2'); x = 2 || 0; verify(x == 2, '309: (2 || 0) == 2'); x = 0 || 3; verify(x == 3, '310: (0 || 3) == 3'); x = 0 || 0; verify(x == 0, '311: (0 || 0) == 0'); x = 2 && 3; verify(x == 3, '312: (2 && 3) == 3'); x = 2 && 0; verify(x == 0, '313: (2 && 0) == 0'); x = 0 && 3; verify(x == 0, '314: (0 && 3) == 0'); x = 2 || error('2 || error()'); x = 0 && error('0 && error()'); print '315: Ending test_logicals';}print '009: parsed test_logicals()';/* * Test simple arithmetic operations and expressions. */define test_arithmetic(){ print '400: Beginning test_arithmetic'; verify(3+4==7, '401: 3 + 4 == 7'); verify(4-1==3, '402: 4 - 1 == 3'); verify(2*3==6, '403: 2 * 3 == 6'); verify(8/4==2, '404: 8 / 4 == 2'); verify(2^3==8, '405: 2 ^ 3 == 8'); verify(9-4-2==3, '406: 9-4-2 == 3'); verify(9-4+2==7, '407: 9-4+2 == 6'); verify(-5+2==-3, '408: -5+2 == -3'); verify(2*3+1==7, '409: 2*3+1 == 7'); verify(1+2*3==7, '410: 1+2*3 == 7'); verify((1+2)*3==9, '411: (1+2)*3 == 9'); verify(2*(3+1)==8, '412: 2*(3+1) == 8'); verify(9-(2+3)==4, '413: 9-(2+3) == 4'); verify(9+(2-3)==8, '414: 9+(2-3) == 8'); verify((2+3)*(4+5)==45, '415: (2+3)*(4+5) == 45'); verify(10/(2+3)==2, '416: 10/(2+3) == 2'); verify(12/3+4==8, '417: 12/3+4 == 8'); verify(6+12/3==10, '418: 6+12/3 == 10'); verify(2+3==1+4, '419: 2+3 == 1+4'); verify(-(2+3)==-5, '420: -(2+3) == -5'); verify(7&18==2, '421: 7&18 == 2'); verify(3|17==19, '422: 3|17 == 19'); verify(2&3|1==3, '423: 2&3|1 == 3'); verify(2&(3|1)==2, '424: 2&(3|1) == 2'); verify(3<<4==48, '425: 3<<4 == 48'); verify(5>>1==2, '426: 5>>1 == 2'); verify(3<<-1==1, '427: 3<<-1 == 1'); verify(5>>-2==20, '428: 5>>-2 == 20'); verify(1<<2<<3==65536, '429: 1<<2<<3 == 65536'); verify((1<<2)<<3==32, '430: (1<<2)<<3 == 32'); verify(2^3^2==512, '431: 2^3^2 == 512'); verify((2^3)^2==64,'432: (2^3)^2 == 64'); verify(4//3==1, '433: 4//3==1'); verify(4//-3==-1, '434: 4//-3==-1'); verify(0.75//-0.51==-1, '435: 0.75//-0.51==-1'); verify(0.75//-0.50==-1, '436: 0.75//-0.50==-1'); verify(0.75//-0.49==-1, '437: 0.75//-0.49==-1'); verify((3/4)//(-1/4)==-3, '438: (3/4)//(-1/4)==-3'); verify(7%3==1, '439: 7%3==1'); verify(0-.5==-.5, '440: 0-.5==-.5'); verify(0^0 == 1, '441: 0^0 == 1'); verify(0^1 == 0, '442: 0^1 == 0'); verify(1^0 == 1, '443: 1^0 == 1'); verify(1^1 == 1, '444: 1^1 == 1'); verify(1/(.8+.8i)==.625-.625i, '445: 1/(.8+.8i)==.625-.625i'); verify((.6+.8i)*(3.6-4.8i)==6, '446: (.6+.8i)*(3.6-4.8i)==6'); print '447: Ending test_arithmetic';}print '010: parsed test_arithmetic()';/* * Test string constants and comparisons */define test_strings(){ local x, y, z; print '500: Beginning test_strings'; x = 'string'; y = "string"; z = x; verify(z == "string", '501: z == "string"'); verify(z != "foo", '502: z != "foo"'); verify(z != 3, '503: z != 3'); verify('' == "", '504: \'\' == ""'); verify("a" == "a", '505: "a" == "a"'); verify("c" != "d", '506: "c" != "d"'); verify("" != "a", '507: "" != "a"'); verify("rs" < "rt", '508: "rs" < "rt"'); verify("rs" < "ss", '509: "rs < "ss"'); verify("rs" <= "rs", '510: "rs" <= "rs"'); verify("rs" <= "tu", '511: "rs" <= "tu"'); verify("rs" > "cd", '512: "rs" > "cd"'); verify("rs" >= "rs", '513: "rs" >= "rs"'); verify("rs" >= "cd", '514: "rs" >= "cd"'); verify("abc" > "ab", '515: "abc" > "ab"'); print '516: Ending test_strings';}print '011: parsed test_strings()';/* * Do multiplication and division on three numbers in various ways * and verify the results agree. */define muldivcheck(a, b, c, str){ local abc, acb, bac, bca, cab, cba; abc = (a * b) * c; acb = (a * c) * b; bac = (b * a) * c; bca = (b * c) * a; cab = (c * a) * b; cba = (c * b) * a; if (abc != acb) {print '**** abc != acb:', str; ++err;} if (acb != bac) {print '**** acb != bac:', str; ++err;} if (bac != bca) {print '**** bac != bca:', str; ++err;} if (bca != cab) {print '**** bca != cab:', str; ++err;} if (cab != cba) {print '**** cab != cba:', str; ++err;} if (abc/a != b*c) {print '**** abc/a != bc:', str; ++err;} if (abc/b != a*c) {print '**** abc/b != ac:', str; ++err;} if (abc/c != a*b) {print '**** abc/c != ab:', str; ++err;} print str;}print '012: parsed muldivcheck(a, b, c, str)';/* * Use the identity for squaring the sum of two squares to check * multiplication and squaring. */define squarecheck(a, b, str){ local a2, b2, tab, apb, apb2, t; a2 = a^2; b2 = b^2; tab = a * b * 2; apb = a + b; apb2 = apb^2; if (a2 != a*a) {print '**** a^2 != a*a:', str; ++err;} if (b2 != b*b) {print '**** b^2 != b*b:', str; ++err;} if (apb2 != apb*apb) { print '**** (a+b)^2 != (a+b)*(a+b):', str; ++err; } if (a2+tab+b2 != apb2) { print '**** (a+b)^2 != a^2 + 2ab + b^2:', str; ++err; } if (a2/a != a) {print '**** a^2/a != a:', str; ++err;} if (b2/b != b) {print '**** b^2/b != b:', str; ++err;} if (apb2/apb != apb) {print '**** (a+b)^2/(a+b) != a+b:', str; ++err;} if (a2*b2 != (a*b)^2) {print '**** a^2*b^2 != (ab)^2:', str; ++err;} print str;}print '013: parsed squarecheck(a, b, str)';/* * Use the raising of numbers to large powers to check multiplication * and exponentiation. */define powercheck(a, p1, p2, str){ local a1, a2, a3; a1 = (a^p1)^p2; a2 = (a^p2)^p1; a3 = a^(p1*p2); if (a1 != a2) {print '**** (a^p1)^p2 != (a^p2)^p1:', str; ++err;} if (a1 != a3) {print '**** (a^p1)^p2 != a^(p1*p2):', str; ++err;} print str;}print '014: parsed powercheck(a, p1, p2, str)';/* * Test fraction reductions. * Arguments MUST be relatively prime. */define fraccheck(a, b, c, str){ local ab, bc, ca, aoc, boc, aob; ab = a * b;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -