mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 19:54:43 +01:00
code kata 230818
This commit is contained in:
44
code_kata_230818/int_to_string_test.go
Normal file
44
code_kata_230818/int_to_string_test.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func IntToString(n int) string {
|
||||||
|
r := make([]byte, 0)
|
||||||
|
m := false
|
||||||
|
if n < 0 {
|
||||||
|
m = true
|
||||||
|
n = -n
|
||||||
|
}
|
||||||
|
for n > 0 {
|
||||||
|
r = append(r, byte('0'+n%10))
|
||||||
|
n /= 10
|
||||||
|
}
|
||||||
|
if m {
|
||||||
|
r = append(r, '-')
|
||||||
|
}
|
||||||
|
for s, e := 0, len(r)-1; s < e; s, e = s+1, e-1 {
|
||||||
|
r[s], r[e] = r[e], r[s]
|
||||||
|
}
|
||||||
|
return string(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntToString(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
input int
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"1", 1, "1"},
|
||||||
|
{"12", 12, "12"},
|
||||||
|
{"-12", -12, "-12"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := IntToString(tc.input)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("IntToString(%d) = %s, want %s\n", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user