Skip to content

Commit

Permalink
fix round2_32, split round2 tests because they depend on sizeof int a…
Browse files Browse the repository at this point in the history
…t compile time (#1607)
  • Loading branch information
Duncaen authored Aug 24, 2023
1 parent 4b0e6c7 commit 6aea1e0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 30 deletions.
30 changes: 0 additions & 30 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"math"
"mime/multipart"
"net/http"
"net/http/httptest"
Expand All @@ -16,7 +15,6 @@ import (
"strings"
"testing"
"time"
"unsafe"

"github.com/valyala/bytebufferpool"
)
Expand Down Expand Up @@ -1968,34 +1966,6 @@ func testSetResponseBodyStreamChunked(t *testing.T, body string, trailer map[str
}
}

func TestRound2ForSliceCap(t *testing.T) {
t.Parallel()

testRound2ForSliceCap(t, 0, 0)
testRound2ForSliceCap(t, 1, 1)
testRound2ForSliceCap(t, 2, 2)
testRound2ForSliceCap(t, 3, 4)
testRound2ForSliceCap(t, 4, 4)
testRound2ForSliceCap(t, 5, 8)
testRound2ForSliceCap(t, 7, 8)
testRound2ForSliceCap(t, 8, 8)
testRound2ForSliceCap(t, 9, 16)
testRound2ForSliceCap(t, 0x10001, 0x20000)

if unsafe.Sizeof(int(0)) == 4 {
testRound2ForSliceCap(t, math.MaxInt32-1, math.MaxInt32)
} else {
testRound2ForSliceCap(t, math.MaxInt32, math.MaxInt32)
testRound2ForSliceCap(t, math.MaxInt64-1, math.MaxInt64-1)
}
}

func testRound2ForSliceCap(t *testing.T, n, expectedRound2 int) {
if roundUpForSliceCap(n) != expectedRound2 {
t.Fatalf("Unexpected round2(%d)=%d. Expected %d", n, roundUpForSliceCap(n), expectedRound2)
}
}

func TestRequestReadChunked(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 2 additions & 0 deletions round2_32.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package fasthttp

import "math"

func roundUpForSliceCap(n int) int {
if n <= 0 {
return 0
Expand Down
32 changes: 32 additions & 0 deletions round2_32_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build !amd64 && !arm64 && !ppc64 && !ppc64le && !s390x
// +build !amd64,!arm64,!ppc64,!ppc64le,!s390x

package fasthttp

import (
"math"
"testing"
)

func TestRound2ForSliceCap(t *testing.T) {
t.Parallel()

testRound2ForSliceCap(t, 0, 0)
testRound2ForSliceCap(t, 1, 1)
testRound2ForSliceCap(t, 2, 2)
testRound2ForSliceCap(t, 3, 4)
testRound2ForSliceCap(t, 4, 4)
testRound2ForSliceCap(t, 5, 8)
testRound2ForSliceCap(t, 7, 8)
testRound2ForSliceCap(t, 8, 8)
testRound2ForSliceCap(t, 9, 16)
testRound2ForSliceCap(t, 0x10001, 0x20000)

testRound2ForSliceCap(t, math.MaxInt32-1, math.MaxInt32-1)
}

func testRound2ForSliceCap(t *testing.T, n, expectedRound2 int) {
if roundUpForSliceCap(n) != expectedRound2 {
t.Fatalf("Unexpected round2(%d)=%d. Expected %d", n, roundUpForSliceCap(n), expectedRound2)
}
}
33 changes: 33 additions & 0 deletions round2_64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build amd64 || arm64 || ppc64 || ppc64le || s390x
// +build amd64 arm64 ppc64 ppc64le s390x

package fasthttp

import (
"math"
"testing"
)

func TestRound2ForSliceCap(t *testing.T) {
t.Parallel()

testRound2ForSliceCap(t, 0, 0)
testRound2ForSliceCap(t, 1, 1)
testRound2ForSliceCap(t, 2, 2)
testRound2ForSliceCap(t, 3, 4)
testRound2ForSliceCap(t, 4, 4)
testRound2ForSliceCap(t, 5, 8)
testRound2ForSliceCap(t, 7, 8)
testRound2ForSliceCap(t, 8, 8)
testRound2ForSliceCap(t, 9, 16)
testRound2ForSliceCap(t, 0x10001, 0x20000)

testRound2ForSliceCap(t, math.MaxInt32, math.MaxInt32)
testRound2ForSliceCap(t, math.MaxInt64-1, math.MaxInt64-1)
}

func testRound2ForSliceCap(t *testing.T, n, expectedRound2 int) {
if roundUpForSliceCap(n) != expectedRound2 {
t.Fatalf("Unexpected round2(%d)=%d. Expected %d", n, roundUpForSliceCap(n), expectedRound2)
}
}

0 comments on commit 6aea1e0

Please sign in to comment.