2020-02-26 20:26:57 +00:00
|
|
|
package reverse
|
2020-02-24 19:54:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-18 10:37:40 +00:00
|
|
|
"strings"
|
2020-02-24 19:54:48 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReverse(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
{input: "", output: ""},
|
|
|
|
{input: "x", output: "x"},
|
|
|
|
{input: "Hello!", output: "!olleH"},
|
|
|
|
{input: "Привет", output: "тевирП"},
|
|
|
|
{input: "\r\n", output: "\n\r"},
|
|
|
|
{input: "\n\n", output: "\n\n"},
|
|
|
|
{input: "\t*", output: "*\t"},
|
|
|
|
// NB: Диакритика съехала!
|
|
|
|
{input: "möp", output: "p̈om"},
|
|
|
|
// NB: Иероглиф развалился!,
|
|
|
|
{input: "뢴", output: "ᆫᅬᄅ"},
|
|
|
|
{input: "Hello, 世界", output: "界世 ,olleH"},
|
|
|
|
{input: "ำ", output: "ำ"},
|
|
|
|
{input: "ำำ", output: "ำำ"},
|
|
|
|
// NB: Эмоджи распался.
|
|
|
|
{input: "👩❤️💋👩", output: "👩💋️❤👩"},
|
|
|
|
// NB: Эмоджи распался.
|
|
|
|
{input: "🏋🏽♀️", output: "️♀\u200d🏽🏋"},
|
|
|
|
{input: "🙂", output: "🙂"},
|
|
|
|
{input: "🙂🙂", output: "🙂🙂"},
|
|
|
|
// NB: DE != ED
|
|
|
|
{input: "🇩🇪", output: "🇪🇩"},
|
|
|
|
// NB: Флаг распался. :)
|
|
|
|
{input: "🏳️🌈", output: "🌈️🏳"},
|
2021-02-18 10:57:48 +00:00
|
|
|
{input: "\xff\x00\xff\x00", output: "\x00\xef\xbf\xbd\x00\xef\xbf\xbd"},
|
2020-02-24 19:54:48 +00:00
|
|
|
} {
|
|
|
|
t.Run(fmt.Sprintf("#%v: %v", i, tc.input), func(t *testing.T) {
|
|
|
|
require.Equal(t, tc.output, Reverse(tc.input))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 10:37:40 +00:00
|
|
|
|
|
|
|
func BenchmarkReverse(b *testing.B) {
|
|
|
|
input := strings.Repeat("🙂🙂", 100)
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = Reverse(input)
|
|
|
|
}
|
|
|
|
}
|