From c356e3576a8be18a6e7124886db7e70a5792cc80 Mon Sep 17 00:00:00 2001 From: FluxCapacitor2 <31071265+FluxCapacitor2@users.noreply.github.com> Date: Sun, 21 Jul 2024 01:33:51 -0400 Subject: [PATCH] Fix test and number underflow --- app/server/pagination.go | 4 ++-- app/server/pagination_test.go | 6 +++--- app/server/server.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/server/pagination.go b/app/server/pagination.go index fbdab6e..0d80247 100644 --- a/app/server/pagination.go +++ b/app/server/pagination.go @@ -19,7 +19,7 @@ func urlWithParam(url *url.URL, key string, value string) string { return url.String() } -func createPagination(url *url.URL, page uint32, pageCount uint32) []resultsPage { +func createPagination(url *url.URL, page int32, pageCount int32) []resultsPage { pages := make([]resultsPage, 0, int(math.Min(12, float64(pageCount+4)))) if pageCount == 1 { @@ -40,8 +40,8 @@ func createPagination(url *url.URL, page uint32, pageCount uint32) []resultsPage }) startIndex := page - 5 - endIndex := page + 5 + if startIndex < 0 { startIndex = 0 } diff --git a/app/server/pagination_test.go b/app/server/pagination_test.go index 6f6568d..df917bb 100644 --- a/app/server/pagination_test.go +++ b/app/server/pagination_test.go @@ -15,8 +15,8 @@ func TestPagination(t *testing.T) { } table := []struct { - page int - pageCount int + page int32 + pageCount int32 want []resultsPage }{ // When the cursor is at the start @@ -78,7 +78,7 @@ func TestPagination(t *testing.T) { for i, testCase := range table { actual := createPagination(url, testCase.page, testCase.pageCount) if !reflect.DeepEqual(testCase.want, actual) { - t.Fatalf("test case %v failed: wanted %v, got %+v", i+1, testCase.want, actual) + t.Fatalf("test case %v failed: wanted %+v, got %+v", i+1, testCase.want, actual) } } } diff --git a/app/server/server.go b/app/server/server.go index 7047dbe..be0061e 100644 --- a/app/server/server.go +++ b/app/server/server.go @@ -189,11 +189,11 @@ func renderTemplateWithResults(db database.Database, config *config.Config, req } ceil := math.Ceil(float64(*total) / 10.0) - pageCount := uint32(math.Min(ceil, math.MaxUint32)) + pageCount := int32(math.Min(ceil, math.MaxInt32)) if pageCount < 1 { pageCount = 1 } - pages := createPagination(req.URL, uint32(page), pageCount) + pages := createPagination(req.URL, int32(page), pageCount) mappedResults := make([]searchResult, len(results)) for i, res := range results {