Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions exercises/practice/collatz-conjecture/.meta/example.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ ALTER TABLE collatz
ADD current INTEGER;

UPDATE collatz
SET steps = 0;
SET
steps = 0;

PRAGMA recursive_triggers = ON;

CREATE TRIGGER IF NOT EXISTS update_collatz
AFTER UPDATE
ON collatz
WHEN NEW.current != 1
BEGIN
UPDATE collatz
SET current = IIF(NEW.current % 2 == 0, NEW.current / 2, NEW.current * 3 + 1),
steps = NEW.steps + 1
WHERE current = NEW.current;
CREATE TRIGGER IF NOT EXISTS update_collatz AFTER
UPDATE ON collatz WHEN NEW.current != 1 BEGIN
UPDATE collatz
SET
current = IIF(
NEW.current % 2 == 0,
NEW.current / 2,
NEW.current * 3 + 1
),
steps = NEW.steps + 1
WHERE
current = NEW.current;

END;

UPDATE collatz
SET current = number;
SET
current = number;
54 changes: 39 additions & 15 deletions exercises/practice/collatz-conjecture/collatz-conjecture_test.sql
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
-- Setup test table and read in student solution:
.read ./test_setup.sql

-- Test cases:
-- Note: the strings below may contain literal tab, newline, carriage returns.

INSERT INTO tests (name, uuid, number, expected)
VALUES
('zero steps for one', '540a3d51-e7a6-47a5-92a3-4ad1838f0bfd',
1, 0),
('divide if even', '3d76a0a6-ea84-444a-821a-f7857c2c1859',
16, 4),
('even and odd steps', '754dea81-123c-429e-b8bc-db20b05a87b9',
12, 9),
('large number of even and odd steps', 'ecfd0210-6f85-44f6-8280-f65534892ff6',
1000000, 152);
INSERT INTO
tests (name, uuid, number, expected)
VALUES
(
'zero steps for one',
'540a3d51-e7a6-47a5-92a3-4ad1838f0bfd',
1,
0
),
(
'divide if even',
'3d76a0a6-ea84-444a-821a-f7857c2c1859',
16,
4
),
(
'even and odd steps',
'754dea81-123c-429e-b8bc-db20b05a87b9',
12,
9
),
(
'large number of even and odd steps',
'ecfd0210-6f85-44f6-8280-f65534892ff6',
1000000,
152
);

-- Comparison of user input and the tests updates the status for each test:
UPDATE tests
SET status = 'pass'
FROM (SELECT number, steps FROM collatz) AS actual
WHERE (actual.number, actual.steps) = (tests.number, tests.expected);
SET
status = 'pass'
FROM
(
SELECT
number,
steps
FROM
collatz
) AS actual
WHERE
(actual.number, actual.steps) = (tests.number, tests.expected);

-- Write results and debug info:
.read ./test_reporter.sql
7 changes: 2 additions & 5 deletions exercises/practice/collatz-conjecture/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
DROP TABLE IF EXISTS collatz;
CREATE TABLE "collatz" (
"number" INTEGER,
"steps" INTEGER
);

-- Note: the CSV file contain literal tab, newline, carriage returns.
CREATE TABLE "collatz" ("number" INTEGER, "steps" INTEGER);

-- Note: the CSV file contain literal tab, newline, carriage returns.
.mode csv
.import ./data.csv collatz
41 changes: 30 additions & 11 deletions exercises/practice/collatz-conjecture/test_reporter.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = (
'Result for ' || tests.number
|| ' is <' || COALESCE(actual.steps, 'NULL')
|| '> but should be <' || tests.expected || '>'
)
FROM (SELECT number, steps FROM collatz) AS actual
WHERE actual.number = tests.number AND tests.status = 'fail';
SET
message = (
'Result for ' || tests.number || ' is <' || COALESCE(actual.steps, 'NULL') || '> but should be <' || tests.expected || '>'
)
FROM
(
SELECT
number,
steps
FROM
collatz
) AS actual
WHERE
actual.number = tests.number
AND tests.status = 'fail';

-- Save results to ./output.json (needed by the online test-runner)
.mode json
.once './output.json'
SELECT name, status, message, output, test_code, task_id
FROM tests;
SELECT
name,
status,
message,
output,
test_code,
task_id
FROM
tests;

-- Display test results in readable form for the student:
.mode table
SELECT name, status, message
FROM tests;
SELECT
name,
status,
message
FROM
tests;
27 changes: 13 additions & 14 deletions exercises/practice/collatz-conjecture/test_setup.sql
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
-- Create database:
.read ./create_fixture.sql

-- Read user student solution and save any output as markdown in user_output.md:
.mode markdown
.output user_output.md
.read ./collatz-conjecture.sql
.output

-- Create a clean testing environment:
DROP TABLE IF EXISTS tests;

CREATE TABLE IF NOT EXISTS tests (
-- uuid and description are taken from the test.toml file
uuid TEXT PRIMARY KEY,
name TEXT NOT NULL,
-- The following section is needed by the online test-runner
status TEXT DEFAULT 'fail',
message TEXT,
output TEXT,
test_code TEXT,
task_id INTEGER DEFAULT NULL,
-- Here are columns for the actual tests
number INT NOT NULL,
expected TEXT NOT NULL
-- uuid and description are taken from the test.toml file
uuid TEXT PRIMARY KEY,
name TEXT NOT NULL,
-- The following section is needed by the online test-runner
status TEXT DEFAULT 'fail',
message TEXT,
output TEXT,
test_code TEXT,
task_id INTEGER DEFAULT NULL,
-- Here are columns for the actual tests
number INT NOT NULL,
expected TEXT NOT NULL
);