Skip to content

Commit 97c34fc

Browse files
inventory-management: generate tests (#424)
1 parent f1c5a9b commit 97c34fc

2 files changed

Lines changed: 202 additions & 1 deletion

File tree

exercises/concept/inventory-management/inventory_management_test.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,71 @@ void test_weight_of_small_box(void) {
1919
}
2020

2121
void test_weight_of_medium_box(void) {
22+
TEST_IGNORE();
2223
TEST_ASSERT_EQUAL_UINT32_MESSAGE(6330, get_box_weight(15, 242, 1100, 2), "The function was called with arguments: 15, 242, 1100, 2.");
2324
}
2425

2526
void test_weight_of_large_box(void) {
27+
TEST_IGNORE();
2628
TEST_ASSERT_EQUAL_UINT32_MESSAGE(136993, get_box_weight(12, 5600, 133, 521),
2729
"The function was called with arguments: 12, 5600, 133, 521.");
2830
}
2931

3032
void test_max_stack_for_small_box(void) {
33+
TEST_IGNORE();
3134
TEST_ASSERT_EQUAL_UINT8_MESSAGE(10, max_number_of_boxes(30), "The function was called with argument: 30.");
3235
}
3336

3437
void test_max_stack_for_medium_box(void) {
38+
TEST_IGNORE();
3539
TEST_ASSERT_EQUAL_UINT8_MESSAGE(3, max_number_of_boxes(85), "The function was called with argument: 85.");
3640
}
3741

3842
void test_max_stack_for_large_box(void) {
39-
TEST_ASSERT_EQUAL_UINT8_MESSAGE(1, max_number_of_boxes(182), "The function was called with argument: 182");
43+
TEST_IGNORE();
44+
TEST_ASSERT_EQUAL_UINT8_MESSAGE(1, max_number_of_boxes(182), "The function was called with argument: 182.");
4045
}
4146

4247
void test_items_remaining_for_first_product(void) {
48+
TEST_IGNORE();
4349
TEST_ASSERT_EQUAL_INT64_MESSAGE(76412, items_to_be_moved(76532, 120), "The function was called with arguments: 76532, 120.");
4450
}
4551

4652
void test_items_remaining_for_second_product(void) {
53+
TEST_IGNORE();
4754
TEST_ASSERT_EQUAL_INT64_MESSAGE(567890, items_to_be_moved(1234876, 666986), "The function was called with arguments: 1234876, 666986.");
4855
}
4956

5057
void test_items_remaining_for_third_product(void) {
58+
TEST_IGNORE();
5159
TEST_ASSERT_EQUAL_INT64_MESSAGE(0, items_to_be_moved(217, 217), "The function was called with arguments: 217, 217.");
5260
}
5361

5462
void test_items_remaining_accounting_error(void) {
63+
TEST_IGNORE();
5564
TEST_ASSERT_EQUAL_INT64_MESSAGE(-567, items_to_be_moved(156, 723), "The function was called with arguments: 156, 723.");
5665
}
5766

5867
void test_payment_few_lost_items(void) {
68+
TEST_IGNORE();
5969
TEST_ASSERT_EQUAL_INT64_MESSAGE(2029, calculate_payment(2000, 1000, 5, 21, 2, 1),
6070
"The function was called with arguments: 2000, 1000, 5, 21, 2, 1.");
6171
}
6272

6373
void test_no_payment_too_many_lost_items(void) {
74+
TEST_IGNORE();
6475
TEST_ASSERT_EQUAL_INT64_MESSAGE(-794, calculate_payment(2000, 598, 2, 120, 45, 4),
6576
"The function was called with arguments: 2000, 598, 2, 120, 45, 4.");
6677
}
6778

6879
void test_payment_many_boxes(void) {
80+
TEST_IGNORE();
6981
TEST_ASSERT_EQUAL_INT64_MESSAGE(2123, calculate_payment(57412, 163214, 183, 1931, 185, 216),
7082
"The function was called with arguments: 57412, 163214, 183, 1931, 185, 216.");
7183
}
7284

7385
void test_no_payment_everything_upfront(void) {
86+
TEST_IGNORE();
7487
TEST_ASSERT_EQUAL_INT64_MESSAGE(0, calculate_payment(22975, 4523, 6, 120, 8, 5),
7588
"The function was called with arguments: 22975, 4523, 6, 120, 8, 5.");
7689
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
FUNC_PROTO = """\
2+
#include "vendor/unity.h"
3+
4+
#include <stdint.h>
5+
6+
extern uint32_t get_box_weight(uint16_t count_first, uint16_t weight_first, uint16_t count_second, uint16_t weight_second);
7+
extern uint8_t max_number_of_boxes(uint8_t box_height);
8+
extern int32_t items_to_be_moved(uint32_t remaining_items, uint32_t items_in_box);
9+
extern int64_t calculate_payment(uint64_t upfront_payment, uint32_t boxes_moved, uint32_t num_trips, uint32_t lost_items,
10+
uint64_t value_of_each_lost_item, uint8_t num_of_workers);
11+
"""
12+
13+
14+
def extra_cases():
15+
return [
16+
{
17+
"task_id": 1,
18+
"description": "weight_of_small_box",
19+
"property": "get_box_weight",
20+
"input": {
21+
"count_first": 30,
22+
"weight_first": 40,
23+
"count_second": 50,
24+
"weight_second": 20,
25+
},
26+
"expected": 2700,
27+
},
28+
{
29+
"task_id": 1,
30+
"description": "weight_of_medium_box",
31+
"property": "get_box_weight",
32+
"input": {
33+
"count_first": 15,
34+
"weight_first": 242,
35+
"count_second": 1100,
36+
"weight_second": 2,
37+
},
38+
"expected": 6330,
39+
},
40+
{
41+
"task_id": 1,
42+
"description": "weight_of_large_box",
43+
"property": "get_box_weight",
44+
"input": {
45+
"count_first": 12,
46+
"weight_first": 5600,
47+
"count_second": 133,
48+
"weight_second": 521,
49+
},
50+
"expected": 136993,
51+
},
52+
{
53+
"task_id": 2,
54+
"description": "max_stack_for_small_box",
55+
"property": "max_number_of_boxes",
56+
"input": 30,
57+
"expected": 10,
58+
},
59+
{
60+
"task_id": 2,
61+
"description": "max_stack_for_medium_box",
62+
"property": "max_number_of_boxes",
63+
"input": 85,
64+
"expected": 3,
65+
},
66+
{
67+
"task_id": 2,
68+
"description": "max_stack_for_large_box",
69+
"property": "max_number_of_boxes",
70+
"input": 182,
71+
"expected": 1,
72+
},
73+
{
74+
"task_id": 3,
75+
"description": "items_remaining_for_first_product",
76+
"property": "items_to_be_moved",
77+
"input": {"remaining_items": 76532, "items_in_box": 120},
78+
"expected": 76412,
79+
},
80+
{
81+
"task_id": 3,
82+
"description": "items_remaining_for_second_product",
83+
"property": "items_to_be_moved",
84+
"input": {"remaining_items": 1234876, "items_in_box": 666986},
85+
"expected": 567890,
86+
},
87+
{
88+
"task_id": 3,
89+
"description": "items_remaining_for_third_product",
90+
"property": "items_to_be_moved",
91+
"input": {"remaining_items": 217, "items_in_box": 217},
92+
"expected": 0,
93+
},
94+
{
95+
"task_id": 3,
96+
"description": "items_remaining_accounting_error",
97+
"property": "items_to_be_moved",
98+
"input": {"remaining_items": 156, "items_in_box": 723},
99+
"expected": -567,
100+
},
101+
{
102+
"task_id": 4,
103+
"description": "payment_few_lost_items",
104+
"property": "calculate_payment",
105+
"input": {
106+
"upfront_payment": 2000,
107+
"boxes_moved": 1000,
108+
"num_trips": 5,
109+
"lost_items": 21,
110+
"value_of_each_lost_item": 2,
111+
"num_of_workers": 1,
112+
},
113+
"expected": 2029,
114+
},
115+
{
116+
"task_id": 4,
117+
"description": "no_payment_too_many_lost_items",
118+
"property": "calculate_payment",
119+
"input": {
120+
"upfront_payment": 2000,
121+
"boxes_moved": 598,
122+
"num_trips": 2,
123+
"lost_items": 120,
124+
"value_of_each_lost_item": 45,
125+
"num_of_workers": 4,
126+
},
127+
"expected": -794,
128+
},
129+
{
130+
"task_id": 4,
131+
"description": "payment_many_boxes",
132+
"property": "calculate_payment",
133+
"input": {
134+
"upfront_payment": 57412,
135+
"boxes_moved": 163214,
136+
"num_trips": 183,
137+
"lost_items": 1931,
138+
"value_of_each_lost_item": 185,
139+
"num_of_workers": 216,
140+
},
141+
"expected": 2123,
142+
},
143+
{
144+
"task_id": 4,
145+
"description": "no_payment_everything_upfront",
146+
"property": "calculate_payment",
147+
"input": {
148+
"upfront_payment": 22975,
149+
"boxes_moved": 4523,
150+
"num_trips": 6,
151+
"lost_items": 120,
152+
"value_of_each_lost_item": 8,
153+
"num_of_workers": 5,
154+
},
155+
"expected": 0,
156+
},
157+
]
158+
159+
160+
def unroll_args(args):
161+
if isinstance(args, (int, float)):
162+
return str(args)
163+
return str([*args.values()]).replace("[", "").replace("]", "")
164+
165+
166+
def gen_func_body(prop, inp, expected):
167+
plural = "s" if isinstance(inp, dict) and len(inp) > 1 else ""
168+
message = f"The function was called with argument{plural}: {unroll_args(inp)}."
169+
if prop == "get_box_weight":
170+
return (
171+
f"TEST_ASSERT_EQUAL_UINT32_MESSAGE({expected}, {prop}"
172+
f"({inp['count_first']}, {inp['weight_first']}, {inp['count_second']}, {inp['weight_second']}), "
173+
f'"{message}");\n'
174+
)
175+
if prop == "max_number_of_boxes":
176+
return f'TEST_ASSERT_EQUAL_UINT8_MESSAGE({expected}, {prop}({inp}), "{message}");\n'
177+
if prop == "items_to_be_moved":
178+
return (
179+
f"TEST_ASSERT_EQUAL_INT64_MESSAGE({expected}, {prop}"
180+
f"({inp['remaining_items']}, {inp['items_in_box']}), "
181+
f'"{message}");\n'
182+
)
183+
return (
184+
f"TEST_ASSERT_EQUAL_INT64_MESSAGE({expected}, {prop}"
185+
f"({inp['upfront_payment']}, {inp['boxes_moved']}, {inp['num_trips']}, "
186+
f"{inp['lost_items']}, {inp['value_of_each_lost_item']}, {inp['num_of_workers']}), "
187+
f'"{message}");\n'
188+
)

0 commit comments

Comments
 (0)