|
| 1 | +package tinystring |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestHasPrefix(t *testing.T) { |
| 6 | + tests := []struct { |
| 7 | + name string |
| 8 | + s string |
| 9 | + prefix string |
| 10 | + want bool |
| 11 | + }{ |
| 12 | + {"empty prefix", "hello", "", true}, |
| 13 | + {"exact match", "hello", "hello", true}, |
| 14 | + {"short prefix", "hello", "he", true}, |
| 15 | + {"not a prefix", "hello", "ello", false}, |
| 16 | + {"prefix longer than string", "hi", "hello", false}, |
| 17 | + {"single byte", "abc", "a", true}, |
| 18 | + {"null byte prefix", "a\x00b", "a\x00", true}, |
| 19 | + {"unicode prefix", "ñandú", "ñan", true}, |
| 20 | + } |
| 21 | + |
| 22 | + for _, tc := range tests { |
| 23 | + t.Run(tc.name, func(t *testing.T) { |
| 24 | + got := HasPrefix(tc.s, tc.prefix) |
| 25 | + if got != tc.want { |
| 26 | + t.Fatalf("HasPrefix(%q, %q) = %v; want %v", tc.s, tc.prefix, got, tc.want) |
| 27 | + } |
| 28 | + }) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestHasSuffix(t *testing.T) { |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + s string |
| 36 | + suffix string |
| 37 | + want bool |
| 38 | + }{ |
| 39 | + {"empty suffix", "hello", "", true}, |
| 40 | + {"exact match", "hello", "hello", true}, |
| 41 | + {"short suffix", "hello", "lo", true}, |
| 42 | + {"not a suffix", "hello", "hel", false}, |
| 43 | + {"suffix longer than string", "go", "golang", false}, |
| 44 | + {"single byte", "abc", "c", true}, |
| 45 | + {"null byte suffix", "a\x00b", "\x00b", true}, |
| 46 | + {"unicode suffix", "pingüino", "üino", true}, |
| 47 | + } |
| 48 | + |
| 49 | + for _, tc := range tests { |
| 50 | + t.Run(tc.name, func(t *testing.T) { |
| 51 | + got := HasSuffix(tc.s, tc.suffix) |
| 52 | + if got != tc.want { |
| 53 | + t.Fatalf("HasSuffix(%q, %q) = %v; want %v", tc.s, tc.suffix, got, tc.want) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
0 commit comments