This is day 2 of the Advent of Code 2017. If you want to read about the puzzles, start with Day 1. As I worked through the puzzles, I decided that I should be testing using their test sets and solving the issues that way. This lets me use the sample data, but also add in my own sets to cover strange situations.
Here are the tests that I used for each part of day 2.
Part I
This wasn’t a tough puzzle, and the test is fairly simple. I had a function that solves the puzzle with the help of input. My test just sets up the sample input in the table, tab delimited, and then calls the function to calculate the total.
EXEC tsqlt.NewTestClass @ClassName = N'tDay2' go CREATE OR ALTER PROCEDURE tDay2.[test day2 sample input] AS BEGIN --------------- -- Assemble --------------- DECLARE @expected INT 18, @actual int; EXEC tsqlt.faketable @TableName = 'Day2', @SchemaName = 'dbo'; INSERT dbo.Day2 (DataRow) VALUES ('5 1 9 5') , ('7 5 3') , ('2 4 6 8') --------------- -- Act --------------- SELECT @actual = SUM(b.diff) FROM day2 a CROSS APPLY dbo.AdventChecksum (a.DataRow) b --------------- -- Assert --------------- EXEC tSQLt.AssertEquals @Expected = @expected, @Actual = @actual, @Message = N'An incorrect calculation occurred.'; END GO EXEC tsqlt.run 'tDay2.[test day2 sample input]';
Part II
The test here just calls a different function and has different input.
CREATE OR ALTER PROCEDURE tDay2.[test day2 b sample input] AS BEGIN --------------- -- Assemble --------------- DECLARE @expected INT = 9, @actual int; EXEC tsqlt.faketable @TableName = 'Day2', @SchemaName = 'dbo'; INSERT dbo.Day2 (DataRow) VALUES ('5 9 2 8') , ('9 4 7 3') , ('3 8 6 5') --------------- -- Act --------------- SELECT @actual = SUM(b.divmatch) FROM day2 a CROSS APPLY dbo.AdventChecksum3 (a.DataRow) b --------------- -- Assert --------------- EXEC tSQLt.AssertEquals @Expected = @expected, @Actual = @actual, @Message = N'An incorrect calculation occurred.'; END GO EXEC tsqlt.run 'tDay2.[test day2 b sample input]'; GO