my base58 encoder/decoder worked the first time and I'm scared

This commit is contained in:
2022-10-21 05:54:08 -06:00
parent 9f65987ca2
commit 3a9cd39046
4 changed files with 1196 additions and 12 deletions
+12 -6
View File
@@ -6,13 +6,19 @@
</head> </head>
<body> <body>
<h1>Base58 Encode/Decode Tests</h1>
<p>Current case: <span id="b58-current-case"></span></p>
<p>Total cases: <span id="b58-total-cases"></span></p>
<button id="b58-go">Go!</button>
<h5>Failed case log:</h5>
<pre id="b58-assershins"></pre>
<h1>Base64 Encode/Decode Tests</h1> <h1>Base64 Encode/Decode Tests</h1>
<p>Current case: <span id="b64-current-case"></span></p>
<p>Current case: <span id="b64-current-case"></span></p> <p>Total cases: <span id="b64-total-cases"></span></p>
<p>Total cases: <span id="b64-total-cases"></span></p> <button id="b64-go">Go!</button>
<button id="b64-go">Go!</button> <h5>Failed case log:</h5>
<pre id="b64-assershins"></pre>
<pre id="b64-assershins"></pre>
<script type="module" src="dist/test.js"></script> <script type="module" src="dist/test.js"></script>
</body> </body>
File diff suppressed because it is too large Load Diff
+54 -4
View File
@@ -1,6 +1,7 @@
import * as cases from './cases.js'; import * as cases from './cases.js';
import * as b64 from './jex_include/local-vanillae-0.1.0/dist/base64.js'; import * as b64 from './jex_include/local-vanillae-0.1.0/dist/base64.js';
import * as b58 from './jex_include/local-vanillae-0.1.0/dist/base58.js';
function uint8arr_eq(a: Uint8Array, b: Uint8Array) { function uint8arr_eq(a: Uint8Array, b: Uint8Array) {
// first test if the length // first test if the length
@@ -12,7 +13,7 @@ function uint8arr_eq(a: Uint8Array, b: Uint8Array) {
for (let i = 0; i < a.length; i++) { for (let i = 0; i < a.length; i++) {
// return false if a[i] != b[i] // return false if a[i] != b[i]
if (a[i] !== b[i]) { if (a[i] !== b[i]) {
return false return false;
} }
} }
@@ -27,7 +28,7 @@ function b64_tests(): void {
let b64_casen : HTMLElement = document.getElementById('b64-current-case'); let b64_casen : HTMLElement = document.getElementById('b64-current-case');
let case_n : number = 1; let case_n : number = 1;
for(let this_case of cases.cases) { for(let this_case of cases.base64) {
// i love this type error // i love this type error
// can't set the inner html to a number // can't set the inner html to a number
// but a string plus a number is totally cool // but a string plus a number is totally cool
@@ -63,10 +64,59 @@ function b64_tests(): void {
} }
} }
function b58_tests(): void {
// @ts-ignore ts can't prove to itself that the element exists
let b58_pre : HTMLElement = document.getElementById('b58-assershins');
// @ts-ignore ts can't prove to itself that the element exists
let b58_casen : HTMLElement = document.getElementById('b58-current-case');
let case_n : number = 1;
for(let this_case of cases.base58) {
// i love this type error
// can't set the inner html to a number
// but a string plus a number is totally cool
b58_casen.innerHTML = '' + case_n;
case_n++;
let {encoded, decoded} = this_case;
let my_encoded = b58.encode(decoded);
let my_decoded = b58.decode(encoded);
let encodes_correctly = (encoded === my_encoded);
let decodes_correctly = uint8arr_eq(decoded, my_decoded);
if (!encodes_correctly) {
b58_pre.innerHTML +=
'===================================\n' +
'FAILED CASE: encode\n' +
'===================================\n' +
'decoded : ' + decoded + '\n' +
'expected: ' + encoded + '\n' +
'actual : ' + my_encoded + '\n\n' ;
}
if (!decodes_correctly) {
b58_pre.innerHTML +=
'===================================\n' +
'FAILED CASE: decode\n' +
'===================================\n' +
'encoded : ' + encoded + '\n' +
'expected: ' + decoded + '\n' +
'actual : ' + my_decoded + '\n\n' ;
}
}
}
function main(): void { function main(): void {
// @ts-ignore ts can't prove to itself that the element exists // @ts-ignore ts can't prove to itself that the element exists
document.getElementById('b64-total-cases')!.innerHTML = cases.cases.length; document.getElementById('b64-total-cases')!.innerHTML = cases.base64.length;
// @ts-ignore ts can't prove to itself that the element exists // @ts-ignore ts can't prove to itself that the element exists
document.getElementById('b64-go')!.onclick = b64_tests; document.getElementById('b64-go')!.onclick = b64_tests;
// @ts-ignore ts can't prove to itself that the element exists
document.getElementById('b58-total-cases')!.innerHTML = cases.base58.length;
// @ts-ignore ts can't prove to itself that the element exists
document.getElementById('b58-go')!.onclick = b58_tests;
} }
main(); main();
+1 -1
View File
@@ -151,7 +151,7 @@ def format_case_js(case):
def format_cases_js(cases): def format_cases_js(cases):
s = '[' s = '['
for case in cases: for case in cases:
s += format_case_erl(case) s += format_case_js(case)
# each case adds ",\n " so remove that # each case adds ",\n " so remove that
# >>> "12345678"[:-3] # >>> "12345678"[:-3]
# '12345' # '12345'