Skip to content
Myr

indexbyte2_test.go

This file

Path

src/algo/indexbyte2_test.go

259 lines · 6.9 KiB · Go

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 76efddd point at it.

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 1–259why, one step back
1package algo
23import (4 "bytes"5 "testing"6)78func TestIndexByteTwo(t *testing.T) {9 tests := []struct {10 name string11 s string12 b1 byte13 b2 byte14 want int15 }{16 {"empty", "", 'a', 'b', -1},17 {"single_b1", "a", 'a', 'b', 0},18 {"single_b2", "b", 'a', 'b', 0},19 {"single_none", "c", 'a', 'b', -1},20 {"b1_first", "xaxb", 'a', 'b', 1},21 {"b2_first", "xbxa", 'a', 'b', 1},22 {"same_byte", "xxa", 'a', 'a', 2},23 {"at_end", "xxxxa", 'a', 'b', 4},24 {"not_found", "xxxxxxxx", 'a', 'b', -1},25 {"long_b1_at_3000", string(make([]byte, 3000)) + "a" + string(make([]byte, 1000)), 'a', 'b', 3000},26 {"long_b2_at_3000", string(make([]byte, 3000)) + "b" + string(make([]byte, 1000)), 'a', 'b', 3000},27 }2829 for _, tt := range tests {30 t.Run(tt.name, func(t *testing.T) {31 got := IndexByteTwo([]byte(tt.s), tt.b1, tt.b2)32 if got != tt.want {33 t.Errorf("IndexByteTwo(%q, %c, %c) = %d, want %d", tt.s[:min(len(tt.s), 40)], tt.b1, tt.b2, got, tt.want)34 }35 })36 }3738 // Exhaustive test: compare against loop reference for various lengths,39 // including sizes around SIMD block boundaries (16, 32, 64).40 for n := 0; n <= 256; n++ {41 data := make([]byte, n)42 for i := range data {43 data[i] = byte('c' + (i % 20))44 }45 // Test with match at every position46 for pos := 0; pos < n; pos++ {47 for _, b := range []byte{'A', 'B'} {48 data[pos] = b49 got := IndexByteTwo(data, 'A', 'B')50 want := loopIndexByteTwo(data, 'A', 'B')51 if got != want {52 t.Fatalf("IndexByteTwo(len=%d, match=%c@%d) = %d, want %d", n, b, pos, got, want)53 }54 data[pos] = byte('c' + (pos % 20))55 }56 }57 // Test with no match58 got := IndexByteTwo(data, 'A', 'B')59 if got != -1 {60 t.Fatalf("IndexByteTwo(len=%d, no match) = %d, want -1", n, got)61 }62 // Test with both bytes present63 if n >= 2 {64 data[n/3] = 'A'65 data[n*2/3] = 'B'66 got := IndexByteTwo(data, 'A', 'B')67 want := loopIndexByteTwo(data, 'A', 'B')68 if got != want {69 t.Fatalf("IndexByteTwo(len=%d, both@%d,%d) = %d, want %d", n, n/3, n*2/3, got, want)70 }71 data[n/3] = byte('c' + ((n / 3) % 20))72 data[n*2/3] = byte('c' + ((n * 2 / 3) % 20))73 }74 }75}7677func TestLastIndexByteTwo(t *testing.T) {78 tests := []struct {79 name string80 s string81 b1 byte82 b2 byte83 want int84 }{85 {"empty", "", 'a', 'b', -1},86 {"single_b1", "a", 'a', 'b', 0},87 {"single_b2", "b", 'a', 'b', 0},88 {"single_none", "c", 'a', 'b', -1},89 {"b1_last", "xbxa", 'a', 'b', 3},90 {"b2_last", "xaxb", 'a', 'b', 3},91 {"same_byte", "axx", 'a', 'a', 0},92 {"at_start", "axxxx", 'a', 'b', 0},93 {"both_present", "axbx", 'a', 'b', 2},94 {"not_found", "xxxxxxxx", 'a', 'b', -1},95 {"long_b1_at_3000", string(make([]byte, 3000)) + "a" + string(make([]byte, 1000)), 'a', 'b', 3000},96 {"long_b2_at_end", string(make([]byte, 4000)) + "b", 'a', 'b', 4000},97 }9899 for _, tt := range tests {100 t.Run(tt.name, func(t *testing.T) {101 got := lastIndexByteTwo([]byte(tt.s), tt.b1, tt.b2)102 if got != tt.want {103 t.Errorf("lastIndexByteTwo(%q, %c, %c) = %d, want %d", tt.s[:min(len(tt.s), 40)], tt.b1, tt.b2, got, tt.want)104 }105 })106 }107108 // Exhaustive test against loop reference109 for n := 0; n <= 256; n++ {110 data := make([]byte, n)111 for i := range data {112 data[i] = byte('c' + (i % 20))113 }114 for pos := 0; pos < n; pos++ {115 for _, b := range []byte{'A', 'B'} {116 data[pos] = b117 got := lastIndexByteTwo(data, 'A', 'B')118 want := refLastIndexByteTwo(data, 'A', 'B')119 if got != want {120 t.Fatalf("lastIndexByteTwo(len=%d, match=%c@%d) = %d, want %d", n, b, pos, got, want)121 }122 data[pos] = byte('c' + (pos % 20))123 }124 }125 // No match126 got := lastIndexByteTwo(data, 'A', 'B')127 if got != -1 {128 t.Fatalf("lastIndexByteTwo(len=%d, no match) = %d, want -1", n, got)129 }130 // Both bytes present131 if n >= 2 {132 data[n/3] = 'A'133 data[n*2/3] = 'B'134 got := lastIndexByteTwo(data, 'A', 'B')135 want := refLastIndexByteTwo(data, 'A', 'B')136 if got != want {137 t.Fatalf("lastIndexByteTwo(len=%d, both@%d,%d) = %d, want %d", n, n/3, n*2/3, got, want)138 }139 data[n/3] = byte('c' + ((n / 3) % 20))140 data[n*2/3] = byte('c' + ((n * 2 / 3) % 20))141 }142 }143}144145func FuzzIndexByteTwo(f *testing.F) {146 f.Add([]byte("hello world"), byte('o'), byte('l'))147 f.Add([]byte(""), byte('a'), byte('b'))148 f.Add([]byte("aaa"), byte('a'), byte('a'))149 f.Fuzz(func(t *testing.T, data []byte, b1, b2 byte) {150 got := IndexByteTwo(data, b1, b2)151 want := loopIndexByteTwo(data, b1, b2)152 if got != want {153 t.Errorf("IndexByteTwo(len=%d, b1=%d, b2=%d) = %d, want %d", len(data), b1, b2, got, want)154 }155 })156}157158func FuzzLastIndexByteTwo(f *testing.F) {159 f.Add([]byte("hello world"), byte('o'), byte('l'))160 f.Add([]byte(""), byte('a'), byte('b'))161 f.Add([]byte("aaa"), byte('a'), byte('a'))162 f.Fuzz(func(t *testing.T, data []byte, b1, b2 byte) {163 got := lastIndexByteTwo(data, b1, b2)164 want := refLastIndexByteTwo(data, b1, b2)165 if got != want {166 t.Errorf("lastIndexByteTwo(len=%d, b1=%d, b2=%d) = %d, want %d", len(data), b1, b2, got, want)167 }168 })169}170171// Reference implementations for correctness checking172func refIndexByteTwo(s []byte, b1, b2 byte) int {173 i1 := bytes.IndexByte(s, b1)174 if i1 == 0 {175 return 0176 }177 scope := s178 if i1 > 0 {179 scope = s[:i1]180 }181 if i2 := bytes.IndexByte(scope, b2); i2 >= 0 {182 return i2183 }184 return i1185}186187func loopIndexByteTwo(s []byte, b1, b2 byte) int {188 for i, b := range s {189 if b == b1 || b == b2 {190 return i191 }192 }193 return -1194}195196func refLastIndexByteTwo(s []byte, b1, b2 byte) int {197 for i := len(s) - 1; i >= 0; i-- {198 if s[i] == b1 || s[i] == b2 {199 return i200 }201 }202 return -1203}204205func benchIndexByteTwo(b *testing.B, size int, pos int) {206 data := make([]byte, size)207 for i := range data {208 data[i] = byte('a' + (i % 20))209 }210 data[pos] = 'Z'211212 type impl struct {213 name string214 fn func([]byte, byte, byte) int215 }216 impls := []impl{217 {"asm", IndexByteTwo},218 {"2xIndexByte", refIndexByteTwo},219 {"loop", loopIndexByteTwo},220 }221 for _, im := range impls {222 b.Run(im.name, func(b *testing.B) {223 for i := 0; i < b.N; i++ {224 im.fn(data, 'Z', 'z')225 }226 })227 }228}229230func benchLastIndexByteTwo(b *testing.B, size int, pos int) {231 data := make([]byte, size)232 for i := range data {233 data[i] = byte('a' + (i % 20))234 }235 data[pos] = 'Z'236237 type impl struct {238 name string239 fn func([]byte, byte, byte) int240 }241 impls := []impl{242 {"asm", lastIndexByteTwo},243 {"loop", refLastIndexByteTwo},244 }245 for _, im := range impls {246 b.Run(im.name, func(b *testing.B) {247 for i := 0; i < b.N; i++ {248 im.fn(data, 'Z', 'z')249 }250 })251 }252}253254func BenchmarkIndexByteTwo_10(b *testing.B) { benchIndexByteTwo(b, 10, 8) }255func BenchmarkIndexByteTwo_100(b *testing.B) { benchIndexByteTwo(b, 100, 80) }256func BenchmarkIndexByteTwo_1000(b *testing.B) { benchIndexByteTwo(b, 1000, 800) }257func BenchmarkLastIndexByteTwo_10(b *testing.B) { benchLastIndexByteTwo(b, 10, 2) }258func BenchmarkLastIndexByteTwo_100(b *testing.B) { benchLastIndexByteTwo(b, 100, 20) }259func BenchmarkLastIndexByteTwo_1000(b *testing.B) { benchLastIndexByteTwo(b, 1000, 200) }