Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix round2_32 #1607

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}