12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package buildin
- import "testing"
- func TestSplitStringByLength(t *testing.T) {
- // 测试用例1:字符串长度小于 maxLength
- str := "Hello"
- maxLength := 10
- expected := []string{"Hello"}
- result := SplitStringByLength(str, maxLength)
- if len(result) != len(expected) {
- t.Errorf("Expected %d elements, but got %d", len(expected), len(result))
- }
- for i := range result {
- if result[i] != expected[i] {
- t.Errorf("Expected %s, but got %s", expected[i], result[i])
- }
- }
- // 测试用例2:字符串长度大于 maxLength
- str = "你好,Hello,世界!"
- maxLength = 5
- expected = []string{"你好,", "Hello", ",世界", "!"}
- result = SplitStringByLength(str, maxLength)
- if len(result) != len(expected) {
- t.Errorf("Expected %d elements, but got %d", len(expected), len(result))
- }
- for i := range result {
- if result[i] != expected[i] {
- t.Errorf("Expected %s, but got %s", expected[i], result[i])
- }
- }
- }
- func TestRandString(t *testing.T) {
- // 测试用例1:生成长度为10的随机字符串
- n := 10
- result := RandString(n)
- if len(result) != n {
- t.Errorf("Expected string length %d, but got %d", n, len(result))
- }
- // 测试用例2:生成长度为20的随机字符串
- n = 20
- result = RandString(n)
- if len(result) != n {
- t.Errorf("Expected string length %d, but got %d", n, len(result))
- }
- // 测试用例3:生成长度为0的随机字符串
- n = 0
- result = RandString(n)
- if len(result) != n {
- t.Errorf("Expected string length %d, but got %d", n, len(result))
- }
- }
- func BenchmarkRandString(b *testing.B) {
- n := 10
- for i := 0; i < b.N; i++ {
- RandString(n)
- }
- }
- func TestRandNumberString(t *testing.T) {
- // 测试用例1:生成3位数字字符串
- bit := uint8(3)
- result := RandNumberString(bit)
- if len(result) != int(bit) {
- t.Errorf("Expected string length %d, but got %d", bit, len(result))
- }
- // 测试用例2:生成5位数字字符串
- bit = uint8(5)
- result = RandNumberString(bit)
- if len(result) != int(bit) {
- t.Errorf("Expected string length %d, but got %d", bit, len(result))
- }
- // 测试用例3:生成0位数字字符串
- bit = uint8(0)
- result = RandNumberString(bit)
- if len(result) != int(bit) {
- t.Errorf("Expected string length %d, but got %d", bit, len(result))
- }
- }
- func BenchmarkRandNumberString(b *testing.B) {
- bit := uint8(10)
- for i := 0; i < b.N; i++ {
- RandNumberString(bit)
- }
- }
|