Skip to content

Commit

Permalink
More delete() tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-kulcsar committed Jul 1, 2024
1 parent f5abe3d commit 6c97f8b
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 2 deletions.
154 changes: 154 additions & 0 deletions delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
namespace DeleteTests {
export function run(): boolean {
let test: TernaryStringSet
let allPassed: boolean = true

// delete() empty string.
test = new TernaryStringSet()
test.add("")
test.add("horse")
if (test.size != 2) {
game.splash("Delete test 1a failed.")
allPassed = false
}
if (!test.has("")) {
game.splash("Delete test 1b failed.")
allPassed = false
}
test.delete("")
if (test.size != 1) {
game.splash("Delete test 1c failed.")
allPassed = false
}
if (test.has("")) {
game.splash("Delete test 1d failed.")
allPassed = false
}

// delete() non-member.
test = new TernaryStringSet()
if (test.size != 0) {
game.splash("Delete test 2a failed.")
allPassed = false
}
test.add("dog")
if (test.size != 1) {
game.splash("Delete test 2b failed.")
allPassed = false
}
if (test.has("cat")) {
game.splash("Delete test 2c failed.")
allPassed = false
}
if (test.delete("cat")) {
game.splash("Delete test 2d failed.")
allPassed = false
}
if (test.size != 1) {
game.splash("Delete test 2e failed.")
allPassed = false
}

// delete() member
test = new TernaryStringSet()
if (test.size != 0) {
game.splash("Delete test 3a failed.")
allPassed = false
}
test.add("dog")
if (test.size != 1) {
game.splash("Delete test 3b failed.")
allPassed = false
}
if (!test.has("dog")) {
game.splash("Delete test 3c failed.")
allPassed = false
}
if (!test.delete("dog")) {
game.splash("Delete test 3d failed.")
allPassed = false
}
if (test.size != 0) {
game.splash("Delete test 3e failed.")
allPassed = false
}

// delete() returns whether element was present.
test = new TernaryStringSet()
test.addAll(ShortEnglishList.words)
ShortEnglishList.words.forEach((s: string) => {
if (!test.delete(s)) {
game.splash(`Delete test 4a failed for word ${s}.`)
allPassed = false
}
})
if (test.size != 0) {
game.splash("Delete test 4b failed.")
allPassed = false
}
if (test.delete("")) {
game.splash("Delete test 4c failed.")
allPassed = false
}
test.add("")
if (test.delete("cat")) {
game.splash("Delete test 4d failed.")
allPassed = false
}
if (!test.delete("")) {
game.splash("Delete test 4e failed.")
allPassed = false
}
if (test.delete("cat")) {
game.splash("Delete test 4f failed.")
allPassed = false
}

// delete() multiple.
test = new TernaryStringSet()
test.addAll(ShortEnglishList.words)
let size: number = test.size
let count: number = 0
let randomOrder: string[] = getShuffledWords()
for (let w of randomOrder) {
if (test.size != size) {
game.splash(`Delete test 5a failed for word ${w} count ${count}.`)
allPassed = false
}
size--
if (!test.has(w)) {
game.splash(`Delete test 5b failed for word ${w} count ${count}.`)
allPassed = false
}
if (!test.delete(w)) {
game.splash(`Delete test 5c failed for word ${w} count ${count}.`)
allPassed = false
}
if (test.has(w)) {
game.splash(`Delete test 5d failed for word ${w} count ${count}.`)
allPassed = false
}
count++
}
if (test.size != 0) {
game.splash("Delete test 5e failed.")
allPassed = false
}

return allPassed
}

function getShuffledWords(): string[] {
let shuffled: string[] = ShortEnglishList.words.slice()
let len: number = shuffled.length
for (let i: number = 0; i < len; i++) {
let swapIndex: number = randint(0, len - 1)
if (i != swapIndex) {
let temp: string = shuffled[i]
shuffled[i] = shuffled[swapIndex]
shuffled[swapIndex] = temp
}
}
return shuffled
}
}
8 changes: 8 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ if (!ClearTests.run()) {
allPassed = false
}

if (!SizeTests.run()) {
allPassed = false
}

if (!DeleteTests.run()) {
allPassed = false
}

// Show summary.
if (allPassed) {
game.splash("All tests passed!")
Expand Down
6 changes: 4 additions & 2 deletions pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"version": "0.0.0",
"dependencies": {
"device": "*",
"pxt-fast-ternary-string-set": "github:robo-technical-group/pxt-fast-ternary-string-set#v1.0.0"
"pxt-fast-ternary-string-set": "github:robo-technical-group/pxt-fast-ternary-string-set#v1.1.1"
},
"files": [
"main.ts",
"constructor.ts",
"add-has.ts",
"clear.ts",
"short-english-list.ts"
"short-english-list.ts",
"size.ts",
"delete.ts"
],
"testDependencies": {},
"targetVersions": {
Expand Down
46 changes: 46 additions & 0 deletions size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace SizeTests {
export function run(): boolean {
let test: TernaryStringSet
let allPassed: boolean = true

// size() not double counted.
test = new TernaryStringSet()
if (test.size != 0) {
game.splash("Size test 1a failed.")
allPassed = false
}
test.add("peach")
if (test.size != 1) {
game.splash("Size test 1b failed.")
allPassed
}
test.add("peach")
if (test.size != 1) {
game.splash("Size test 1c failed.")
allPassed
}

// size() not double deleted.
test = new TernaryStringSet()
if (test.size != 0) {
game.splash("Size test 2a failed.")
allPassed = false
}
test.add("peach")
if (test.size != 1) {
game.splash("Size test 2b failed.")
allPassed
}
test.delete("peach")
if (test.size != 0) {
game.splash("Size test 2c failed.")
allPassed = false
}
test.delete("peach")
if (test.size != 0) {
game.splash("Size test 2d failed.")
allPassed = false
}
return allPassed
}
}

0 comments on commit 6c97f8b

Please sign in to comment.