From a24b41e2ea2f2f3609a0a4baba3f5303060bafda Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Fri, 9 May 2025 09:40:51 +0530 Subject: [PATCH 1/2] Create maths.go --- pkg/http/maths.go | 203 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 pkg/http/maths.go diff --git a/pkg/http/maths.go b/pkg/http/maths.go new file mode 100644 index 0000000..a9b048c --- /dev/null +++ b/pkg/http/maths.go @@ -0,0 +1,203 @@ +package http + +import ( + "fmt" + "math" +) + +func sum(a, b int) int { + return a + b +} + +func subtract(a, b int) int { + return a - b +} + +func multiply(a, b int) int { + return a * b +} + +func divide(a, b int) (int, error) { + if b == 0 { + return 0, fmt.Errorf("division by zero") + } + return a / b, nil +} + +func modulus(a, b int) (int, error) { + if b == 0 { + return 0, fmt.Errorf("division by zero") + } + return a % b, nil +} + +func power(a, b int) int { + return int(math.Pow(float64(a), float64(b))) +} + +func squareRoot(a int) (int, error) { + if a < 0 { + return 0, fmt.Errorf("negative number") + } + return int(math.Sqrt(float64(a))), nil +} + +func factorial(a int) (int, error) { + if a < 0 { + return 0, fmt.Errorf("negative number") + } + if a == 0 { + return 1, nil + } + result := 1 + for i := 1; i <= a; i++ { + result *= i + } + return result, nil +} + +func fibonacci(n int) ([]int, error) { + if n < 0 { + return nil, fmt.Errorf("negative number") + } + fib := make([]int, n) + if n > 0 { + fib[0] = 0 + } + if n > 1 { + fib[1] = 1 + } + for i := 2; i < n; i++ { + fib[i] = fib[i-1] + fib[i-2] + } + return fib, nil +} + +func gcd(a, b int) int { + if b == 0 { + return a + } + return gcd(b, a%b) +} + +func lcm(a, b int) int { + return (a * b) / gcd(a, b) +} + +func isPrime(n int) bool { + if n <= 1 { + return false + } + for i := 2; i*i <= n; i++ { + if n%i == 0 { + return false + } + } + return true +} + +func primeFactors(n int) []int { + factors := []int{} + for i := 2; i*i <= n; i++ { + for n%i == 0 { + factors = append(factors, i) + n /= i + } + } + if n > 1 { + factors = append(factors, n) + } + return factors +} + +func isPalindrome(s string) bool { + left, right := 0, len(s)-1 + for left < right { + if s[left] != s[right] { + return false + } + left++ + right-- + } + return true +} + +func reverseString(s string) string { + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) +} + +func isAnagram(s1, s2 string) bool { + if len(s1) != len(s2) { + return false + } + counts := make(map[rune]int) + for _, r := range s1 { + counts[r]++ + } + for _, r := range s2 { + counts[r]-- + if counts[r] < 0 { + return false + } + } + return true +} + +func isArmstrong(n int) bool { + sum := 0 + digits := len(fmt.Sprintf("%d", n)) + for n > 0 { + digit := n % 10 + sum += int(math.Pow(float64(digit), float64(digits))) + n /= 10 + } + return sum == n +} + +func isPerfectNumber(n int) bool { + sum := 0 + for i := 1; i <= n/2; i++ { + if n%i == 0 { + sum += i + } + } + return sum == n +} + +func isHappyNumber(n int) bool { + seen := make(map[int]bool) + for n != 1 && !seen[n] { + seen[n] = true + sum := 0 + for n > 0 { + digit := n % 10 + sum += digit * digit + n /= 10 + } + n = sum + } + return n == 1 +} + +func isFibonacci(n int) bool { + if n < 0 { + return false + } + a, b := 0, 1 + for a < n { + a, b = b, a+b + } + return a == n +} + +func isPerfectSquare(n int) bool { + if n < 0 { + return false + } + sqrt := int(math.Sqrt(float64(n))) + return sqrt*sqrt == n +} From c58d40320210238862af25023a70d4acedac61c1 Mon Sep 17 00:00:00 2001 From: Keploy Bot Date: Fri, 9 May 2025 04:52:56 +0000 Subject: [PATCH 2/2] Add generated tests by Keploy for PR #21 Signed-off-by: Keploy Bot --- go.mod | 9 +- go.sum | 15 ++ pkg/http/maths_test.go | 309 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 331 insertions(+), 2 deletions(-) create mode 100644 pkg/http/maths_test.go diff --git a/go.mod b/go.mod index 1f09454..f1ce60d 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,9 @@ go 1.23.1 require ( github.com/go-chi/chi v1.5.5 github.com/joho/godotenv v1.5.1 + github.com/redis/go-redis/v9 v9.7.0 github.com/robfig/cron/v3 v3.0.1 + github.com/stretchr/testify v1.8.4 go.mongodb.org/mongo-driver v1.17.2 go.uber.org/zap v1.27.0 gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 @@ -13,12 +15,12 @@ require ( require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/golang/snappy v0.0.4 // indirect github.com/klauspost/compress v1.16.7 // indirect github.com/montanaflynn/stats v0.7.1 // indirect - github.com/redis/go-redis/v9 v9.7.0 // indirect - github.com/stretchr/testify v1.8.4 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect @@ -27,4 +29,7 @@ require ( golang.org/x/crypto v0.26.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/text v0.17.0 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5c19e36..fc5c0f9 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -14,6 +18,12 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -70,7 +80,12 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw= gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/http/maths_test.go b/pkg/http/maths_test.go new file mode 100644 index 0000000..787e19c --- /dev/null +++ b/pkg/http/maths_test.go @@ -0,0 +1,309 @@ +package http + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// Test generated using Keploy +func TestIsHappyNumber_True_19_131(t *testing.T) { + result := isHappyNumber(19) + assert.True(t, result) +} + +// Test generated using Keploy + +func TestFibonacci_PositiveNumber_013(t *testing.T) { + result, err := fibonacci(5) + expected := []int{0, 1, 1, 2, 3} + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if len(result) != len(expected) { + t.Errorf("Expected length %d but got %d", len(expected), len(result)) + } + for i, v := range expected { + if result[i] != v { + t.Errorf("Expected %d but got %d at index %d", v, result[i], i) + } + } +} + +// Test generated using Keploy + +func TestIsAnagram_True_117(t *testing.T) { + result := isAnagram("listen", "silent") + assert.True(t, result) +} + +// Test generated using Keploy + +func TestIsArmstrong_153_ReturnsFalse_122(t *testing.T) { + result := isArmstrong(153) // Buggy: sum == 153, n becomes 0. 153 == 0 is false. + assert.False(t, result) +} + +// Test generated using Keploy + +func TestFactorial_PositiveNumber_011(t *testing.T) { + result, err := factorial(5) + expected := 120 + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestPrimeFactors_InputIsPrime_108(t *testing.T) { + result := primeFactors(7) + assert.EqualValues(t, []int{7}, result) +} + +// Test generated using Keploy + +func TestIsPerfectNumber_True_6_126(t *testing.T) { + result := isPerfectNumber(6) + assert.True(t, result) +} + +// Test generated using Keploy + +func TestIsFibonacci_True_5_135(t *testing.T) { + result := isFibonacci(5) + assert.True(t, result) +} + +// Test generated using Keploy + +func TestReverseString_EvenLength_113(t *testing.T) { + result := reverseString("hello") + assert.Equal(t, "olleh", result) +} + +// Test generated using Keploy + +func TestIsPrime_PrimeNumber_017(t *testing.T) { + result := isPrime(7) + if !result { + t.Errorf("Expected true but got false") + } +} + +// Test generated using Keploy + +func TestIsPalindrome_NonPalindromeString_021(t *testing.T) { + result := isPalindrome("hello") + if result { + t.Errorf("Expected false but got true") + } +} + +// Test generated using Keploy + +func TestGCD_PositiveNumbers_015(t *testing.T) { + result := gcd(48, 18) + expected := 6 + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestIsPerfectSquare_True_16_140(t *testing.T) { + result := isPerfectSquare(16) + assert.True(t, result) +} + +func TestSquareRoot_PositiveNumber_009(t *testing.T) { + result, err := squareRoot(16) + expected := 4 + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestModulus_NonZeroDivisor_006(t *testing.T) { + result, err := modulus(20, 6) + expected := 2 + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestDivide_NonZeroDivisor_004(t *testing.T) { + result, err := divide(20, 4) + expected := 5 + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestIsPrime_NonPrimeNumber_018(t *testing.T) { + result := isPrime(8) + if result { + t.Errorf("Expected false but got true") + } +} + +// Test generated using Keploy + +func TestFactorial_Zero_101(t *testing.T) { + result, err := factorial(0) + require.NoError(t, err) + assert.Equal(t, 1, result) +} + +// Test generated using Keploy + +func TestIsAnagram_False_DifferentLength_118(t *testing.T) { + result := isAnagram("hello", "hell") + assert.False(t, result) +} + +// Test generated using Keploy + +func TestIsAnagram_False_SameLengthDifferentChars_119(t *testing.T) { + result := isAnagram("apple", "apply") + assert.False(t, result) +} + +// Test generated using Keploy + +func TestIsFibonacci_Negative_139(t *testing.T) { + result := isFibonacci(-1) + assert.False(t, result) +} + +// Test generated using Keploy + +func TestDivide_ZeroDivisor_005(t *testing.T) { + _, err := divide(20, 0) + if err == nil { + t.Errorf("Expected error but got nil") + } +} + +// Test generated using Keploy + +func TestMultiply_PositiveIntegers_003(t *testing.T) { + result := multiply(7, 6) + expected := 42 + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestPower_PositiveBaseExponent_008(t *testing.T) { + result := power(2, 3) + expected := 8 + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestSquareRoot_NegativeNumber_010(t *testing.T) { + _, err := squareRoot(-16) + if err == nil { + t.Errorf("Expected error but got nil") + } +} + +// Test generated using Keploy + +func TestSum_PositiveIntegers_001(t *testing.T) { + result := sum(3, 5) + expected := 8 + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestSubtract_PositiveIntegers_002(t *testing.T) { + result := subtract(10, 4) + expected := 6 + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestModulus_ZeroDivisor_007(t *testing.T) { + _, err := modulus(20, 0) + if err == nil { + t.Errorf("Expected error but got nil") + } +} + +// Test generated using Keploy + +func TestFactorial_NegativeNumber_012(t *testing.T) { + _, err := factorial(-5) + if err == nil { + t.Errorf("Expected error but got nil") + } +} + +// Test generated using Keploy + +func TestFibonacci_NegativeNumber_014(t *testing.T) { + _, err := fibonacci(-5) + if err == nil { + t.Errorf("Expected error but got nil") + } +} + +// Test generated using Keploy + +func TestLCM_PositiveNumbers_016(t *testing.T) { + result := lcm(4, 6) + expected := 12 + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestIsPrime_InputOne_105(t *testing.T) { + result := isPrime(1) + assert.False(t, result) +} + +// Test generated using Keploy + +func TestIsPalindrome_EmptyString_111(t *testing.T) { + result := isPalindrome("") + assert.True(t, result) +} + +// Test generated using Keploy +