-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_test.go
More file actions
197 lines (179 loc) · 4.67 KB
/
validate_test.go
File metadata and controls
197 lines (179 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package strings
import (
"testing"
)
// TestIsASCII tests the IsASCII function.
func TestIsASCII(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"Hello, World!", true},
{"こんにちは", false}, // Japanese characters
{"12345", true}, // Numeric ASCII characters
{"", true}, // Empty string
{"abc\x80", false}, // Non-ASCII byte in string
{"\x7F\x7E", true}, // Edge ASCII values
}
for _, tt := range tests {
result := IsASCII(tt.input)
if result != tt.expected {
t.Errorf("IsASCII(%q) = %v; want %v", tt.input, result, tt.expected)
}
}
}
// TestIsUTF8 tests the IsUTF8 function.
func TestIsUTF8(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"Hello, World!", true},
{"こんにちは", true}, // Valid UTF-8 Japanese characters
{"\xff\xfe\xfd", false}, // Invalid UTF-8 byte sequence
{"12345", true}, // ASCII is valid UTF-8
{"", true}, // Empty string
}
for _, tt := range tests {
result := IsUTF8(tt.input)
if result != tt.expected {
t.Errorf("IsUTF8(%q) = %v; want %v", tt.input, result, tt.expected)
}
}
}
// TestIsUTF8Bytes tests the IsUTF8Bytes function.
func TestIsUTF8Bytes(t *testing.T) {
tests := []struct {
input []byte
expected bool
}{
{[]byte("Hello, World!"), true},
{[]byte("こんにちは"), true}, // Valid UTF-8 Japanese characters
{[]byte{0xff, 0xfe, 0xfd}, false}, // Invalid UTF-8 byte sequence
{[]byte("12345"), true}, // ASCII is valid UTF-8
{[]byte{}, true}, // Empty byte slice
}
for _, tt := range tests {
result := IsUTF8Bytes(tt.input)
if result != tt.expected {
t.Errorf("IsUTF8Bytes(%v) = %v; want %v", tt.input, result, tt.expected)
}
}
}
// TestIsInt tests the IsInt function.
func TestIsInt(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"123", true},
{"-123", true},
{"+123", true},
{"123.45", false}, // Not an integer
{"abc", false}, // Not a number
{"", false}, // Empty string
}
for _, tt := range tests {
result := IsInt(tt.input)
if result != tt.expected {
t.Errorf("IsInt(%q) = %v; want %v", tt.input, result, tt.expected)
}
}
}
// TestIsFloat tests the IsFloat function.
func TestIsFloat(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"123.45", true},
{"-123.45", true},
{"+123.45", true},
{"123", true}, // Integers are valid floats
{"abc", false}, // Not a number
{"", false}, // Empty string
}
for _, tt := range tests {
result := IsFloat(tt.input)
if result != tt.expected {
t.Errorf("IsFloat(%q) = %v; want %v", tt.input, result, tt.expected)
}
}
}
// TestStripNewlines tests the StripNewlines function.
func TestStripNewlines(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"Hello\nWorld", "HelloWorld"},
{"Hello\r\nWorld", "HelloWorld"},
{"Hello World", "Hello World"}, // No newlines
{"\n\r\n\r", ""}, // Only newlines
{"", ""}, // Empty string
}
for _, tt := range tests {
result := StripNewlines(tt.input)
if result != tt.expected {
t.Errorf("StripNewlines(%q) = %q; want %q", tt.input, result, tt.expected)
}
}
}
// TestStripNulls tests the StripNulls function.
func TestStripNulls(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"Hello\x00World", "HelloWorld"},
{"Hello World", "Hello World"}, // No null characters
{"\x00\x00\x00", ""}, // Only null characters
{"", ""}, // Empty string
}
for _, tt := range tests {
result := StripNulls(tt.input)
if result != tt.expected {
t.Errorf("StripNulls(%q) = %q; want %q", tt.input, result, tt.expected)
}
}
}
// TestHasNull tests the HasNull function.
func TestHasNull(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"Hello\x00World", true},
{"Hello World", false}, // No null characters
{"\x00", true}, // Single null character
{"", false}, // Empty string
}
for _, tt := range tests {
result := HasNull(tt.input)
if result != tt.expected {
t.Errorf("HasNull(%q) = %v; want %v", tt.input, result, tt.expected)
}
}
}
func TestIsWhitespace(t *testing.T) {
tests := []struct {
name string
s string
want bool
}{
{"empty", "", true},
{"space", " ", true},
{"newline", "\n", true},
{"tab", "\t", true},
{"mix", " \t\n", true},
{"not", "bn", false},
{"mix-not", " b\np", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsWhitespace(tt.s); got != tt.want {
t.Errorf("IsWhitespace() = %v, want %v", got, tt.want)
}
})
}
}