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
31 changes: 31 additions & 0 deletions exercises/practice/allergies/.meta/test_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from lib import (
assert_eq,
assert_false,
assert_true,
d_array,
d_inline_array,
d_int,
d_string,
)


def describe(case):
(parent,) = case["parents"]
return f"{parent.rstrip(':')}: {case['description']}"


def gen_case(case):
score = d_int(case["input"]["score"])
lines = [f"scope Allergies allergies = new Allergies({score});"]
if case["property"] == "allergicTo":
call = f"allergies.allergicTo({d_string(case['input']['item'])})"
lines.append(assert_true(call) if case["expected"] else assert_false(call))
return lines

allergens = case["expected"]
render = d_array if len(allergens) > 2 else d_inline_array
expected = render(allergens, d_string)
lines.append("string[] result = allergies.list();")
lines.append(f"string[] expected = {expected};")
lines.append(assert_eq("result", "expected"))
return lines
2 changes: 1 addition & 1 deletion exercises/practice/allergies/example/allergies.d
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ private:
"pollen",
"cats"
];
}
}
76 changes: 10 additions & 66 deletions exercises/practice/allergies/source/allergies.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ unittest

static if (allTestsEnabled)
{

// testing for eggs allergy: allergic only to eggs
{
scope Allergies allergies = new Allergies(1);
Expand All @@ -55,7 +54,7 @@ unittest
assert(allergies.allergicTo("eggs"));
}

// testing for peanuts allergy: not allergic to peanuts
// testing for peanuts allergy: not allergic to anything
{
scope Allergies allergies = new Allergies(0);
assert(!allergies.allergicTo("peanuts"));
Expand Down Expand Up @@ -85,7 +84,7 @@ unittest
assert(allergies.allergicTo("peanuts"));
}

// testing for shellfish allergy: not allergic to shellfish
// testing for shellfish allergy: not allergic to anything
{
scope Allergies allergies = new Allergies(0);
assert(!allergies.allergicTo("shellfish"));
Expand Down Expand Up @@ -115,61 +114,7 @@ unittest
assert(allergies.allergicTo("shellfish"));
}

// testing for eggs allergy: allergic only to eggs
{
scope Allergies allergies = new Allergies(1);
assert(allergies.allergicTo("eggs"));
}

// testing for eggs allergy: allergic to eggs and something else
{
scope Allergies allergies = new Allergies(3);
assert(allergies.allergicTo("eggs"));
}

// testing for eggs allergy: allergic to something, but not eggs
{
scope Allergies allergies = new Allergies(2);
assert(!allergies.allergicTo("eggs"));
}

// testing for eggs allergy: allergic to everything
{
scope Allergies allergies = new Allergies(255);
assert(allergies.allergicTo("eggs"));
}

// testing for peanuts allergy: not allergic to peanuts
{
scope Allergies allergies = new Allergies(0);
assert(!allergies.allergicTo("peanuts"));
}

// testing for peanuts allergy: allergic only to peanuts
{
scope Allergies allergies = new Allergies(2);
assert(allergies.allergicTo("peanuts"));
}

// testing for peanuts allergy: allergic to peanuts and something else
{
scope Allergies allergies = new Allergies(7);
assert(allergies.allergicTo("peanuts"));
}

// testing for peanuts allergy: allergic to something, but not peanuts
{
scope Allergies allergies = new Allergies(5);
assert(!allergies.allergicTo("peanuts"));
}

// testing for peanuts allergy: allergic to everything
{
scope Allergies allergies = new Allergies(255);
assert(allergies.allergicTo("peanuts"));
}

// testing for strawberries allergy: not allergic to strawberries
// testing for strawberries allergy: not allergic to anything
{
scope Allergies allergies = new Allergies(0);
assert(!allergies.allergicTo("strawberries"));
Expand Down Expand Up @@ -199,7 +144,7 @@ unittest
assert(allergies.allergicTo("strawberries"));
}

// testing for tomatoes allergy: not allergic to tomatoes
// testing for tomatoes allergy: not allergic to anything
{
scope Allergies allergies = new Allergies(0);
assert(!allergies.allergicTo("tomatoes"));
Expand Down Expand Up @@ -229,7 +174,7 @@ unittest
assert(allergies.allergicTo("tomatoes"));
}

// testing for chocolate allergy: not allergic to chocolate
// testing for chocolate allergy: not allergic to anything
{
scope Allergies allergies = new Allergies(0);
assert(!allergies.allergicTo("chocolate"));
Expand Down Expand Up @@ -259,7 +204,7 @@ unittest
assert(allergies.allergicTo("chocolate"));
}

// testing for pollen allergy: not allergic to pollen
// testing for pollen allergy: not allergic to anything
{
scope Allergies allergies = new Allergies(0);
assert(!allergies.allergicTo("pollen"));
Expand Down Expand Up @@ -289,7 +234,7 @@ unittest
assert(allergies.allergicTo("pollen"));
}

// testing for cats allergy: not allergic to cats
// testing for cats allergy: not allergic to anything
{
scope Allergies allergies = new Allergies(0);
assert(!allergies.allergicTo("cats"));
Expand Down Expand Up @@ -376,7 +321,7 @@ unittest
"tomatoes",
"chocolate",
"pollen",
"cats"
"cats",
];
assert(result == expected);
}
Expand All @@ -393,7 +338,7 @@ unittest
"tomatoes",
"chocolate",
"pollen",
"cats"
"cats",
];
assert(result == expected);
}
Expand All @@ -409,7 +354,7 @@ unittest
"tomatoes",
"chocolate",
"pollen",
"cats"
"cats",
];
assert(result == expected);
}
Expand All @@ -422,5 +367,4 @@ unittest
assert(result == expected);
}
}

}
20 changes: 20 additions & 0 deletions exercises/practice/anagram/.meta/test_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from lib import assert_eq, d_inline_array, d_string


def describe(case):
return case["description"]


def gen_case(case):
prop = case["property"]
subject = d_string(case["input"]["subject"])
candidates = d_inline_array(case["input"]["candidates"], d_string)
expected = d_inline_array(case["expected"], d_string)
return [
f"immutable string subject = {subject};",
f"immutable string[] candidates = {candidates};",
f"string[] actual = {prop}(subject, candidates);",
f"string[] expected = {expected};",
"",
assert_eq("actual", "expected"),
]
1 change: 0 additions & 1 deletion exercises/practice/anagram/example/anagram.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ pure string[] findAnagrams(immutable string subject, immutable string[] candidat
}
return results;
}

4 changes: 1 addition & 3 deletions exercises/practice/anagram/source/anagram.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ unittest

static if (allTestsEnabled)
{

// detects two anagrams
{
immutable string subject = "solemn";
Expand Down Expand Up @@ -165,7 +164,7 @@ unittest
// words other than themselves can be anagrams
{
immutable string subject = "LISTEN";
immutable string[] candidates = ["Listen", "Silent", "LISTEN"];
immutable string[] candidates = ["LISTEN", "Silent"];
string[] actual = findAnagrams(subject, candidates);
string[] expected = ["Silent"];

Expand All @@ -183,4 +182,3 @@ unittest
}
}
}

15 changes: 15 additions & 0 deletions exercises/practice/binary-search/.meta/test_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from lib import assert_eq, assert_throws, d_inline_array, d_int, is_error


def gen_case(case):
prop = case["property"]
array = d_inline_array(case["input"]["array"])
value = d_int(case["input"]["value"])
call = f"bs.{prop}({value})"

lines = [f"BinarySearch bs = new BinarySearch({array});"]
if is_error(case["expected"]):
lines.append(assert_throws(call))
else:
lines.append(assert_eq(call, d_int(case["expected"])))
return lines
8 changes: 3 additions & 5 deletions exercises/practice/binary-search/source/binary_search.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class BinarySearch

unittest
{
const int allTestsEnabled = 0;
import std.exception : assertThrown;

immutable int allTestsEnabled = 0;

// Finds a value in an array with one element
{
Expand All @@ -25,9 +27,6 @@ unittest

static if (allTestsEnabled)
{

import std.exception : assertThrown;

// Finds a value in the middle of an array
{
BinarySearch bs = new BinarySearch([1, 3, 4, 6, 8, 9, 11]);
Expand Down Expand Up @@ -87,6 +86,5 @@ unittest
BinarySearch bs = new BinarySearch([1, 2]);
assertThrown(bs.find(0));
}

}
}
45 changes: 45 additions & 0 deletions exercises/practice/circular-buffer/.meta/test_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from lib import assert_eq, d_int

EMPTY = "Empty buffer should throw exception if popped!"
FULL = "Full buffer should throw exception if new element pushed!"

# The element type used to instantiate the template in each test case.
TYPES = {
"reading empty buffer should fail": "int",
"each item may only be read once": "short",
"full buffer can't be written to": "long",
"read position is maintained even across multiple writes": "int",
"clear frees up capacity for another write": "short",
"overwrite acts like write on non-full buffer": "long",
"overwrite replaces the oldest item remaining in buffer following a read": "int",
}


def d_item(item, kind):
return f"'{item}'" if kind == "char" else d_int(item)


def gen_operation(operation, kind):
name = operation["operation"]
if name == "clear":
return "myBuffer.clear();"
if name == "overwrite":
return f"myBuffer.forcePush({d_item(operation['item'], kind)});"
if name == "write":
call = f"myBuffer.push({d_item(operation['item'], kind)})"
if operation["should_succeed"]:
return f"{call};"
return f'assertThrown({call}, "{FULL}");'
if operation["should_succeed"]:
return assert_eq("myBuffer.pop()", d_item(operation["expected"], kind))
return f'assertThrown(myBuffer.pop(), "{EMPTY}");'


def gen_case(case):
kind = TYPES.get(case["description"], "char")
capacity = d_int(case["input"]["capacity"])
operations = case["input"]["operations"]
return [
f"auto myBuffer = new Buffer!({kind})({capacity});",
*(gen_operation(operation, kind) for operation in operations),
]
Loading
Loading