From 20e54a8ecf20b3f559fc38f3dd8278ae60a26a06 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Thu, 22 Mar 2018 18:11:24 +0000 Subject: [PATCH] add tests for setOperations --- src/core/operations/SetOperations.js | 14 +- test/index.js | 1 + test/tests/operations/SetOperations.js | 298 +++++++++++++++++++++++++ 3 files changed, 309 insertions(+), 4 deletions(-) create mode 100644 test/tests/operations/SetOperations.js diff --git a/src/core/operations/SetOperations.js b/src/core/operations/SetOperations.js index c3f7caeb..dad25697 100644 --- a/src/core/operations/SetOperations.js +++ b/src/core/operations/SetOperations.js @@ -53,8 +53,8 @@ class SetOps { } let result = { - Union: this.runUnion, - Intersection: this.runIntersect, + "Union": this.runUnion, + "Intersection": this.runIntersect, "Set Difference": this.runSetDifference, "Symmetric Difference": this.runSymmetricDifference, "Cartesian Product": this.runCartesianProduct, @@ -62,7 +62,7 @@ class SetOps { }[operation] .apply(null, sets.map(s => s.split(itemDelimiter))); - // Formatting issues due to the nested characteristics of power set. + // Formatting issues due to the nested characteristics of power set. if (operation === "Power Set") { result = result.map(i => `${i}\n`).join(""); } else { @@ -155,12 +155,18 @@ class SetOps { */ runPowerSet(delimiter) { return function(a) { + + // empty array items getting picked up + a = a.filter(i => i.length); + if (!a.length) { + return []; + } + /** * * @param {*} dec */ const toBinary = (dec) => (dec >>> 0).toString(2); - const result = new Set(); const maxBinaryValue = parseInt(Number(a.map(i => "1").reduce((p, c) => p + c)), 2); const binaries = [...Array(maxBinaryValue + 1).keys()] diff --git a/test/index.js b/test/index.js index e58d7e20..4603db96 100644 --- a/test/index.js +++ b/test/index.js @@ -33,6 +33,7 @@ import "./tests/operations/OTP.js"; import "./tests/operations/Regex.js"; import "./tests/operations/StrUtils.js"; import "./tests/operations/SeqUtils.js"; +import "./tests/operations/SetOperations.js" let allTestsPassing = true; diff --git a/test/tests/operations/SetOperations.js b/test/tests/operations/SetOperations.js new file mode 100644 index 00000000..af03c95d --- /dev/null +++ b/test/tests/operations/SetOperations.js @@ -0,0 +1,298 @@ +/** + * Set Operations tests. + * + * @author d98762625 + * + * @copyright Crown Copyright 2017 + * @license Apache-2.0 + */ +import TestRegister from "../../TestRegister.js"; + +TestRegister.addTests([ + { + name: "Set Operations: Nothing", + input: "\n\n", + expectedOutput: "", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Union"], + }, + ], + }, + { + name: "Set Operations: Union", + input: "1 2 3 4 5\n\n3 4 5 6 7", + expectedOutput: "1 2 3 4 5 6 7", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Union"], + }, + ], + }, + { + name: "Set Operations: Union: invalid sample number", + input: "1 2 3 4 5\n\n3 4 5 6 7\n\n1", + expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Union"], + }, + ], + }, + { + name: "Set Operations: Union: item delimiter", + input: "1,2,3,4,5\n\n3,4,5,6,7", + expectedOutput: "1,2,3,4,5,6,7", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", ",", "Union"], + }, + ], + }, + { + name: "Set Operations: Union: sample delimiter", + input: "1 2 3 4 5whatever3 4 5 6 7", + expectedOutput: "1 2 3 4 5 6 7", + recipeConfig: [ + { + op: "Set Operations", + args: ["whatever", " ", "Union"], + }, + ], + }, + { + name: "Set Operations: Intersection", + input: "1 2 3 4 5\n\n3 4 5 6 7", + expectedOutput: "3 4 5", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Intersection"], + }, + ], + }, + { + name: "Set Operations: Intersection: only one set", + input: "1 2 3 4 5 6 7 8", + expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Intersection"], + }, + ], + }, + { + name: "Set Operations: Intersection: item delimiter", + input: "1-2-3-4-5\n\n3-4-5-6-7", + expectedOutput: "3-4-5", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", "-", "Intersection"], + }, + ], + }, + { + name: "Set Operations: Intersection: sample delimiter", + input: "1-2-3-4-5\/3-4-5-6-7", + expectedOutput: "3-4-5", + recipeConfig: [ + { + op: "Set Operations", + args: ["\/", "-", "Intersection"], + }, + ], + }, + { + name: "Set Operations: Set Difference", + input: "1 2 3 4 5\n\n3 4 5 6 7", + expectedOutput: "1 2", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Set Difference"], + }, + ], + }, + { + name: "Set Operations: Set Difference: wrong sample count", + input: "1 2 3 4 5_3_4 5 6 7", + expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + recipeConfig: [ + { + op: "Set Operations", + args: [" ", "_", "Set Difference"], + }, + ], + }, + { + name: "Set Operations: Set Difference: item delimiter", + input: "1;2;3;4;5\n\n3;4;5;6;7", + expectedOutput: "1;2", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", ";", "Set Difference"], + }, + ], + }, + { + name: "Set Operations: Set Difference: sample delimiter", + input: "1;2;3;4;5===3;4;5;6;7", + expectedOutput: "1;2", + recipeConfig: [ + { + op: "Set Operations", + args: ["===", ";", "Set Difference"], + }, + ], + }, + { + name: "Set Operations: Symmetric Difference", + input: "1 2 3 4 5\n\n3 4 5 6 7", + expectedOutput: "1 2 6 7", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Symmetric Difference"], + }, + ], + }, + { + name: "Set Operations: Symmetric Difference: wrong sample count", + input: "1 2\n\n3 4 5\n\n3 4 5 6 7", + expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Symmetric Difference"], + }, + ], + }, + { + name: "Set Operations: Symmetric Difference: item delimiter", + input: "a_b_c_d_e\n\nc_d_e_f_g", + expectedOutput: "a_b_f_g", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", "_", "Symmetric Difference"], + }, + ], + }, + { + name: "Set Operations: Symmetric Difference: sample delimiter", + input: "a_b_c_d_eAAAAAc_d_e_f_g", + expectedOutput: "a_b_f_g", + recipeConfig: [ + { + op: "Set Operations", + args: ["AAAAA", "_", "Symmetric Difference"], + }, + ], + }, + { + name: "Set Operations: Cartesian Product", + input: "1 2 3 4 5\n\na b c d e", + expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e)", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Cartesian Product"], + }, + ], + }, + { + name: "Set Operations: Cartesian Product: wrong sample count", + input: "1 2\n\n3 4 5\n\na b c d e", + expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Cartesian Product"], + }, + ], + }, + { + name: "Set Operations: Cartesian Product: too many on left", + input: "1 2 3 4 5 6\n\na b c d e", + expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e) (6,undefined)", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Cartesian Product"], + }, + ], + }, + { + name: "Set Operations: Cartesian Product: too many on right", + input: "1 2 3 4 5\n\na b c d e f", + expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e) (undefined,f)", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Cartesian Product"], + }, + ], + }, + { + name: "Set Operations: Cartesian Product: item delimiter", + input: "1-2-3-4-5\n\na-b-c-d-e", + expectedOutput: "(1,a)-(2,b)-(3,c)-(4,d)-(5,e)", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", "-", "Cartesian Product"], + }, + ], + }, + { + name: "Set Operations: Cartesian Product: sample delimiter", + input: "1 2 3 4 5_a b c d e", + expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e)", + recipeConfig: [ + { + op: "Set Operations", + args: ["_", " ", "Cartesian Product"], + }, + ], + }, + { + name: "Set Operations: Power set: nothing", + input: "", + expectedOutput: "", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Power Set"], + }, + ], + }, + { + name: "Set Operations: Power set: Too many samples", + input: "1 2 3\n\n4", + expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Power Set"], + }, + ], + }, + { + name: "Set Operations: Power set", + input: "1 2 4", + expectedOutput: "\n4\n2\n1\n2 4\n1 4\n1 2\n1 2 4\n", + recipeConfig: [ + { + op: "Set Operations", + args: ["\n\n", " ", "Power Set"], + }, + ], + }, +]);