From 899443d9eb08a3c37dab1d936fbb6954611c7761 Mon Sep 17 00:00:00 2001 From: Kshitij Sharma <132280155+kshitij412@users.noreply.github.com> Date: Thu, 8 May 2025 08:56:59 +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 f1847d0b6e3f34261c63c0dd61d2d953a0533b5c Mon Sep 17 00:00:00 2001 From: Keploy Bot Date: Thu, 8 May 2025 03:41:04 +0000 Subject: [PATCH 2/2] Add generated tests by Keploy for PR #20 Signed-off-by: Keploy Bot --- go.mod | 4 +- go.sum | 15 ++ pkg/http/maths_test.go | 311 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 pkg/http/maths_test.go diff --git a/go.mod b/go.mod index 1f09454..55ab096 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ 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 go.mongodb.org/mongo-driver v1.17.2 go.uber.org/zap v1.27.0 @@ -17,7 +18,6 @@ require ( 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/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.2 // indirect @@ -27,4 +27,6 @@ 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 ) 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..eb85262 --- /dev/null +++ b/pkg/http/maths_test.go @@ -0,0 +1,311 @@ +package http + +import ( + "testing" +) + +// Test generated using Keploy +func TestIsHappyNumber_HappyNumber_555(t *testing.T) { + result := isHappyNumber(19) + if !result { + t.Errorf("Expected true, but got false") + } +} + +// Test generated using Keploy + +func TestFibonacci_ValidInput_888(t *testing.T) { + result, err := fibonacci(7) + expected := []int{0, 1, 1, 2, 3, 5, 8} + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + for i, v := range expected { + if result[i] != v { + t.Errorf("Expected %d at index %d, but got %d", v, i, result[i]) + } + } +} + +// Test generated using Keploy + +func TestPrimeFactors_ValidInput_505(t *testing.T) { + result := primeFactors(28) + expected := []int{2, 2, 7} + for i, v := range expected { + if result[i] != v { + t.Errorf("Expected %d at index %d, but got %d", v, i, result[i]) + } + } +} + +// Test generated using Keploy + +func TestIsAnagram_ValidAnagramStrings_909(t *testing.T) { + result := isAnagram("listen", "silent") + if !result { + t.Errorf("Expected true, but got false") + } +} + +// Test generated using Keploy + +func TestIsArmstrong_NonArmstrongNumber_222(t *testing.T) { + result := isArmstrong(123) + if result { + t.Errorf("Expected false, but got true") + } +} + +// Test generated using Keploy + +func TestFactorial_PositiveInteger_555(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 TestIsPalindrome_PalindromeString_606(t *testing.T) { + result := isPalindrome("racecar") + if !result { + t.Errorf("Expected true, but got false") + } +} + +// Test generated using Keploy + +func TestIsPerfectNumber_PerfectNumber_333(t *testing.T) { + result := isPerfectNumber(28) + if !result { + t.Errorf("Expected true, but got false") + } +} + +// Test generated using Keploy + +func TestIsFibonacci_FibonacciNumber_777(t *testing.T) { + result := isFibonacci(8) + if !result { + t.Errorf("Expected true, but got false") + } +} + +// Test generated using Keploy + +func TestIsPrime_PrimeNumber_303(t *testing.T) { + result := isPrime(7) + if !result { + t.Errorf("Expected true, but got false") + } +} + +// Test generated using Keploy + +func TestReverseString_ValidString_808(t *testing.T) { + result := reverseString("hello") + expected := "olleh" + if result != expected { + t.Errorf("Expected %s, but got %s", expected, result) + } +} + +// Test generated using Keploy + +func TestGCD_ValidInputs_101(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_PerfectSquare_999(t *testing.T) { + result := isPerfectSquare(16) + if !result { + t.Errorf("Expected true, but got false") + } +} + +func TestSquareRoot_PositiveInteger_333(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 TestDivide_ValidInputs_321(t *testing.T) { + result, err := divide(10, 2) + 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 TestModulus_ValidInputs_987(t *testing.T) { + result, err := modulus(10, 3) + expected := 1 + 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 TestIsAnagram_NonAnagramStrings_010(t *testing.T) { + result := isAnagram("hello", "world") + if result { + t.Errorf("Expected false, but got true") + } +} + +// Test generated using Keploy + +func TestFactorial_NegativeInteger_666(t *testing.T) { + _, err := factorial(-5) + if err == nil { + t.Errorf("Expected error, but got nil") + } +} + +// Test generated using Keploy + +func TestMultiply_PositiveIntegers_789(t *testing.T) { + result := multiply(3, 7) + expected := 21 + if result != expected { + t.Errorf("Expected %d, but got %d", expected, result) + } +} + +// Test generated using Keploy + +func TestDivide_DivisionByZero_654(t *testing.T) { + _, err := divide(10, 0) + if err == nil { + t.Errorf("Expected error, but got nil") + } +} + +// Test generated using Keploy + +func TestModulus_DivisionByZero_111(t *testing.T) { + _, err := modulus(10, 0) + if err == nil { + t.Errorf("Expected error, but got nil") + } +} + +// Test generated using Keploy + +func TestPower_PositiveIntegers_222(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 TestSum_PositiveIntegers_123(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_456(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 TestFactorial_Zero_777(t *testing.T) { + result, err := factorial(0) + expected := 1 + 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 TestFibonacci_NegativeInput_999(t *testing.T) { + _, err := fibonacci(-5) + if err == nil { + t.Errorf("Expected error, but got nil") + } +} + +// Test generated using Keploy + +func TestIsPrime_NonPrimeNumber_404(t *testing.T) { + result := isPrime(8) + if result { + t.Errorf("Expected false, but got true") + } +} + +// Test generated using Keploy + +func TestSquareRoot_NegativeInteger_444(t *testing.T) { + _, err := squareRoot(-16) + if err == nil { + t.Errorf("Expected error, but got nil") + } +} + +// Test generated using Keploy + +func TestLCM_ValidInputs_202(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 TestIsPalindrome_NonPalindromeString_707(t *testing.T) { + result := isPalindrome("hello") + if result { + t.Errorf("Expected false, but got true") + } +} + +// Test generated using Keploy +