|
| 1 | +/* eslint-env mocha */ |
| 2 | +const { expect } = require('chai') |
| 3 | +const { validateRecords } = require('./expenseValidation') |
| 4 | + |
| 5 | +/** |
| 6 | + * Sum all the values in an array |
| 7 | + */ |
| 8 | +const arrSum = (arr) => arr.reduce((x, y) => x + y, 0) |
| 9 | +/** |
| 10 | + * Multiply all the values in an array |
| 11 | + */ |
| 12 | +const arrMult = (arr) => arr.reduce((x, y) => x * y, 1) |
| 13 | +const testData = [ |
| 14 | + 1721, |
| 15 | + 979, |
| 16 | + 366, |
| 17 | + 299, |
| 18 | + 675, |
| 19 | + 1456 |
| 20 | +] |
| 21 | + |
| 22 | +describe('--- 2020 Day 1: Report Repair ---', () => { |
| 23 | + describe('Part 1', () => { |
| 24 | + describe('validateRecords()', () => { |
| 25 | + it('it finds the two records that sum to 2020', () => { |
| 26 | + const expected = [1721, 299] |
| 27 | + const results = validateRecords(testData, undefined, 2) |
| 28 | + // Should be 2 results |
| 29 | + expect(results.length).to.equal(2) |
| 30 | + // Result order is unnecessary, but all expected hould be in the result set |
| 31 | + expected.forEach(result => { |
| 32 | + expect(testData.indexOf(result)).to.be.greaterThan(-1) |
| 33 | + }) |
| 34 | + // Results add up to the checksum |
| 35 | + expect(arrSum(results)).to.equal(2020) |
| 36 | + // Confirm the multiplied total |
| 37 | + expect(arrMult(results)).to.equal(514579) |
| 38 | + }) |
| 39 | + it('it supports specifying an alternate checksum', () => { |
| 40 | + const expected = [testData[3], testData[5]] |
| 41 | + const checksum = arrSum(expected) |
| 42 | + const results = validateRecords(testData, checksum) |
| 43 | + // Should be 2 results |
| 44 | + expect(results.length).to.equal(2) |
| 45 | + // Result order is unnecessary, but all expected hould be in the result set |
| 46 | + expected.forEach(result => { |
| 47 | + expect(results.indexOf(result)).to.be.greaterThan(-1) |
| 48 | + }) |
| 49 | + // Results add up to the checksum |
| 50 | + expect(arrSum(results)).to.equal(checksum) |
| 51 | + }) |
| 52 | + it('Throws an error when no records provided', () => { |
| 53 | + expect(validateRecords).to.throw() |
| 54 | + }) |
| 55 | + it('Throws an error when no records match checksum', () => { |
| 56 | + expect(() => validateRecords([1, 2, 3], 100)).to.throw('Couldn\'t find a checksum match') |
| 57 | + }) |
| 58 | + }) |
| 59 | + }) |
| 60 | + describe('Part 2', () => { |
| 61 | + describe('validateRecords()', () => { |
| 62 | + it('it can find a specified number of records adding up to 2020', () => { |
| 63 | + const expected = [979, 366, 675] |
| 64 | + const results = validateRecords(testData, undefined, 3) |
| 65 | + // Should same number of results |
| 66 | + expect(results.length).to.equal(expected.length) |
| 67 | + // Result order is unnecessary, but all expected hould be in the result set |
| 68 | + expected.forEach(result => { |
| 69 | + expect(testData.indexOf(result)).to.be.greaterThan(-1) |
| 70 | + }) |
| 71 | + // Results add up to the checksum |
| 72 | + expect(arrSum(results)).to.equal(2020) |
| 73 | + // Confirm the multiplied total |
| 74 | + expect(arrMult(results)).to.equal(241861950) |
| 75 | + }) |
| 76 | + }) |
| 77 | + }) |
| 78 | +}) |
0 commit comments