begin writing bitvec unit tests + return constants for unit tests

This commit is contained in:
ferricles 2022-07-06 20:27:04 -07:00
parent e8a9439b64
commit c6cc5bfc24
3 changed files with 107 additions and 3 deletions

View file

@ -181,6 +181,10 @@ Base64 = (function () {
throw new TypeError("BitVector must be instantiated with a Number or a B64 String"); throw new TypeError("BitVector must be instantiated with a Number or a B64 String");
} }
if (bit_vec.length == 0) {
bit_vec = [0];
}
this.length = length; this.length = length;
this.bits = new Uint32Array(bit_vec); this.bits = new Uint32Array(bit_vec);
} }
@ -335,8 +339,8 @@ Base64 = (function () {
if (typeof data === "string") { if (typeof data === "string") {
//daily reminder that shifts are modded by 32 //daily reminder that shifts are modded by 32
for (const char of data) { for (const character of data) {
char = Base64.toInt(char); let char = Base64.toInt(character);
this.bits[curr_idx] |= (char << pos); this.bits[curr_idx] |= (char << pos);
//if we go to the "next" char, update it //if we go to the "next" char, update it
@ -660,6 +664,9 @@ const getScript = url => new Promise((resolve, reject) => {
/* /*
GENERIC TEST FUNCTIONS GENERIC TEST FUNCTIONS
*/ */
const TEST_SUCCESS = 1;
const TEST_FAIL = 0;
/** The generic assert function. Fails on all "false-y" values. Useful for non-object equality checks, boolean value checks, and existence checks. /** The generic assert function. Fails on all "false-y" values. Useful for non-object equality checks, boolean value checks, and existence checks.
* *
* @param {*} arg - argument to assert. * @param {*} arg - argument to assert.
@ -669,6 +676,7 @@ GENERIC TEST FUNCTIONS
if (!arg) { if (!arg) {
throw new Error(msg ? msg : "Assert failed."); throw new Error(msg ? msg : "Assert failed.");
} }
return TEST_SUCCESS;
} }
/** Asserts object equality of the 2 parameters. For loose and strict asserts, use assert(). /** Asserts object equality of the 2 parameters. For loose and strict asserts, use assert().
@ -681,6 +689,7 @@ function assert_equals(arg1, arg2, msg) {
if (!Object.is(arg1, arg2)) { if (!Object.is(arg1, arg2)) {
throw new Error(msg ? msg : "Assert Equals failed. " + arg1 + " is not " + arg2 + "."); throw new Error(msg ? msg : "Assert Equals failed. " + arg1 + " is not " + arg2 + ".");
} }
return TEST_SUCCESS;
} }
/** Asserts object inequality of the 2 parameters. For loose and strict asserts, use assert(). /** Asserts object inequality of the 2 parameters. For loose and strict asserts, use assert().
@ -693,6 +702,7 @@ function assert_equals(arg1, arg2, msg) {
if (Object.is(arg1, arg2)) { if (Object.is(arg1, arg2)) {
throw new Error(msg ? msg : "Assert Not Equals failed. " + arg1 + " is " + arg2 + "."); throw new Error(msg ? msg : "Assert Not Equals failed. " + arg1 + " is " + arg2 + ".");
} }
return TEST_SUCCESS;
} }
/** Asserts proximity between 2 arguments. Should be used for any floating point datatype. /** Asserts proximity between 2 arguments. Should be used for any floating point datatype.
@ -706,6 +716,7 @@ function assert_near(arg1, arg2, epsilon = 1E-5, msg) {
if (Math.abs(arg1 - arg2) > epsilon) { if (Math.abs(arg1 - arg2) > epsilon) {
throw new Error(msg ? msg : "Assert Near failed. " + arg1 + " is not within " + epsilon + " of " + arg2 + "."); throw new Error(msg ? msg : "Assert Near failed. " + arg1 + " is not within " + epsilon + " of " + arg2 + ".");
} }
return TEST_SUCCESS;
} }
/** Asserts that the input argument is null. /** Asserts that the input argument is null.
@ -717,6 +728,7 @@ function assert_null(arg, msg) {
if (arg !== null) { if (arg !== null) {
throw new Error(msg ? msg : "Assert Near failed. " + arg + " is not null."); throw new Error(msg ? msg : "Assert Near failed. " + arg + " is not null.");
} }
return TEST_SUCCESS;
} }
/** Asserts that the input argument is undefined. /** Asserts that the input argument is undefined.
@ -728,6 +740,7 @@ function assert_null(arg, msg) {
if (arg !== undefined) { if (arg !== undefined) {
throw new Error(msg ? msg : "Assert Near failed. " + arg + " is not undefined."); throw new Error(msg ? msg : "Assert Near failed. " + arg + " is not undefined.");
} }
return TEST_SUCCESS;
} }
/** Asserts that there is an error when a callback function is run. /** Asserts that there is an error when a callback function is run.
@ -739,7 +752,7 @@ function assert_error(func_binding, msg) {
try { try {
func_binding(); func_binding();
} catch (err) { } catch (err) {
return; return TEST_SUCCESS;
} }
throw new Error(msg ? msg : "Function didn't throw an error."); throw new Error(msg ? msg : "Function didn't throw an error.");
} }
@ -757,3 +770,51 @@ function deepcopy(obj) {
} }
return ret; return ret;
} }
function bv_test() {
console.log("=====STARTING BITVECTOR UNIT TEST=====");
// Empty Constructor + append str
let bv = new BitVector("");
bv.append("Bc8");
assert_equals(bv.bits.length, 1);
assert_equals(bv.length, 18);
assert_equals(bv.toB64(), "Bc8");
// Empty Constructor + append num 1
bv = new BitVector("");
bv.append(10000, 18);
assert_equals(bv.bits.length, 1);
assert_equals(bv.length, 18);
assert_equals(bv.toB64(), "GS2"); //have to read backwards
// Empty Constructor + append num 1
bv = new BitVector("");
bv.append(10000, 14);
assert_equals(bv.bits.length, 1);
assert_equals(bv.length, 14);
assert_equals(bv.toB64(), "GS2"); //have to read backwards
// 1-int constructor (num)
bv = new BitVector(10000, 14);
assert_equals(bv.bits.length, 1);
assert_equals(bv.length, 14);
assert_equals(bv.toB64(), "GS2");
// 1-int constructor (str)
bv = new BitVector("abcde");
assert_equals(bv.bits.length, 1);
assert_equals(bv.length, 30);
assert_equals(bv.toB64(), "abcde");
// test constructor ignore length when data is str
bv = new BitVector("abcde", 40);
assert_equals(bv.bits.length, 1);
assert_equals(bv.length, 30);
assert_equals(bv.toB64(), "abcde");
console.log("=====FINISHED BITVECTOR UNIT TEST=====");
}
bv_test();

11
temp.py Normal file
View file

@ -0,0 +1,11 @@
import json
import sys
import os
import base64
import argparse
parser = argparse.ArgumentParser(description="Do a little trolling.")
parser.add_argument('infile', help='input file to read data from')
parser.add_argument('outfile', help='output file to dump clean data into')
args = parser.parse_args()
infile, outfile = args.infile, args.outfile

32
testing/index.html Normal file
View file

@ -0,0 +1,32 @@
<!DOCTYPE html>
<!-- This is supposed to be a testing page. Modify it as you need. -->
<html scroll-behavior="smooth">
<head>
<title>WynnBuilder Dev</title>
<link rel="icon" href="../media/icons/new/atlas64.png">
<link rel="manifest" href="manifest.json">
<meta name="viewport" content="width=device-width, initial-scale=.45, user-scalable=no">
<!-- nunito font, copying wynnbuilder, which is copying wynndata -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Nunito&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Nunito&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="../css/sq2bs.css">
</head>
<body id = "body" class = "all">
<!-- add DOM elements as desired -->
<!-- add scripts -->
<script type="text/javascript" src="../js/utils.js"></script>
</body>
</html>