Skip to content

Commit

Permalink
test: add ut & bench test
Browse files Browse the repository at this point in the history
  • Loading branch information
welkeyever committed Jan 2, 2024
1 parent 4e52621 commit f4128fb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
20 changes: 20 additions & 0 deletions internal/bytesconv/bytesconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package bytesconv

import (
"net/url"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -203,3 +204,22 @@ func TestValidHeaderFieldValueTable(t *testing.T) {
assert.DeepEqual(t, expectedS, res)
}
}

func TestNewlineToSpaceTable(t *testing.T) {
t.Parallel()
// Test all characters
allBytes := make([]byte, 0)
for i := 0; i < 256; i++ {
allBytes = append(allBytes, byte(i))
}

var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ")

Check failure on line 216 in internal/bytesconv/bytesconv_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.18)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 216 in internal/bytesconv/bytesconv_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.19)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 216 in internal/bytesconv/bytesconv_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.20)

File is not `gofumpt`-ed (gofumpt)

expectedS := headerNewlineToSpace.Replace(string(allBytes))

for i := 0; i < len(allBytes); i++ {
allBytes[i] = NewlineToSpaceTable[allBytes[i]]
}

assert.DeepEqual(t, expectedS, string(allBytes))
}
29 changes: 29 additions & 0 deletions internal/bytesconv/bytesconv_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package bytesconv

import (
"strings"
"testing"

"golang.org/x/net/http/httpguts"
Expand Down Expand Up @@ -51,3 +52,31 @@ func BenchmarkValidHeaderFiledValueTableHertz(b *testing.B) {
}
}
}

func BenchmarkNewlineToSpace(b *testing.B) {
// Test all characters
allBytes := make([]byte, 0)
for i := 0; i < 256; i++ {
allBytes = append(allBytes, byte(i))
}
var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ")

Check failure on line 62 in internal/bytesconv/bytesconv_timing_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.18)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 62 in internal/bytesconv/bytesconv_timing_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.19)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 62 in internal/bytesconv/bytesconv_timing_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.20)

File is not `gofumpt`-ed (gofumpt)

for i := 0; i < b.N; i++ {
_ = headerNewlineToSpace.Replace(string(allBytes))
}

Check failure on line 67 in internal/bytesconv/bytesconv_timing_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.18)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 67 in internal/bytesconv/bytesconv_timing_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.19)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 67 in internal/bytesconv/bytesconv_timing_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.20)

File is not `gofumpt`-ed (gofumpt)
}

func BenchmarkNewlineToSpaceHertz01(b *testing.B) {
// Test all characters
allBytes := make([]byte, 0)
for i := 0; i < 256; i++ {
allBytes = append(allBytes, byte(i))
}

for i := 0; i < b.N; i++ {
for i := 0; i < len(allBytes); i++ {
allBytes[i] = NewlineToSpaceTable[allBytes[i]]
}
}
}

0 comments on commit f4128fb

Please sign in to comment.