From d8fae79fdc57327f2464b7feff81bb74c359fe7b Mon Sep 17 00:00:00 2001 From: Alex Kulcsar <38046796+alex-kulcsar@users.noreply.github.com> Date: Mon, 1 Jul 2024 16:45:45 -0400 Subject: [PATCH] More stats tests. --- main.ts | 4 ++ pxt.json | 5 +- size.ts | 77 ++++++++++++++++++++ stats.ts | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 295 insertions(+), 2 deletions(-) create mode 100644 stats.ts diff --git a/main.ts b/main.ts index 94afa1b..96bc7ab 100644 --- a/main.ts +++ b/main.ts @@ -24,6 +24,10 @@ if (!DeleteTests.run()) { allPassed = false } +if (!StatsTests.run()) { + allPassed = false +} + // Show summary. if (allPassed) { game.splash("All tests passed!") diff --git a/pxt.json b/pxt.json index ddb8b0a..79b4f21 100644 --- a/pxt.json +++ b/pxt.json @@ -3,7 +3,7 @@ "version": "0.0.0", "dependencies": { "device": "*", - "pxt-fast-ternary-string-set": "github:robo-technical-group/pxt-fast-ternary-string-set#v1.2.1" + "pxt-fast-ternary-string-set": "github:robo-technical-group/pxt-fast-ternary-string-set#v1.3.1" }, "files": [ "main.ts", @@ -12,7 +12,8 @@ "clear.ts", "short-english-list.ts", "size.ts", - "delete.ts" + "delete.ts", + "stats.ts" ], "testDependencies": {}, "targetVersions": { diff --git a/size.ts b/size.ts index 7a853fc..61b4205 100644 --- a/size.ts +++ b/size.ts @@ -41,6 +41,83 @@ namespace SizeTests { game.splash("Size test 2d failed.") allPassed = false } + + // size with empty string. + test = new TernaryStringSet() + if (test.size != 0) { + game.splash("Size test 3a failed.") + allPassed = false + } + test.add("") + if (test.size != 1) { + game.splash("Size test 3b failed.") + allPassed = false + } + test.add("") + if (test.size != 1) { + game.splash("Size test 3c failed.") + allPassed = false + } + test.delete("") + if (test.size != 0) { + game.splash("Size test 3d failed.") + allPassed = false + } + test.delete("") + if (test.size != 0) { + game.splash("Size test 3e failed.") + allPassed = false + } + test.add("") + if (test.size != 1) { + game.splash("Size test 3f failed.") + allPassed = false + } + test.add("whale") + if (test.size != 2) { + game.splash("Size test 3g failed.") + allPassed = false + } + + // size accurate after addAll() of word list. + test = new TernaryStringSet() + if (test.size != 0) { + game.splash("Size test 4a failed.") + allPassed = false + } + test.addAll(ShortEnglishList.words) + let len: number = ShortEnglishList.words.length + if (test.size != len) { + game.splash("Size test 4b failed.") + allPassed = false + } + // test.balance() + if (test.size != len) { + game.splash("Size test 4c failed.") + allPassed = false + } + // test.compact() + if (test.size != len) { + game.splash("Size test 4d failed.") + allPassed = false + } + let i: number = 0 + for (const el of ShortEnglishList.words) { + test.delete(el) + i++ + if (test.size != len- i){ + game.splash(`Size test 4e failed at count ${i}.`) + allPassed = false + } + } + for (const el of ShortEnglishList.words) { + test.delete(el) + if (test.size != 0) { + game.splash(`Size test 4f failed at word ${el}.`) + allPassed = false + } + } + return allPassed } } \ No newline at end of file diff --git a/stats.ts b/stats.ts new file mode 100644 index 0000000..ecafd97 --- /dev/null +++ b/stats.ts @@ -0,0 +1,211 @@ +namespace StatsTests { + export function run(): boolean { + let test: TernaryStringSet + let stats: TernaryTreeStats + let allPassed: boolean = true + + // Trivial stats for empty set. + test = new TernaryStringSet() + stats = test.stats + if (stats.size != 0) { + game.splash("Stats test 1a failed.") + allPassed = false + } + if (stats.nodes != 0) { + game.splash("Stats test 1b failed.") + allPassed = false + } + if (stats.depth != 0) { + game.splash("Stats test 1c failed.") + allPassed = false + } + if (stats.breadth.length != 0) { + game.splash("Stats test 1d failed.") + allPassed = false + } + if (stats.minCodePoint != 0) { + game.splash("Stats test 1e failed.") + allPassed = false + } + if (stats.maxCodePoint != 0) { + game.splash("Stats test 1f failed.") + allPassed = false + } + if (stats.surrogates != 0) { + game.splash("Stats test 1g failed.") + allPassed = false + } + + // Empty string increments size but adds no nodes. + test.add("") + stats = test.stats + if (stats.size != 1) { + game.splash("Stats test 2a failed.") + allPassed = false + } + if (stats.nodes != 0) { + game.splash("Stats test 2b failed.") + allPassed = false + } + if (stats.depth != 0) { + game.splash("Stats test 2c failed.") + allPassed = false + } + if (stats.breadth.length != 0) { + game.splash("Stats test 2d failed.") + allPassed = false + } + if (stats.minCodePoint != 0) { + game.splash("Stats test 2e failed.") + allPassed = false + } + if (stats.maxCodePoint != 0) { + game.splash("Stats test 2f failed.") + allPassed = false + } + if (stats.surrogates != 0) { + game.splash("Stats test 2g failed.") + allPassed = false + } + + // Stats for singleton length 1 string. + test = new TernaryStringSet() + test.add("B") + stats = test.stats + if (stats.size != 1) { + game.splash("Stats test 3a failed.") + allPassed = false + } + if (stats.nodes != 1) { + game.splash("Stats test 3b failed.") + allPassed = false + } + if (stats.depth != 1) { + game.splash("Stats test 3c failed.") + allPassed = false + } + if (stats.breadth.length != 1) { + game.splash("Stats test 3d failed.") + allPassed = false + } + if (stats.minCodePoint != 66) { + game.splash("Stats test 3e failed.") + allPassed = false + } + if (stats.maxCodePoint != 66) { + game.splash("Stats test 3f failed.") + allPassed = false + } + if (stats.surrogates != 0) { + game.splash("Stats test 3g failed.") + allPassed = false + } + + // Stats for tree with single left child. + test = new TernaryStringSet() + test.add("B") + test.add("A") + stats = test.stats + if (stats.size != 2) { + game.splash("Stats test 4a failed.") + allPassed = false + } + if (stats.nodes != 2) { + game.splash("Stats test 4b failed.") + allPassed = false + } + if (stats.depth != 2) { + game.splash("Stats test 4c failed.") + allPassed = false + } + if (stats.breadth.length != 2) { + game.splash("Stats test 4d failed.") + allPassed = false + } + if (stats.minCodePoint != 65) { + game.splash("Stats test 4e failed.") + allPassed = false + } + if (stats.maxCodePoint != 66) { + game.splash("Stats test 4f failed.") + allPassed = false + } + if (stats.surrogates != 0) { + game.splash("Stats test 4g failed.") + allPassed = false + } + + // Stats for tree with both children. + test = new TernaryStringSet() + test.add("B") + test.add("A") + test.add("C") + stats = test.stats + if (stats.size != 3) { + game.splash("Stats test 5a failed.") + allPassed = false + } + if (stats.nodes != 3) { + game.splash("Stats test 5b failed.") + allPassed = false + } + if (stats.depth != 2) { + game.splash("Stats test 5c failed.") + allPassed = false + } + if (stats.breadth.length != 2) { + game.splash("Stats test 5d failed.") + allPassed = false + } + if (stats.minCodePoint != 65) { + game.splash("Stats test 5e failed.") + allPassed = false + } + if (stats.maxCodePoint != 67) { + game.splash("Stats test 5f failed.") + allPassed = false + } + if (stats.surrogates != 0) { + game.splash("Stats test 5g failed.") + allPassed = false + } + + // Stats for three-level tree. + test = new TernaryStringSet() + test.add("B") + test.add("A") + test.add("C") + test.add("D") + stats = test.stats + if (stats.size != 4) { + game.splash("Stats test 6a failed.") + allPassed = false + } + if (stats.nodes != 4) { + game.splash("Stats test 6b failed.") + allPassed = false + } + if (stats.depth != 3) { + game.splash("Stats test 6c failed.") + allPassed = false + } + if (stats.breadth.length != 3) { + game.splash("Stats test 6d failed.") + allPassed = false + } + if (stats.minCodePoint != 65) { + game.splash("Stats test 6e failed.") + allPassed = false + } + if (stats.maxCodePoint != 68) { + game.splash("Stats test 6f failed.") + allPassed = false + } + if (stats.surrogates != 0) { + game.splash("Stats test 6g failed.") + allPassed = false + } + + return allPassed + } +} \ No newline at end of file