Skip to content

Commit 12acdd7

Browse files
authored
defer delete strings in the tests in Two Fer (#191)
* `defer delete` strings in the tests in Two Fer https://exercism.org/tracks/odin/exercises/two-fer * use fmt.aprintf(), add memory management block into instructions.append.md
1 parent 15bb00c commit 12acdd7

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

exercises/practice/two-fer/.docs/instructions.append.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ If the arguments you need are in positional order, like in the example above, th
6666
greeting("John Doe", add_exclamation=true)
6767
```
6868

69+
## Memory management
70+
71+
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.
72+
73+
```odin
74+
// The procedure will be called like that:
75+
str := two_fer("Alice")
76+
// ...
77+
// and eventually the caller will delete the string
78+
delete(str)
79+
```
80+
6981
---
7082

7183
You may also be interested in the official Odin documentation about [default arguments][default-values] and Odin [named arguments][named-arguments].

exercises/practice/two-fer/.meta/example.odin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package two_fer
33
import "core:fmt"
44

55
two_fer :: proc(name: string = "you") -> string {
6-
return fmt.tprintf("One for {}, one for me.", name)
6+
return fmt.aprintf("One for {}, one for me.", name)
77
}

exercises/practice/two-fer/two_fer_test.odin

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ import "core:testing"
55
@(test)
66
/// description = no name given
77
test_no_name_given :: proc(t: ^testing.T) {
8-
testing.expect_value(t, two_fer(), "One for you, one for me.")
8+
str := two_fer()
9+
defer delete(str)
10+
testing.expect_value(t, str, "One for you, one for me.")
911
}
1012

1113
@(test)
1214
/// description = a name given
1315
test_a_name_given :: proc(t: ^testing.T) {
14-
testing.expect_value(t, two_fer("Alice"), "One for Alice, one for me.")
16+
str := two_fer("Alice")
17+
defer delete(str)
18+
testing.expect_value(t, str, "One for Alice, one for me.")
1519
}
1620

1721
@(test)
1822
/// description = another name given
1923
test_another_name_given :: proc(t: ^testing.T) {
20-
testing.expect_value(t, two_fer("Bob"), "One for Bob, one for me.")
24+
str := two_fer("Bob")
25+
defer delete(str)
26+
testing.expect_value(t, str, "One for Bob, one for me.")
2127
}

0 commit comments

Comments
 (0)