From 1b44841034d358d7dfaed0e08cd71a5f4bec129e Mon Sep 17 00:00:00 2001 From: Valerie R Young Date: Fri, 9 Nov 2018 16:56:07 -0500 Subject: [PATCH] Add unit tests for treatment of infinity primatives --- test/unit/blocks_data_infinity.js | 114 +++++++++ test/unit/blocks_operators.js | 1 - test/unit/blocks_operators_infinity.js | 328 +++++++++++++++++++++++++ 3 files changed, 442 insertions(+), 1 deletion(-) create mode 100644 test/unit/blocks_data_infinity.js create mode 100644 test/unit/blocks_operators_infinity.js diff --git a/test/unit/blocks_data_infinity.js b/test/unit/blocks_data_infinity.js new file mode 100644 index 000000000..b97a3ff49 --- /dev/null +++ b/test/unit/blocks_data_infinity.js @@ -0,0 +1,114 @@ +const test = require('tap').test; +const Data = require('../../src/blocks/scratch3_data'); + +const blocks = new Data(); + +const lists = {}; +const util = { + target: { + lookupOrCreateList (id, name) { + if (!(name in lists)) { + lists[name] = {value: []}; + } + return lists[name]; + } + } +}; + +test('List with postive infinity primitive contains postive infinity', t => { + lists.list = {value: [Infinity]}; + let args = {ITEM: Infinity, LIST: {name: 'list'}}; + let contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '[Infinity] contains Infinity'); + + lists.list = {value: [Infinity]}; + args = {ITEM: 'Infinity', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '[Infinity] contains "Infinity"'); + + lists.list = {value: [Infinity]}; + args = {ITEM: 'INFINITY', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '[Infinity] contains "INFINITY"'); + + lists.list = {value: ['Infinity']}; + args = {ITEM: Infinity, LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["Infinity"] contains Infinity'); + + lists.list = {value: ['Infinity']}; + args = {ITEM: 'Infinity', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["Infinity"] contains "Infinity"'); + + lists.list = {value: ['Infinity']}; + args = {ITEM: 'INFINITY', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["Infinity"] contains "INFINITY"'); + + lists.list = {value: ['INFINITY']}; + args = {ITEM: Infinity, LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["INFINITY"] contains Infinity'); + + lists.list = {value: ['INFINITY']}; + args = {ITEM: 'Infinity', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["INFINITY"] contains "Infinity"'); + + lists.list = {value: ['INFINITY']}; + args = {ITEM: 'INFINITY', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["INFINITY"] contains "INFINITY"'); + + t.end(); +}); + +test('List with negative infinity primitive contains negative infinity', t => { + lists.list = {value: [-Infinity]}; + let args = {ITEM: -Infinity, LIST: {name: 'list'}}; + let contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '[-Infinity] contains -Infinity'); + + lists.list = {value: [-Infinity]}; + args = {ITEM: '-Infinity', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '[-Infinity] contains "-Infinity"'); + + lists.list = {value: [-Infinity]}; + args = {ITEM: '-INFINITY', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '[-Infinity] contains "-INFINITY"'); + + lists.list = {value: ['-Infinity']}; + args = {ITEM: -Infinity, LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["-Infinity"] contains -Infinity'); + + lists.list = {value: ['-Infinity']}; + args = {ITEM: '-Infinity', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["-Infinity"] contains "-Infinity"'); + + lists.list = {value: ['-Infinity']}; + args = {ITEM: '-INFINITY', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["-Infinity"] contains "-INFINITY"'); + + lists.list = {value: ['-INFINITY']}; + args = {ITEM: -Infinity, LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["-INFINITY"] contains -Infinity'); + + lists.list = {value: ['-INFINITY']}; + args = {ITEM: '-Infinity', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["-INFINITY"] contains "-Infinity"'); + + lists.list = {value: ['-INFINITY']}; + args = {ITEM: '-INFINITY', LIST: {name: 'list'}}; + contains = blocks.listContainsItem(args, util); + t.strictEqual(contains, true, '["-INFINITY"] contains "-INFINITY"'); + + t.end(); +}); diff --git a/test/unit/blocks_operators.js b/test/unit/blocks_operators.js index 6584bec67..5dbe082e9 100644 --- a/test/unit/blocks_operators.js +++ b/test/unit/blocks_operators.js @@ -28,7 +28,6 @@ test('multiply', t => { test('divide', t => { t.strictEqual(blocks.divide({NUM1: '2', NUM2: '2'}), 1); - t.strictEqual(blocks.divide({NUM1: '1', NUM2: '0'}), Infinity); // @todo t.ok(isNaN(blocks.divide({NUM1: 'foo', NUM2: 'bar'}))); // @todo t.end(); }); diff --git a/test/unit/blocks_operators_infinity.js b/test/unit/blocks_operators_infinity.js new file mode 100644 index 000000000..aff138301 --- /dev/null +++ b/test/unit/blocks_operators_infinity.js @@ -0,0 +1,328 @@ +const test = require('tap').test; +const Operators = require('../../src/blocks/scratch3_operators'); +const Data = require('../../src/blocks/scratch3_data'); + +const blocks = new Operators(null); + +test('divide: (1) / (0) = Infinity', t => { + t.strictEqual( + blocks.divide({NUM1: '1', NUM2: '0'}), Infinity, '1 / 0 = Infinity' + ); + + t.end(); +}); + +test('divide: division with Infinity', t => { + t.strictEqual( + blocks.divide({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" / 111 = Infinity' + ); + t.strictEqual( + blocks.divide({NUM1: 'INFINITY', NUM2: 222}), Infinity, '"INFINITY" / 222 = Infinity' + ); + t.strictEqual( + blocks.divide({NUM1: Infinity, NUM2: 333}), Infinity, 'Infinity / 333 = Infinity' + ); + + t.strictEqual( + blocks.divide({NUM1: 111, NUM2: 'Infinity'}), 0, '111 / "Infinity" = 0' + ); + t.strictEqual( + blocks.divide({NUM1: 222, NUM2: 'INFINITY'}), 0, '222 / "INFINITY" = 0' + ); + t.strictEqual( + blocks.divide({NUM1: 333, NUM2: Infinity}), 0, '333 / Infinity = 0' + ); + + t.strictEqual( + blocks.divide({NUM1: '-Infinity', NUM2: 111}), -Infinity, '"-Infinity" / 111 = -Infinity' + ); + t.strictEqual( + blocks.divide({NUM1: '-INFINITY', NUM2: 222}), -Infinity, '"-INFINITY" / 222 = -Infinity' + ); + t.strictEqual( + blocks.divide({NUM1: -Infinity, NUM2: 333}), -Infinity, '-Infinity / 333 = -Infinity' + ); + + t.strictEqual( + blocks.divide({NUM1: 111, NUM2: '-Infinity'}), 0, '111 / "-Infinity" = 0' + ); + t.strictEqual( + blocks.divide({NUM1: 222, NUM2: '-INFINITY'}), 0, '222 / "-INFINITY" = 0' + ); + t.strictEqual( + blocks.divide({NUM1: 333, NUM2: -Infinity}), 0, '333 / -Infinity = 0' + ); + + t.end(); +}); + +test('multiply: multiply Infinity with numbers', t => { + t.strictEqual( + blocks.multiply({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" * 111 = Infinity' + ); + t.strictEqual( + blocks.multiply({NUM1: 'INFINITY', NUM2: 222}), Infinity, '"INFINITY" * 222 = Infinity' + ); + t.strictEqual( + blocks.multiply({NUM1: Infinity, NUM2: 333}), Infinity, 'Infinity * 333 = Infinity' + ); + t.strictEqual( + blocks.multiply({NUM1: '-Infinity', NUM2: 111}), -Infinity, '"-Infinity" * 111 = -Infinity' + ); + t.strictEqual( + blocks.multiply({NUM1: '-INFINITY', NUM2: 222}), -Infinity, '"-INFINITY" * 222 = -Infinity' + ); + t.strictEqual( + blocks.multiply({NUM1: -Infinity, NUM2: 333}), -Infinity, '-Infinity * 333 = -Infinity' + ); + t.strictEqual( + blocks.multiply({NUM1: -Infinity, NUM2: Infinity}), -Infinity, '-Infinity * Infinity = -Infinity' + ); + t.strictEqual( + Number.isNaN(blocks.multiply({NUM1: Infinity, NUM2: 0})), true, 'Infinity * 0 = NaN' + ); + + t.end(); +}); + +test('add: add Infinity to a number', t => { + + t.strictEqual( + blocks.add({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" + 111 = Infinity' + ); + t.strictEqual( + blocks.add({NUM1: 'INFINITY', NUM2: 222}), Infinity, '"INFINITY" + 222 = Infinity' + ); + t.strictEqual( + blocks.add({NUM1: Infinity, NUM2: 333}), Infinity, 'Infinity + 333 = Infinity' + ); + t.strictEqual( + blocks.add({NUM1: '-Infinity', NUM2: 111}), -Infinity, '"-Infinity" + 111 = -Infinity' + ); + t.strictEqual( + blocks.add({NUM1: '-INFINITY', NUM2: 222}), -Infinity, '"-INFINITY" + 222 = -Infinity' + ); + t.strictEqual( + blocks.add({NUM1: -Infinity, NUM2: 333}), -Infinity, '-Infinity + 333 = -Infinity' + ); + t.strictEqual( + Number.isNaN(blocks.add({NUM1: -Infinity, NUM2: Infinity})), true, '-Infinity + Infinity = NaN' + ); + + t.end(); +}); + +test('subtract: subtract Infinity with a number', t => { + + t.strictEqual( + blocks.subtract({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" - 111 = Infinity' + ); + t.strictEqual( + blocks.subtract({NUM1: 'INFINITY', NUM2: 222}), Infinity, '"INFINITY" - 222 = Infinity' + ); + t.strictEqual( + blocks.subtract({NUM1: Infinity, NUM2: 333}), Infinity, 'Infinity - 333 = Infinity' + ); + t.strictEqual( + blocks.subtract({NUM1: 111, NUM2: 'Infinity'}), -Infinity, '111 - "Infinity" = -Infinity' + ); + t.strictEqual( + blocks.subtract({NUM1: 222, NUM2: 'INFINITY'}), -Infinity, '222 - "INFINITY" = -Infinity' + ); + t.strictEqual( + blocks.subtract({NUM1: 333, NUM2: Infinity}), -Infinity, '333 - Infinity = -Infinity' + ); + t.strictEqual( + Number.isNaN(blocks.subtract({NUM1: Infinity, NUM2: Infinity})), true, 'Infinity - Infinity = NaN' + ); + + t.end(); +}); + +test('equals: compare string infinity and numeric Infinity', t => { + + t.strictEqual( + blocks.equals({OPERAND1: 'Infinity', OPERAND2: 'INFINITY'}), true, '"Infinity" = "INFINITY"' + ); + t.strictEqual( + blocks.equals({OPERAND1: 'INFINITY', OPERAND2: 'Infinity'}), true, '"INFINITY" = "Infinity"' + ); + t.strictEqual( + blocks.equals({OPERAND1: 'Infinity', OPERAND2: 'Infinity'}), true, '"Infinity" = "Infinity"' + ); + t.strictEqual( + blocks.equals({OPERAND1: 'INFINITY', OPERAND2: 'INFINITY'}), true, '"INFINITY" = "INFINITY"' + ); + t.strictEqual( + blocks.equals({OPERAND1: 'INFINITY', OPERAND2: 'infinity'}), true, '"INFINITY" = "infinity"' + ); + + t.strictEqual( + blocks.equals({OPERAND1: Infinity, OPERAND2: Infinity}), true, 'Infinity = Infinity' + ); + t.strictEqual( + blocks.equals({OPERAND1: 'Infinity', OPERAND2: Infinity}), true, '"Infinity" = Infinity' + ); + t.strictEqual( + blocks.equals({OPERAND1: 'INFINITY', OPERAND2: Infinity}), true, '"INFINITY" = Infinity' + ); + t.strictEqual( + blocks.equals({OPERAND1: Infinity, OPERAND2: 'Infinity'}), true, 'Infinity = "Infinity"' + ); + t.strictEqual( + blocks.equals({OPERAND1: Infinity, OPERAND2: 'INFINITY'}), true, 'Infinity = "INFINITY' + ); + + t.end(); +}); + +test('equals: compare string negative infinity and numeric negative Infinity', t => { + + t.strictEqual( + blocks.equals({OPERAND1: '-Infinity', OPERAND2: '-INFINITY'}), true, '"-Infinity" = "-INFINITY"' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-INFINITY', OPERAND2: '-Infinity'}), true, '"-INFINITY" = "-Infinity"' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-Infinity', OPERAND2: '-Infinity'}), true, '"-Infinity" = "-Infinity"' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-INFINITY', OPERAND2: '-INFINITY'}), true, '"-INFINITY" = "-INFINITY"' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-INFINITY', OPERAND2: '-infinity'}), true, '"-INFINITY" = "-infinity"' + ); + + t.strictEqual( + blocks.equals({OPERAND1: -Infinity, OPERAND2: -Infinity}), true, '-Infinity = -Infinity' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-Infinity', OPERAND2: -Infinity}), true, '"-Infinity" = -Infinity' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-INFINITY', OPERAND2: -Infinity}), true, '"-INFINITY" = -Infinity' + ); + t.strictEqual( + blocks.equals({OPERAND1: -Infinity, OPERAND2: '-Infinity'}), true, '-Infinity = "-Infinity"' + ); + t.strictEqual( + blocks.equals({OPERAND1: -Infinity, OPERAND2: '-INFINITY'}), true, '-Infinity = "-INFINITY' + ); + + t.end(); +}); + + +test('equals: compare negative to postive string and numeric Infinity', t => { + t.strictEqual( + blocks.equals({OPERAND1: '-Infinity', OPERAND2: 'Infinity'}), false, '"-Infinity" != "Infinity"' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-Infinity', OPERAND2: 'INFINITY'}), false, '"-infinity" != "INFINITY"' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-INFINITY', OPERAND2: 'Infinity'}), false, '"-INFINITY" != "Infinity"' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-INFINITY', OPERAND2: 'INFINITY'}), false, '"-INFINITY" != "INFINITY"' + ); + + t.strictEqual( + blocks.equals({OPERAND1: '-Infinity', OPERAND2: Infinity}), false, '"-Infinity" != Infinity' + ); + t.strictEqual( + blocks.equals({OPERAND1: '-INFINITY', OPERAND2: Infinity}), false, '"-INFINITY" != Infinity' + ); + t.strictEqual( + blocks.equals({OPERAND1: 'Infinity', OPERAND2: -Infinity}), false, '"Infinity" != -Infinity' + ); + t.strictEqual( + blocks.equals({OPERAND1: 'INFINITY', OPERAND2: -Infinity}), false, '"INFINITY" != -Infinity' + ); + + t.strictEqual( + blocks.equals({OPERAND1: Infinity, OPERAND2: -Infinity}), false, 'Infinity != -Infinity' + ); + + t.end(); +}); + +test('less than: compare string infinity and numeric Infinity', t => { + + t.strictEqual( + blocks.lt({OPERAND1: 'Infinity', OPERAND2: 'INFINITY'}), false, '"Infinity" !< "INFINITY"' + ); + t.strictEqual( + blocks.lt({OPERAND1: 'INFINITY', OPERAND2: Infinity}), false, '"INFINITY" !< "Infinity"' + ); + + t.strictEqual( + blocks.lt({OPERAND1: '-INFINITY', OPERAND2: 'INFINITY'}), true, '"-Infinity" < "INFINITY"' + ); + t.strictEqual( + blocks.lt({OPERAND1: -Infinity, OPERAND2: 'INFINITY'}), true, '-Infinity < "INFINITY"' + ); + + + t.strictEqual( + blocks.lt({OPERAND1: 'Infinity', OPERAND2: 111}), false, '"Infinity" !< 111' + ); + t.strictEqual( + blocks.lt({OPERAND1: 'INFINITY', OPERAND2: 222}), false, '"INFINITY" !< 222' + ); + t.strictEqual( + blocks.lt({OPERAND1: Infinity, OPERAND2: 333}), false, 'Infinity !< 333' + ); + + t.strictEqual( + blocks.lt({OPERAND1: 111, OPERAND2: 'Infinity'}), true, '111 < "Infinity"' + ); + t.strictEqual( + blocks.lt({OPERAND1: 222, OPERAND2: 'INFINITY'}), true, '222 < "INFINITY"' + ); + t.strictEqual( + blocks.lt({OPERAND1: 333, OPERAND2: Infinity}), true, '333 < Infinity' + ); + + t.end(); +}); + +test('more than: compare string infinity and numeric Infinity', t => { + + t.strictEqual( + blocks.gt({OPERAND1: 'Infinity', OPERAND2: 'INFINITY'}), false, '"Infinity" !> "INFINITY"' + ); + t.strictEqual( + blocks.gt({OPERAND1: 'INFINITY', OPERAND2: Infinity}), false, '"INFINITY" !> "Infinity"' + ); + + t.strictEqual( + blocks.gt({OPERAND1: 'INFINITY', OPERAND2: '-INFINITY'}), true, '"Infinity" < "-INFINITY"' + ); + t.strictEqual( + blocks.gt({OPERAND1: Infinity, OPERAND2: '-INFINITY'}), true, 'Infinity < "-INFINITY"' + ); + + t.strictEqual( + blocks.gt({OPERAND1: 'Infinity', OPERAND2: 111}), true, '"Infinity" > 111' + ); + t.strictEqual( + blocks.gt({OPERAND1: 'INFINITY', OPERAND2: 222}), true, '"INFINITY" > 222' + ); + t.strictEqual( + blocks.gt({OPERAND1: Infinity, OPERAND2: 333}), true, 'Infinity > 333' + ); + + t.strictEqual( + blocks.gt({OPERAND1: 111, OPERAND2: 'Infinity'}), false, '111 !> "Infinity"' + ); + t.strictEqual( + blocks.gt({OPERAND1: 222, OPERAND2: 'INFINITY'}), false, '222 !> "INFINITY"' + ); + t.strictEqual( + blocks.gt({OPERAND1: 333, OPERAND2: Infinity}), false, '333 !> Infinity' + ); + + t.end(); +});