Commit 3e0ef60a by feidy

slice test

parent b0cd1c59
......@@ -2,8 +2,8 @@
* @Author: zhoufei
* @Date: 2022-05-25 13:54:05
* @LastEditors: zhoufei
* @LastEditTime: 2022-06-07 11:57:37
* @FilePath: /sdc/Users/software/Documents/go/utils/slice.go
* @LastEditTime: 2022-06-08 17:02:04
* @FilePath: /utils/slice.go
* @Description:
*
* Copyright (c) 2022 by zhoufei, All Rights Reserved.
......@@ -21,6 +21,15 @@ func SliceIsContain[T any](array []T, value T) (index int, ok bool) {
return -1, false
}
func Reverse(s string) string {
bs := []byte(s)
length := len(bs)
for i := 0; i < length/2; i++ {
bs[i], bs[length-i-1] = bs[length-i-1], bs[i]
}
return string(bs)
}
func SliceDel[T comparable](array []T, values ...T) []T {
if len(values) == 0 {
return array
......
......@@ -2,7 +2,7 @@
* @Author: zhoufei
* @Date: 2022-05-25 14:41:48
* @LastEditors: zhoufei
* @LastEditTime: 2022-06-07 14:19:50
* @LastEditTime: 2022-06-08 17:21:39
* @FilePath: /utils/slice_test.go
* @Description:
*
......@@ -12,6 +12,7 @@ package utils
import (
"testing"
"unicode/utf8"
"github.com/stretchr/testify/assert"
)
......@@ -25,3 +26,20 @@ func TestSliceDel(t *testing.T) {
assert.Equal(t, []string{"a", "b"}, SliceDel([]string{"a", "b", "c"}, "c"))
assert.Equal(t, []string{"a", "b", "c"}, SliceDel([]string{"a", "b", "c"}))
}
func FuzzReverse(f *testing.F) {
str_slice := []string{"abc", "bb"}
for _, v := range str_slice {
f.Add(v)
}
f.Fuzz(func(t *testing.T, str string) {
rev_str1 := Reverse(str)
rev_str2 := Reverse(rev_str1)
if str != rev_str2 {
t.Errorf("fuzz test failed. str:%s, rev_str1:%s, rev_str2:%s", str, rev_str1, rev_str2)
}
if utf8.ValidString(str) && !utf8.ValidString(rev_str1) {
t.Errorf("reverse result is not utf8. str:%s, len: %d, rev_str1:%s", str, len(str), rev_str1)
}
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment