Skip to content

Commit a1024fd

Browse files
authored
defer delete formatted strings in the tests in Line Up (#187)
* `defer delete` formatted strings in the tests * Use fmt.aprintf, Add instructions on memory management * Added instructions.append.md to explain caller is responsible for reclaiming memory * run ./bin/format-all.sh
1 parent 51d3a06 commit a1024fd

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed

dev/tools/tagfixer/example/expected_rational_numbers_test.odin

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,3 @@ unique_26_characters_but_not_pangram :: proc(t: ^testing.T) {
579579
non_alphanumeric_printable :: proc(t: ^testing.T) {
580580
testing.expect(t, !is_pangram(" !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"))
581581
}
582-
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions append
2+
3+
In this exercise you will implement a procedure which is meant to allocate the string it returns. Deallocating the string is the responsibility of the prodecure's caller.
4+
5+
```odin
6+
// The procedure will be called like that:
7+
formatted := format("Gianna", 4)
8+
// ...
9+
// and eventually the caller will delete the string
10+
delete(formatted)
11+
```

exercises/practice/line-up/.meta/example.odin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ suffix :: proc(number: int) -> string {
2121
}
2222

2323
format :: proc(name: string, number: int) -> string {
24-
return fmt.tprintf(
24+
return fmt.aprintf(
2525
"%s, you are the %d%s customer we serve today. Thank you!",
2626
name,
2727
number,

exercises/practice/line-up/line_up_test.odin

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,189 +5,219 @@ import "core:testing"
55
@(test)
66
/// description = format smallest non-exceptional ordinal numeral 4
77
test_format_smallest_non_exceptional_ordinal_numeral_4 :: proc(t: ^testing.T) {
8+
formatted := format("Gianna", 4)
9+
defer delete(formatted)
810
testing.expect_value(
911
t,
10-
format("Gianna", 4),
12+
formatted,
1113
"Gianna, you are the 4th customer we serve today. Thank you!",
1214
)
1315
}
1416

1517
@(test)
1618
/// description = format greatest single digit non-exceptional ordinal numeral 9
1719
test_format_greatest_single_digit_non_exceptional_ordinal_numeral_9 :: proc(t: ^testing.T) {
20+
formatted := format("Maarten", 9)
21+
defer delete(formatted)
1822
testing.expect_value(
1923
t,
20-
format("Maarten", 9),
24+
formatted,
2125
"Maarten, you are the 9th customer we serve today. Thank you!",
2226
)
2327
}
2428

2529
@(test)
2630
/// description = format non-exceptional ordinal numeral 5
2731
test_format_non_exceptional_ordinal_numeral_5 :: proc(t: ^testing.T) {
32+
formatted := format("Petronila", 5)
33+
defer delete(formatted)
2834
testing.expect_value(
2935
t,
30-
format("Petronila", 5),
36+
formatted,
3137
"Petronila, you are the 5th customer we serve today. Thank you!",
3238
)
3339
}
3440

3541
@(test)
3642
/// description = format non-exceptional ordinal numeral 6
3743
test_format_non_exceptional_ordinal_numeral_6 :: proc(t: ^testing.T) {
44+
formatted := format("Attakullakulla", 6)
45+
defer delete(formatted)
3846
testing.expect_value(
3947
t,
40-
format("Attakullakulla", 6),
48+
formatted,
4149
"Attakullakulla, you are the 6th customer we serve today. Thank you!",
4250
)
4351
}
4452

4553
@(test)
4654
/// description = format non-exceptional ordinal numeral 7
4755
test_format_non_exceptional_ordinal_numeral_7 :: proc(t: ^testing.T) {
48-
testing.expect_value(
49-
t,
50-
format("Kate", 7),
51-
"Kate, you are the 7th customer we serve today. Thank you!",
52-
)
56+
formatted := format("Kate", 7)
57+
defer delete(formatted)
58+
testing.expect_value(t, formatted, "Kate, you are the 7th customer we serve today. Thank you!")
5359
}
5460

5561
@(test)
5662
/// description = format non-exceptional ordinal numeral 8
5763
test_format_non_exceptional_ordinal_numeral_8 :: proc(t: ^testing.T) {
64+
formatted := format("Maximiliano", 8)
65+
defer delete(formatted)
5866
testing.expect_value(
5967
t,
60-
format("Maximiliano", 8),
68+
formatted,
6169
"Maximiliano, you are the 8th customer we serve today. Thank you!",
6270
)
6371
}
6472

6573
@(test)
6674
/// description = format exceptional ordinal numeral 1
6775
test_format_exceptional_ordinal_numeral_1 :: proc(t: ^testing.T) {
68-
testing.expect_value(
69-
t,
70-
format("Mary", 1),
71-
"Mary, you are the 1st customer we serve today. Thank you!",
72-
)
76+
formatted := format("Mary", 1)
77+
defer delete(formatted)
78+
testing.expect_value(t, formatted, "Mary, you are the 1st customer we serve today. Thank you!")
7379
}
7480

7581
@(test)
7682
/// description = format exceptional ordinal numeral 2
7783
test_format_exceptional_ordinal_numeral_2 :: proc(t: ^testing.T) {
84+
formatted := format("Haruto", 2)
85+
defer delete(formatted)
7886
testing.expect_value(
7987
t,
80-
format("Haruto", 2),
88+
formatted,
8189
"Haruto, you are the 2nd customer we serve today. Thank you!",
8290
)
8391
}
8492

8593
@(test)
8694
/// description = format exceptional ordinal numeral 3
8795
test_format_exceptional_ordinal_numeral_3 :: proc(t: ^testing.T) {
96+
formatted := format("Henriette", 3)
97+
defer delete(formatted)
8898
testing.expect_value(
8999
t,
90-
format("Henriette", 3),
100+
formatted,
91101
"Henriette, you are the 3rd customer we serve today. Thank you!",
92102
)
93103
}
94104

95105
@(test)
96106
/// description = format smallest two digit non-exceptional ordinal numeral 10
97107
test_format_smallest_two_digit_non_exceptional_ordinal_numeral_10 :: proc(t: ^testing.T) {
108+
formatted := format("Alvarez", 10)
109+
defer delete(formatted)
98110
testing.expect_value(
99111
t,
100-
format("Alvarez", 10),
112+
formatted,
101113
"Alvarez, you are the 10th customer we serve today. Thank you!",
102114
)
103115
}
104116

105117
@(test)
106118
/// description = format non-exceptional ordinal numeral 11
107119
test_format_non_exceptional_ordinal_numeral_11 :: proc(t: ^testing.T) {
120+
formatted := format("Jacqueline", 11)
121+
defer delete(formatted)
108122
testing.expect_value(
109123
t,
110-
format("Jacqueline", 11),
124+
formatted,
111125
"Jacqueline, you are the 11th customer we serve today. Thank you!",
112126
)
113127
}
114128

115129
@(test)
116130
/// description = format non-exceptional ordinal numeral 12
117131
test_format_non_exceptional_ordinal_numeral_12 :: proc(t: ^testing.T) {
132+
formatted := format("Juan", 12)
133+
defer delete(formatted)
118134
testing.expect_value(
119135
t,
120-
format("Juan", 12),
136+
formatted,
121137
"Juan, you are the 12th customer we serve today. Thank you!",
122138
)
123139
}
124140

125141
@(test)
126142
/// description = format non-exceptional ordinal numeral 13
127143
test_format_non_exceptional_ordinal_numeral_13 :: proc(t: ^testing.T) {
144+
formatted := format("Patricia", 13)
145+
defer delete(formatted)
128146
testing.expect_value(
129147
t,
130-
format("Patricia", 13),
148+
formatted,
131149
"Patricia, you are the 13th customer we serve today. Thank you!",
132150
)
133151
}
134152

135153
@(test)
136154
/// description = format exceptional ordinal numeral 21
137155
test_format_exceptional_ordinal_numeral_21 :: proc(t: ^testing.T) {
156+
formatted := format("Washi", 21)
157+
defer delete(formatted)
138158
testing.expect_value(
139159
t,
140-
format("Washi", 21),
160+
formatted,
141161
"Washi, you are the 21st customer we serve today. Thank you!",
142162
)
143163
}
144164

145165
@(test)
146166
/// description = format exceptional ordinal numeral 62
147167
test_format_exceptional_ordinal_numeral_62 :: proc(t: ^testing.T) {
168+
formatted := format("Nayra", 62)
169+
defer delete(formatted)
148170
testing.expect_value(
149171
t,
150-
format("Nayra", 62),
172+
formatted,
151173
"Nayra, you are the 62nd customer we serve today. Thank you!",
152174
)
153175
}
154176

155177
@(test)
156178
/// description = format exceptional ordinal numeral 100
157179
test_format_exceptional_ordinal_numeral_100 :: proc(t: ^testing.T) {
180+
formatted := format("John", 100)
181+
defer delete(formatted)
158182
testing.expect_value(
159183
t,
160-
format("John", 100),
184+
formatted,
161185
"John, you are the 100th customer we serve today. Thank you!",
162186
)
163187
}
164188

165189
@(test)
166190
/// description = format exceptional ordinal numeral 101
167191
test_format_exceptional_ordinal_numeral_101 :: proc(t: ^testing.T) {
192+
formatted := format("Zeinab", 101)
193+
defer delete(formatted)
168194
testing.expect_value(
169195
t,
170-
format("Zeinab", 101),
196+
formatted,
171197
"Zeinab, you are the 101st customer we serve today. Thank you!",
172198
)
173199
}
174200

175201
@(test)
176202
/// description = format non-exceptional ordinal numeral 112
177203
test_format_non_exceptional_ordinal_numeral_112 :: proc(t: ^testing.T) {
204+
formatted := format("Knud", 112)
205+
defer delete(formatted)
178206
testing.expect_value(
179207
t,
180-
format("Knud", 112),
208+
formatted,
181209
"Knud, you are the 112th customer we serve today. Thank you!",
182210
)
183211
}
184212

185213
@(test)
186214
/// description = format exceptional ordinal numeral 123
187215
test_format_exceptional_ordinal_numeral_123 :: proc(t: ^testing.T) {
216+
formatted := format("Yma", 123)
217+
defer delete(formatted)
188218
testing.expect_value(
189219
t,
190-
format("Yma", 123),
220+
formatted,
191221
"Yma, you are the 123rd customer we serve today. Thank you!",
192222
)
193223
}

0 commit comments

Comments
 (0)