From 01678ba7ffb30d25a3e926ef9f44278f405e5d1a Mon Sep 17 00:00:00 2001 From: homerli Date: Thu, 24 Oct 2019 20:19:13 -0700 Subject: [PATCH 1/2] added unit test for Trie --- package.json | 2 +- .../Trees/Trie/total-words-in-trie/index.js | 4 +-- .../Trie/total-words-in-trie/index.test.js | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/_DataStructures_/Trees/Trie/total-words-in-trie/index.test.js diff --git a/package.json b/package.json index 5dd70a80..5d609452 100644 --- a/package.json +++ b/package.json @@ -21,4 +21,4 @@ "eslint-plugin-import": "^2.14.0", "jest": "^25.0.0" } -} +} \ No newline at end of file diff --git a/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js index 679f9cda..a5cf8b82 100644 --- a/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js +++ b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js @@ -1,6 +1,4 @@ // eslint-disable-next-line no-unused-vars -const Trie = require('../index'); - function totalWords(root) { let result = 0; if (root.isEndOfWord) { @@ -20,4 +18,4 @@ function totalWords(root) { // words.forEach(word => trie.insert(word)); // console.log(totalWords(trie.root)); -module.exports = totalWords; +module.exports = { totalWords }; diff --git a/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.test.js b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.test.js new file mode 100644 index 00000000..22a5efd7 --- /dev/null +++ b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.test.js @@ -0,0 +1,25 @@ +const trie = require('../index'); +const wordcounts = require('./index'); + +describe('Trees Total words in trie', () => { + //case with non-empty array + it("Solution for this testarray is 5", () => { + const testarray = ["one", "two", "three", "four", "five"]; + const t = new trie(); + + for (let i of testarray) { + t.insert(i); + } + expect(wordcounts.totalWords(t.root)).toEqual(testarray.length); + }) + + //case with empty array + it("Solution for this empty_array is empty", () => { + const empty_array = []; + const t = new trie(); + for (let i of empty_array) { + t.insert(i); + } + expect(wordcounts.totalWords(t.root)).toEqual(0); + }) +}) \ No newline at end of file From e64ae5db518513ddc7373ae4d3ceeb70327d3340 Mon Sep 17 00:00:00 2001 From: homerli Date: Sat, 26 Oct 2019 11:32:29 -0700 Subject: [PATCH 2/2] test: added new Trie test --- .../Trees/Trie/total-words-in-trie/index.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.test.js b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.test.js index 22a5efd7..51397490 100644 --- a/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.test.js +++ b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.test.js @@ -17,8 +17,8 @@ describe('Trees Total words in trie', () => { it("Solution for this empty_array is empty", () => { const empty_array = []; const t = new trie(); - for (let i of empty_array) { - t.insert(i); + for (let j of empty_array) { + t.insert(j); } expect(wordcounts.totalWords(t.root)).toEqual(0); })