mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-23 09:42:53 +02:00
Move cypress & netscript tests to ./test subfolder
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
function test(name, val) {
|
||||
if (val) {
|
||||
tprint("Test " + name + ": OK");
|
||||
} else {
|
||||
tprint("Test " + name + ": FAILED");
|
||||
//A failed test prints failure to results text file
|
||||
write("tb_results.txt", "FAIL");
|
||||
}
|
||||
}
|
||||
|
||||
test("run", (args.length === 1 && args[0] === "OK"));
|
||||
|
||||
//Arithmetic
|
||||
test("arith1", 0 * 10 === 0);
|
||||
test("arith2", 0 + 1 === 1);
|
||||
test("arith3", 10 / 5 === 2);
|
||||
test("arith4", 6-3 === 3);
|
||||
test("arith5", 14 % 10 === 4);
|
||||
test("arith6", 5 === 24 / 6 + 1);
|
||||
test("arith7", 6 === 2 * 3);
|
||||
|
||||
//Logical operators
|
||||
test("logic1", true && true);
|
||||
test("logic2", !(true && false));
|
||||
test("logic3", true || false);
|
||||
test("logic4", !(false || false));
|
||||
test("logic5", 1 < 3);
|
||||
test("logic6", !(3 < 1));
|
||||
test("logic7", 5 >= 5);
|
||||
test("logic8", !(5 <= 4));
|
||||
test("logic9", true == true);
|
||||
test("logic10", true === true);
|
||||
test("logic11", !(0 != 0));
|
||||
|
||||
//Assignment
|
||||
i = 5;
|
||||
test("asgn1", i == 5);
|
||||
i = 0;
|
||||
test("asgn2", i === 0);
|
||||
test("asgn3", 10 === (i = 10));
|
||||
test("asgn4", i === 10);
|
||||
|
||||
|
||||
//Basic array functionality
|
||||
testArr = [1, 2, 3, 4, 5, "str1", "str2", "str3"];
|
||||
test("arr1", testArr[0] == 1);
|
||||
test("arr2", testArr[1] === 2);
|
||||
test("arr3", testArr[5] === "str1");
|
||||
test("arr4", testArr[7] === "str3");
|
||||
|
||||
x = 1;
|
||||
y = 2;
|
||||
z = 3;
|
||||
testArr = [];
|
||||
testArr.push(x);
|
||||
testArr.push(y);
|
||||
testArr.push(z);
|
||||
test("arr5", testArr.length === 3);
|
||||
test("arr6", 1 === testArr[0]);
|
||||
test("arr7", 3 === testArr[2]);
|
||||
|
||||
x = 10;
|
||||
y = 10;
|
||||
z = 10;
|
||||
test("arr8", testArr.toString() === "1,2,3");
|
||||
|
||||
testArr.push(4);
|
||||
testArr.push("some str");
|
||||
test("arr9", "1,2,3,4,some str" === testArr.toString());
|
||||
testArr.pop();
|
||||
test("arr10", "1,2,3,4" === testArr.toString());
|
||||
testArr.shift();
|
||||
test("arr11", "2,3,4" === testArr.toString());
|
||||
testArr.unshift(1);
|
||||
test("arr12", "1,2,3,4" === testArr.toString());
|
||||
|
||||
testArr[0] = 10;
|
||||
foo = 1;
|
||||
testArr[foo] = 11;
|
||||
foo = 2;
|
||||
testArr[foo] = 12;
|
||||
foo = 3;
|
||||
testArr[foo] = 13;
|
||||
test("arr13", "10,11,12,13" === testArr.join());
|
||||
testArr.splice(testArr.length, 2, 14, 15);
|
||||
test("arr14", testArr.length === 6);
|
||||
test("arr15", testArr.join() == "10,11,12,13,14,15");
|
||||
|
||||
//Short circuiting Logical Operators
|
||||
results = [];
|
||||
res = false || results.push("OK")
|
||||
res = (0 < -5) || results.push("OK")
|
||||
res = true && results.push("OK")
|
||||
res = (1 > 0) && results.push("OK")
|
||||
res = 5 && results.push("OK")
|
||||
|
||||
test("shortcircuit1", results.length === 5);
|
||||
for (i = 0; i < results.length; ++i) {
|
||||
test("shortcircuit" + (2 + i), results[i] === "OK");
|
||||
}
|
||||
|
||||
res = true || tprint('1');
|
||||
res = (true && true) || tprint('2');
|
||||
res = 1 > 0 || tprint('3');
|
||||
res = false && tprint('4');
|
||||
res = 1 < 0 && tprint('5');
|
||||
test("shortcircuit7", results.length === 5);
|
||||
|
||||
//Conditional Statements
|
||||
results = [];
|
||||
i = 1;
|
||||
if (i == 0) {
|
||||
results.push("FAIL");
|
||||
} else if (i == 2) {
|
||||
results.push("FAIL");
|
||||
} else if (i == 1) {
|
||||
results.push("OK");
|
||||
} else {
|
||||
results.push("FAIL");
|
||||
}
|
||||
|
||||
i = 5;
|
||||
if (i == 0) {
|
||||
results.push("FAIL");
|
||||
} else if (i == 2) {
|
||||
results.push("FAIL");
|
||||
} else if (i == 1) {
|
||||
results.push("FAIL");
|
||||
} else {
|
||||
results.push("OK");
|
||||
}
|
||||
|
||||
test("conditional1", results.length === 2);
|
||||
test("conditional2", results[0] === "OK");
|
||||
test("conditional3", results[1] === "OK");
|
||||
|
||||
run("tb_multiarray.script", 1, "OK");
|
||||
exec("tb_ports.script", "home", 1, "OK");
|
||||
|
||||
write("tb_results.txt", ",tb_basic");
|
||||
@@ -0,0 +1,4 @@
|
||||
while(true) {
|
||||
print("hi");
|
||||
sleep(5000);
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
import {test} from "tb_basic.script";
|
||||
|
||||
test("run", (args.length === 1 && args[0] === "OK"));
|
||||
|
||||
//scan
|
||||
res = scan();
|
||||
test("scan1", res.includes("iron-gym") && res.includes("foodnstuff") &&
|
||||
res.includes("sigma-cosmetics") && res.includes("joesguns") &&
|
||||
res.includes("hong-fang-tea") && res.includes("harakiri-sushi"));
|
||||
test("scan2", res.length === 6);
|
||||
res = scan("foodnstuff");
|
||||
test("scan3", res.includes("home"));
|
||||
|
||||
//hasRootAccess and nuke
|
||||
svr = "foodnstuff";
|
||||
test("hasRootAccess1", !hasRootAccess(svr));
|
||||
test("nuke", nuke(svr));
|
||||
test("hasRootAccess2", hasRootAccess(svr));
|
||||
|
||||
//sleep and Date.now();
|
||||
time = Date.now();
|
||||
sleep(10000);
|
||||
time2 = Date.now();
|
||||
test("sleep", time2 - time > 8000);
|
||||
|
||||
//hack, grow, weaken, and their effects
|
||||
FORTIFYAMOUNT = 0.002;
|
||||
WEAKENAMOUNT = 0.05;
|
||||
|
||||
playerStartingMoney = getServerMoneyAvailable("home");
|
||||
serverStartingMoney = getServerMoneyAvailable("foodnstuff");
|
||||
startingSecLvl = getServerSecurityLevel("foodnstuff");
|
||||
|
||||
res = hack(svr);
|
||||
|
||||
playerEndingMoney = getServerMoneyAvailable("home");
|
||||
serverEndingMoney = getServerMoneyAvailable("foodnstuff");
|
||||
endingSecLvl = getServerSecurityLevel("foodnstuff");
|
||||
|
||||
success = !(res == 0);
|
||||
|
||||
if (success) {
|
||||
tprint("Hack succeeded");
|
||||
test("hackplyrmoney", res === (playerEndingMoney - playerStartingMoney));
|
||||
test("hacksvrmoney", res === (serverStartingMoney - serverEndingMoney));
|
||||
//Dumb JS precision
|
||||
test("seclevel", (endingSecLvl - startingSecLvl) - FORTIFYAMOUNT < 1e2 * Number.EPSILON);
|
||||
} else {
|
||||
tprint("Hack failed");
|
||||
test("hackres", res === 0);
|
||||
test("hackplymoney", playerEndingMoney === playerStartingMoney);
|
||||
test("hacksvrmoney", serverEndingMoney === serverStartingMoney);
|
||||
test("seclevel", endingSecLvl === startingSecLvl);
|
||||
}
|
||||
|
||||
serverStartingMoney = getServerMoneyAvailable(svr);
|
||||
res = grow(svr);
|
||||
serverEndingMoney = getServerMoneyAvailable(svr);
|
||||
test("grow", serverEndingMoney > serverStartingMoney);
|
||||
test("growtest", res * serverStartingMoney - serverEndingMoney < Number.EPSILON);
|
||||
|
||||
startingSecLvl = getServerSecurityLevel(svr);
|
||||
res = weaken(svr);
|
||||
endingSecLvl = getServerSecurityLevel(svr);
|
||||
test("weaken", res === WEAKENAMOUNT);
|
||||
test("weakenres", startingSecLvl - endingSecLvl - res < 1e2 * Number.EPSILON);
|
||||
|
||||
//misc getter functions
|
||||
test("getHostname", getHostname() === "home");
|
||||
test("getHackingLevel", getHackingLevel() >= 1); //Can vary based on aug mults
|
||||
res = getHackingMultipliers();
|
||||
test("getHackingMultipliers", res.chance >= 1 && res.speed >= 1 &&
|
||||
res.money >= 1 && res.growth >= 1);
|
||||
svr = "joesguns";
|
||||
baseSecLvl = getServerBaseSecurityLevel(svr);
|
||||
minSecLvl = getServerMinSecurityLevel(svr);
|
||||
test("getServerBase/MinSecurityLevel1", minSecLvl < baseSecLvl);
|
||||
test("getServerBase/MinSecurityLevel1", minSecLvl === Math.max(1, Math.round(baseSecLvl / 3)));
|
||||
test("getServerRequiredHackingLevel", getServerRequiredHackingLevel(svr) === 10);
|
||||
test("getServerMaxMoney", getServerMaxMoney(svr) > getServerMoneyAvailable(svr)); //Can vary by BN
|
||||
test("getServerGrowth", getServerGrowth(svr) >= 1); //Can vary by BN
|
||||
test("getServerNumPortsRequired1", getServerNumPortsRequired(svr) === 0);
|
||||
test("getServerNumPortsRequired1", getServerNumPortsRequired("neo-net") === 1);
|
||||
test("getServerRam", getServerRam("home")[0] === 8 ||
|
||||
getServerRam("home")[0] === 16 ||
|
||||
getServerRam("home")[0] === 32);
|
||||
test("serverExists1", serverExists("home"));
|
||||
test("serverExists2", !serverExists("foofooofofofof"));
|
||||
test("fileExists1", fileExists("tb_start.script"));
|
||||
test("fileExists2", !fileExists("tosdifoodafgbiofdagbaod.txt"));
|
||||
|
||||
//run a misc script
|
||||
test("isRunning1", !isRunning("tb_foo.script", "home"));
|
||||
run("tb_foo.script", 1, 1, 2, 3);
|
||||
test("isRunning2", !isRunning("tb_foo.script", "home", "foo"));
|
||||
test("isRunning3", isRunning("tb_foo.script", "home", 1, 2, 3));
|
||||
|
||||
//kill
|
||||
kill("tb_foo.script", "home", "fee");
|
||||
sleep(10000);
|
||||
test("isRunning4", isRunning("tb_foo.script", "home", 1, 2, 3));
|
||||
kill("tb_foo.script", "home", 1, 2, 3);
|
||||
sleep(10000);
|
||||
test("isRunning4", !isRunning("tb_foo.script", "home", 1, 2, 3));
|
||||
|
||||
//scp
|
||||
svr = "foodnstuff";
|
||||
test("fileExists3", !fileExists("tb_remote.script", svr));
|
||||
test("fileExists4", !fileExists("tb_basic.script", svr));
|
||||
scp("tb_remote.script", "home", svr);
|
||||
scp("tb_basic.script", svr);
|
||||
test("fileExists5", fileExists("tb_remote.script", svr));
|
||||
test("fileExists6", fileExists("tb_basic.script", svr));
|
||||
|
||||
write("tb_results.txt", ",tb_functions"); //Done, pretty much
|
||||
|
||||
//exec and spawn
|
||||
exec("tb_remote.script", "foodnstuff", 1, "OK");
|
||||
spawn("tb_functions2.script", 1, "OK");
|
||||
@@ -0,0 +1,90 @@
|
||||
//Tests for multidimensional arrays
|
||||
import {test} from "tb_basic.script";
|
||||
|
||||
runSuccess = (args.length === 1 && args[0] === "OK");
|
||||
test("run", runSuccess);
|
||||
|
||||
arr = [];
|
||||
arr[0] = [];
|
||||
arr[1] = [];
|
||||
arr.push([]);
|
||||
|
||||
test("multiarr1", arr.toString() === ",,");
|
||||
test("multiarr2", arr.length === 3);
|
||||
arr[0].push(0);
|
||||
arr[0].push(0);
|
||||
arr[0].push(0);
|
||||
test("multiarr3", arr[0].length === 3);
|
||||
test("multiarr4", arr[0].toString() === "0,0,0");
|
||||
arr[1] = [0, 0, 0];
|
||||
test("multiarr5", arr.length === 3);
|
||||
test("multiarr6", arr[1].length === 3);
|
||||
test("multiarr7", arr[1].toString() === "0,0,0");
|
||||
arr.pop();
|
||||
arr.push([0,0,0]);
|
||||
test("multiarr8", arr.length === 3);
|
||||
test("multiarr9", arr[2].length === 3);
|
||||
test("multiarr10", "0,0,0,0,0,0,0,0,0" === arr.toString());
|
||||
for (r = 0; r < arr.length; ++r) {
|
||||
for (c = 0; c < arr[r].length; ++c) {
|
||||
arr[r][c] = r * 3 + c + 1;
|
||||
}
|
||||
}
|
||||
test("multiarr11", "1,2,3,4,5,6,7,8,9" === arr.toString());
|
||||
|
||||
arr = [[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0]];
|
||||
test("multiarr12", 4 === arr.length);
|
||||
for (i = 0; i < arr.length; ++i) {
|
||||
test("multiarr" + (13 + i), arr[i].length === 4);
|
||||
}
|
||||
|
||||
for (r = 0; r < arr.length; ++r) {
|
||||
for (c = 0; c < arr[r].length; ++c) {
|
||||
arr[r][c] = r * 10 + c + 1;
|
||||
}
|
||||
}
|
||||
|
||||
test("multiarr17", arr.toString() === "1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34");
|
||||
|
||||
|
||||
//3D array
|
||||
arr = [[], [], [], []];
|
||||
arr[0].push([0, 0, 0]);
|
||||
arr[0].push([0, 0, 0]);
|
||||
arr[0].push([0, 0, 0]);
|
||||
|
||||
arr[1].push([0, 0, 0]);
|
||||
arr[1].push([0, 0, 0]);
|
||||
arr[1].push([0, 0, 0]);
|
||||
|
||||
arr[2].push([0, 0, 0]);
|
||||
arr[2].push([0, 0, 0]);
|
||||
arr[2].push([0, 0, 0]);
|
||||
|
||||
arr[3].push([0, 0, 0]);
|
||||
arr[3].push([0, 0, 0]);
|
||||
arr[3].push([0, 0, 0]);
|
||||
|
||||
i = 0;
|
||||
|
||||
for (r = 0; r < arr.length; ++r) {
|
||||
for (c = 0; c < arr[r].length; ++c) {
|
||||
for (d = 0; d < arr[r][c].length; ++d) {
|
||||
arr[r][c][d] = i;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test("multiarr18", "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35" === arr.toString());
|
||||
ref = 0;
|
||||
for (r = 0; r < arr.length; ++r) {
|
||||
for (c = 0; c < arr[r].length; ++c) {
|
||||
for (d = 0; d < arr[r][c].length; ++d) {
|
||||
test("multiarr" + (19 + ref), arr[r][c][d] === ref);
|
||||
++ref;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
write("tb_results.txt", ",tb_multiarray");
|
||||
@@ -0,0 +1,73 @@
|
||||
import {test} from "tb_basic.script";
|
||||
|
||||
test("run", (args.length === 1 && args[0] === "OK"));
|
||||
|
||||
|
||||
MAXPORTS = 20;
|
||||
MAXPORTSIZE = 100;
|
||||
|
||||
for (i = 1; i <= MAXPORTS; ++i) {
|
||||
clear(i);
|
||||
}
|
||||
|
||||
//write
|
||||
for (i = 1; i <= MAXPORTS; ++i) {
|
||||
for (j = 1; j <= 5; ++j) {
|
||||
write(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 1; i <= MAXPORTS; ++i) {
|
||||
port = getPortHandle(i);
|
||||
test("write" + i, port.data.length === 5);
|
||||
}
|
||||
|
||||
//read
|
||||
for (i = 1; i <= MAXPORTS; ++i) {
|
||||
for (j = 1; j <= 2; ++j) {
|
||||
res = read(i);
|
||||
test("read-p" + i + "-" + j, res === j);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 1; i <= MAXPORTS; ++i) {
|
||||
port = getPortHandle(i); //Check that read removes elements
|
||||
test("readpops" + i, port.data.length === 3);
|
||||
}
|
||||
|
||||
//peek
|
||||
for (i = 1; i <= MAXPORTS; ++i) {
|
||||
test("peek" + i, peek(i) === 3);
|
||||
port = getPortHandle(i);
|
||||
test("peeknopop" + i, port.data.length === 3);
|
||||
}
|
||||
|
||||
//clear and empty
|
||||
for (i = 1; i <= MAXPORTS; ++i) {
|
||||
clear(i);
|
||||
port = getPortHandle(i);
|
||||
test("clear" + i, port.data.length === 0);
|
||||
test("empty" + i, port.empty());
|
||||
}
|
||||
|
||||
//Write so that the port is full (only port 1 for this)
|
||||
for (i = 0; i < MAXPORTSIZE + 1; ++i) {
|
||||
write(1, i)
|
||||
}
|
||||
|
||||
//full
|
||||
port = getPortHandle(1);
|
||||
test("full", port.full());
|
||||
test("notempty", !port.empty());
|
||||
|
||||
//tryWrite
|
||||
firstElem = peek(1);
|
||||
test("trywritefails", !port.tryWrite("foo"));
|
||||
test("trywritenochange", peek(1) === firstElem);
|
||||
read(1);
|
||||
test("trywritesucceeds", port.tryWrite("foo"));
|
||||
test("trywritewrites", port.data.pop() === "foo");
|
||||
test("notfull", !port.full());
|
||||
|
||||
write("tb_results.txt", ",tb_ports");
|
||||
run("tb_functions.script", 1, "OK");
|
||||
@@ -0,0 +1,17 @@
|
||||
import {test} from "tb_basic.script";
|
||||
|
||||
test("run", args.length === 1 && args[0] === "OK");
|
||||
|
||||
svr = "foodnstuff";
|
||||
test("getHostname", getHostname() === svr);
|
||||
//ls
|
||||
res = ls(svr);
|
||||
test("ls1", res.includes("sector-12-crime.lit"));
|
||||
test("ls2", res.includes("tb_remote.script"));
|
||||
test("ls3", res.includes("tb_basic.script"));
|
||||
test("ls4", res.length === 3);
|
||||
res = ls(svr, ".lit");
|
||||
test("ls5", res.length === 1);
|
||||
test("ls6", res.includes("sector-12-crime.lit"));
|
||||
|
||||
write("tb_results.txt", "tb_remote");
|
||||
@@ -0,0 +1,35 @@
|
||||
tprint("Beginning testbench. A soft reset MUST be performed before running this.");
|
||||
tprint("This should be run on a Bitnode between 1 and 7, with $100k+, 16GB RAM+ " +
|
||||
"and access to Singularity functions otherwise some tests may fail");
|
||||
|
||||
run("tb_basic.script", 1, "OK");
|
||||
write("tb_results.txt", "tb_start", "w");
|
||||
|
||||
while(true) {
|
||||
sleep(10000);
|
||||
res = read("tb_results.txt");
|
||||
if (res.includes("FAIL")) {
|
||||
tprint("TESTBENCH FAILED");
|
||||
killall();
|
||||
}
|
||||
if (res.includes("tb_start") && res.includes("tb_basic") &&
|
||||
res.includes("tb_ports") && res.includes("tb_multiarray") &&
|
||||
res.includes("tb_functions")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Check remote
|
||||
scp("tb_results.txt", "foodnstuff", "home");
|
||||
res = read("tb_results.txt");
|
||||
if (res.includes("FAIL")) {
|
||||
tprint("TESTBENCH FAILED");
|
||||
killall();
|
||||
}
|
||||
if (res.includes("tb_remote")) {
|
||||
tprint("TESTBENCH SUCCESS");
|
||||
} else {
|
||||
tprint("TESTBENCH FAILED");
|
||||
killall();
|
||||
}
|
||||
Reference in New Issue
Block a user