| 1 |
#include <tads.h> |
| 2 |
#include <bignum.h> |
| 3 |
#include <strbuf.h> |
| 4 |
|
| 5 |
main(args) |
| 6 |
{ |
| 7 |
test('A4/(A10)', 'One', 'Two', 'Three', 'Four', 'Five'); |
| 8 |
test('C/l', [0x12345678, 0x11aabbcc]); |
| 9 |
test('A10 X5 A3', 'aaaaaaaaaa', 'BBBBB'); |
| 10 |
|
| 11 |
test('c4*'); |
| 12 |
test('c4*', 1, 2); |
| 13 |
test('c4*', 1, 2, 3); |
| 14 |
test('c4*', 1, 2, 3, 4); |
| 15 |
test('c4*', 1, 2, 3, 4, 5, 6); |
| 16 |
test('c4*', []); |
| 17 |
test('c4*', [1, 2, 3]); |
| 18 |
test('c4*', [1, 2, 3, 4]); |
| 19 |
test('c4*', [1, 2, 3, 4, 5]); |
| 20 |
test('[cc]4*!'); |
| 21 |
test('[cc]4*!', [1, 2], [3], [4, 5]); |
| 22 |
test('[cc]4*!', [1, 2], [3], [4, 5], [6, 7]); |
| 23 |
test('[cc]4*!', [1, 2], [3], [4, 5], [6, 7], [8]); |
| 24 |
test('a10*', 'short'); |
| 25 |
test('a10*', '10 exactly'); |
| 26 |
test('a10*', 'a bit too long'); |
| 27 |
|
| 28 |
ovTest('c', 127, 128, 129, -127, -128, -129); |
| 29 |
ovTest('C', 127, 128, 129, 255, 256, -1); |
| 30 |
ovTest('s', 32767, 32768, -32767, -32768, -32769); |
| 31 |
ovTest('l', 0x7fffffff, 0x80000000, 3000000000.0); |
| 32 |
ovTest('L~', 0x7fffffff, -1, 2.0.raiseToPower(32)-1, 2.0.raiseToPower(32)); |
| 33 |
ovTest('q~', 0x7fffffff, 2.0.raiseToPower(63)-1, 2.0.raiseToPower(63), |
| 34 |
-(2.0.raiseToPower(63)), -(2.0.raiseToPower(63)+1)); |
| 35 |
ovTest('Q~', 0x7fffffff, 2.0.raiseToPower(63)-1, 2.0.raiseToPower(63), |
| 36 |
-(2.0.raiseToPower(63)), -(2.0.raiseToPower(63)+1), |
| 37 |
2.0.raiseToPower(64)-1, 2.0.raiseToPower(64), |
| 38 |
2.0.raiseToPower(64) + 0x12345678); |
| 39 |
ovTest('f', 1.12345e38, 3.402823e38, 3.402824e38, 4.0e39, |
| 40 |
-3.402824e38, -3.402824e38, -4.0e39); |
| 41 |
ovTest('d', 1.12345e308, 1.7976931348623158e308, |
| 42 |
1.797694e308, -1.797694e308, 4.0e309, -4.0e309); |
| 43 |
|
| 44 |
local s = 'This is a test hash message!'.sha256(); |
| 45 |
"sha256 = <<s>>\n"; |
| 46 |
"unpacked = <<binstr(String.packBytes('H*', s))>>\b"; |
| 47 |
} |
| 48 |
|
| 49 |
test(fmt, [args]) |
| 50 |
{ |
| 51 |
local s = String.packBytes(fmt, args...); |
| 52 |
"format = <<fmt.htmlify()>>, args = <<sayList(args)>>\n"; |
| 53 |
".. pack = '<<binstr(s)>>'\n"; |
| 54 |
".. pack hex = <<s.unpackBytes('H*')[1]>>\n"; |
| 55 |
".. unpack = <<sayList(s.unpackBytes(fmt))>>\b"; |
| 56 |
} |
| 57 |
|
| 58 |
ovTest(fmt, [args]) |
| 59 |
{ |
| 60 |
foreach (local arg in args) |
| 61 |
{ |
| 62 |
ovTest1(fmt, arg); |
| 63 |
ovTest1(fmt + '%', arg); |
| 64 |
"\b"; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
ovTest1(fmt, arg) |
| 69 |
{ |
| 70 |
try |
| 71 |
{ |
| 72 |
local s = String.packBytes(fmt, arg); |
| 73 |
local lst = s.unpackBytes(fmt); |
| 74 |
"pack OK: pack('<<fmt.htmlify()>>', <<arg>>) = << |
| 75 |
s.unpackBytes('H*')[1]>>\n.. unpack = << |
| 76 |
sayList(lst)>><< |
| 77 |
lst[1] != arg ? " *** value change ***\n" : "\n">>"; |
| 78 |
} |
| 79 |
catch (Exception exc) |
| 80 |
{ |
| 81 |
"pack error: pack('<<fmt.htmlify()>>', <<arg>>) = << |
| 82 |
exc.displayException()>>\n"; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
binstr(s) |
| 87 |
{ |
| 88 |
return rexReplace('[\x00-\x1f\\ ]', s, new function(m) { |
| 89 |
if (m == '\\') |
| 90 |
return '\\\\'; |
| 91 |
else if (m == '\'') |
| 92 |
return '\\\''; |
| 93 |
else if (m == ' ') |
| 94 |
return '\ '; |
| 95 |
else |
| 96 |
{ |
| 97 |
m = toString(m.toUnicode(1), 16); |
| 98 |
return '\\x<<'00'.substr(m.length() + 1)>><<m>>'; |
| 99 |
} |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
sayList(lst) |
| 104 |
{ |
| 105 |
"["; |
| 106 |
|
| 107 |
local idx = 0; |
| 108 |
foreach (local val in lst) |
| 109 |
{ |
| 110 |
if (idx++ > 0) |
| 111 |
", "; |
| 112 |
|
| 113 |
switch (dataType(val)) |
| 114 |
{ |
| 115 |
case TypeList: |
| 116 |
sayList(val); |
| 117 |
break; |
| 118 |
|
| 119 |
case TypeSString: |
| 120 |
"'<<rexReplace('[\'\\\n\t\b]', val, new function(m) { |
| 121 |
return ['\'' -> '\\\'', '\\' -> '\\\\', |
| 122 |
'\n' -> '\\n', '\t' -> '\\t', '\b' -> '\\b', |
| 123 |
* -> '\\?'][m]; |
| 124 |
})>>'"; |
| 125 |
break; |
| 126 |
|
| 127 |
case TypeInt: |
| 128 |
"<<val>> (0x<<toString(val, 16)>>)"; |
| 129 |
break; |
| 130 |
|
| 131 |
case TypeObject: |
| 132 |
if (val.ofKind(BigNumber)) |
| 133 |
{ |
| 134 |
"<<val>> (bignum)"; |
| 135 |
if ((val.numType() & NumTypeNum) != 0 |
| 136 |
&& val.getWhole() == val |
| 137 |
&& val.getScale() <= 20) |
| 138 |
" (0x<<bignumToHex(val)>>)"; |
| 139 |
} |
| 140 |
else if (val.ofKind(ByteArray)) |
| 141 |
{ |
| 142 |
"<<val.mapToString(new CharacterSet('latin1'))>>"; |
| 143 |
} |
| 144 |
else |
| 145 |
"(object)"; |
| 146 |
break; |
| 147 |
|
| 148 |
default: |
| 149 |
"<<val>>"; |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
"]"; |
| 155 |
} |
| 156 |
|
| 157 |
bignumToHex(val) |
| 158 |
{ |
| 159 |
local s = new StringBuffer(30); |
| 160 |
local neg = (val < 0); |
| 161 |
if (neg) |
| 162 |
val = -val; |
| 163 |
|
| 164 |
while (val > 0) |
| 165 |
{ |
| 166 |
local q = val.divideBy(16); |
| 167 |
s.append(toString(toInteger(q[2]), 16)); |
| 168 |
val = q[1]; |
| 169 |
} |
| 170 |
|
| 171 |
if (s.length() == 0) |
| 172 |
s.append('0'); |
| 173 |
if (neg) |
| 174 |
s.append('-'); |
| 175 |
|
| 176 |
for (local i = 1, local j = s.length() ; i < j ; ++i, --j) |
| 177 |
{ |
| 178 |
local tmp = s[i]; |
| 179 |
s[i] = s[j]; |
| 180 |
s[j] = tmp; |
| 181 |
} |
| 182 |
|
| 183 |
return toString(s); |
| 184 |
} |