Skip to content

Commit 15bb00c

Browse files
authored
defer delete converted strings in the tests in Raindrops (#188)
* `defer delete` converted strings in the tests in Raindrops https://exercism.org/tracks/odin/exercises/raindrops * fix example.odin, create instructions.append.md
1 parent a1024fd commit 15bb00c

3 files changed

Lines changed: 66 additions & 37 deletions

File tree

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+
converted := convert(3)
8+
// ...
9+
// and eventually the caller will delete the string
10+
delete(converted)
11+
```

exercises/practice/raindrops/.meta/example.odin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ convert :: proc(number: int) -> string {
1010
if number % 5 == 0 { strings.write_string(&b, "Plang") }
1111
if number % 7 == 0 { strings.write_string(&b, "Plong") }
1212
if strings.builder_len(b) == 0 { strings.write_int(&b, number) }
13-
return strings.to_string(b)
13+
return strings.clone(strings.to_string(b))
1414
}
Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,149 @@
11
package raindrops
22

3-
import "core:mem"
43
import "core:testing"
54

65
@(test)
76
/// description = the sound for 1 is 1
87
test_the_sound_for_1_is_1 :: proc(t: ^testing.T) {
9-
testing.expect_value(t, convert(1), "1")
8+
converted := convert(1)
9+
defer delete(converted)
10+
testing.expect_value(t, converted, "1")
1011
}
1112

1213
@(test)
1314
/// description = the sound for 3 is Pling
1415
test_the_sound_for_3_is_pling :: proc(t: ^testing.T) {
15-
testing.expect_value(t, convert(3), "Pling")
16+
converted := convert(3)
17+
defer delete(converted)
18+
testing.expect_value(t, converted, "Pling")
1619
}
1720

1821
@(test)
1922
/// description = the sound for 5 is Plang
2023
test_the_sound_for_5_is_plang :: proc(t: ^testing.T) {
21-
testing.expect_value(t, convert(5), "Plang")
24+
converted := convert(5)
25+
defer delete(converted)
26+
testing.expect_value(t, converted, "Plang")
2227
}
2328

2429
@(test)
2530
/// description = the sound for 7 is Plong
2631
test_the_sound_for_7_is_plong :: proc(t: ^testing.T) {
27-
testing.expect_value(t, convert(7), "Plong")
32+
converted := convert(7)
33+
defer delete(converted)
34+
testing.expect_value(t, converted, "Plong")
2835
}
2936

3037
@(test)
3138
/// description = the sound for 6 is Pling as it has a factor 3
3239
test_the_sound_for_6_is_pling_as_it_has_a_factor_3 :: proc(t: ^testing.T) {
33-
testing.expect_value(t, convert(6), "Pling")
40+
converted := convert(6)
41+
defer delete(converted)
42+
testing.expect_value(t, converted, "Pling")
3443
}
3544

3645
@(test)
3746
/// description = 2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base
3847
test_2_to_the_power_3_does_not_make_a_raindrop_sound_as_3_is_the_exponent_not_the_base :: proc(
3948
t: ^testing.T,
4049
) {
41-
testing.expect_value(t, convert(8), "8")
50+
converted := convert(8)
51+
defer delete(converted)
52+
testing.expect_value(t, converted, "8")
4253
}
4354

4455
@(test)
4556
/// description = the sound for 9 is Pling as it has a factor 3
4657
test_the_sound_for_9_is_pling_as_it_has_a_factor_3 :: proc(t: ^testing.T) {
47-
testing.expect_value(t, convert(9), "Pling")
58+
converted := convert(9)
59+
defer delete(converted)
60+
testing.expect_value(t, converted, "Pling")
4861
}
4962

5063
@(test)
5164
/// description = the sound for 10 is Plang as it has a factor 5
5265
test_the_sound_for_10_is_plang_as_it_has_a_factor_5 :: proc(t: ^testing.T) {
53-
testing.expect_value(t, convert(10), "Plang")
66+
converted := convert(10)
67+
defer delete(converted)
68+
testing.expect_value(t, converted, "Plang")
5469
}
5570

5671
@(test)
5772
/// description = the sound for 14 is Plong as it has a factor of 7
5873
test_the_sound_for_14_is_plong_as_it_has_a_factor_of_7 :: proc(t: ^testing.T) {
59-
testing.expect_value(t, convert(14), "Plong")
74+
converted := convert(14)
75+
defer delete(converted)
76+
testing.expect_value(t, converted, "Plong")
6077
}
6178

6279
@(test)
6380
/// description = the sound for 15 is PlingPlang as it has factors 3 and 5
6481
test_the_sound_for_15_is_pling_plang_as_it_has_factors_3_and_5 :: proc(t: ^testing.T) {
65-
testing.expect_value(t, convert(15), "PlingPlang")
82+
converted := convert(15)
83+
defer delete(converted)
84+
testing.expect_value(t, converted, "PlingPlang")
6685
}
6786

6887
@(test)
6988
/// description = the sound for 21 is PlingPlong as it has factors 3 and 7
7089
test_the_sound_for_21_is_pling_plong_as_it_has_factors_3_and_7 :: proc(t: ^testing.T) {
71-
testing.expect_value(t, convert(21), "PlingPlong")
90+
converted := convert(21)
91+
defer delete(converted)
92+
testing.expect_value(t, converted, "PlingPlong")
7293
}
7394

7495
@(test)
7596
/// description = the sound for 25 is Plang as it has a factor 5
7697
test_the_sound_for_25_is_plang_as_it_has_a_factor_5 :: proc(t: ^testing.T) {
77-
testing.expect_value(t, convert(25), "Plang")
98+
converted := convert(25)
99+
defer delete(converted)
100+
testing.expect_value(t, converted, "Plang")
78101
}
79102

80103
@(test)
81104
/// description = the sound for 27 is Pling as it has a factor 3
82105
test_the_sound_for_27_is_pling_as_it_has_a_factor_3 :: proc(t: ^testing.T) {
83-
testing.expect_value(t, convert(27), "Pling")
106+
converted := convert(27)
107+
defer delete(converted)
108+
testing.expect_value(t, converted, "Pling")
84109
}
85110

86111
@(test)
87112
/// description = the sound for 35 is PlangPlong as it has factors 5 and 7
88113
test_the_sound_for_35_is_plang_plong_as_it_has_factors_5_and_7 :: proc(t: ^testing.T) {
89-
testing.expect_value(t, convert(35), "PlangPlong")
114+
converted := convert(35)
115+
defer delete(converted)
116+
testing.expect_value(t, converted, "PlangPlong")
90117
}
91118

92119
@(test)
93120
/// description = the sound for 49 is Plong as it has a factor 7
94121
test_the_sound_for_49_is_plong_as_it_has_a_factor_7 :: proc(t: ^testing.T) {
95-
testing.expect_value(t, convert(49), "Plong")
122+
converted := convert(49)
123+
defer delete(converted)
124+
testing.expect_value(t, converted, "Plong")
96125
}
97126

98127
@(test)
99128
/// description = the sound for 52 is 52
100129
test_the_sound_for_52_is_52 :: proc(t: ^testing.T) {
101-
testing.expect_value(t, convert(52), "52")
130+
converted := convert(52)
131+
defer delete(converted)
132+
testing.expect_value(t, converted, "52")
102133
}
103134

104135
@(test)
105136
/// description = the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7
106137
test_the_sound_for_105_is_pling_plang_plong_as_it_has_factors_3_5_and_7 :: proc(t: ^testing.T) {
107-
testing.expect_value(t, convert(105), "PlingPlangPlong")
138+
converted := convert(105)
139+
defer delete(converted)
140+
testing.expect_value(t, converted, "PlingPlangPlong")
108141
}
109142

110143
@(test)
111144
/// description = the sound for 3125 is Plang as it has a factor 5
112145
test_the_sound_for_3125_is_plang_as_it_has_a_factor_5 :: proc(t: ^testing.T) {
113-
testing.expect_value(t, convert(3125), "Plang")
114-
}
115-
116-
// This test is optional. If you are on the command line
117-
// and want to check that your solution has no memory
118-
// leak, uncomment this test.
119-
// If you want to learn more about Odin's tracking
120-
// allocator, you may watch this YouTube video:
121-
// https://www.youtube.com/watch?v=dg6qogN8kIE
122-
// @(test)
123-
test_no_memory_leaks :: proc(t: ^testing.T) {
124-
track: mem.Tracking_Allocator
125-
mem.tracking_allocator_init(&track, context.allocator)
126-
defer mem.tracking_allocator_destroy(&track)
127-
context.allocator = mem.tracking_allocator(&track)
128-
test_the_sound_for_105_is_pling_plang_plong_as_it_has_factors_3_5_and_7(t)
129-
testing.expect_value(t, len(track.allocation_map), 0)
130-
testing.expect_value(t, len(track.bad_free_array), 0)
146+
converted := convert(3125)
147+
defer delete(converted)
148+
testing.expect_value(t, converted, "Plang")
131149
}

0 commit comments

Comments
 (0)