Skip to content

Commit

Permalink
feat(gcd): make gcd solution a function instead of a program
Browse files Browse the repository at this point in the history
- update tests accordingly
  • Loading branch information
nprimo authored and HarryVasanth committed Jun 27, 2024
1 parent b4a1739 commit 52b72ff
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
12 changes: 12 additions & 0 deletions solutions/gcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package solutions

func Gcd(a, b uint) uint {
for a != b {
if a > b {
a -= b
} else {
b -= a
}
}
return a
}
26 changes: 0 additions & 26 deletions solutions/gcd/main.go

This file was deleted.

44 changes: 23 additions & 21 deletions tests/gcd_test/main.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package main

import (
"strconv"

"github.com/01-edu/go-tests/lib/challenge"
"github.com/01-edu/go-tests/lib/random"
"fmt"
"os"
student "student"
)

func main() {
args := [][]string{
{"42", "10"},
{"42", "12"},
{"14", "77"},
{"17", "3"},
{"23"},
{"12", "23"},
{"25", "15"},
{"23043", "122"},
{"11", "77"},
}
for i := 0; i < 5; i++ {
a := strconv.Itoa(random.IntBetween(1, 100000))
b := strconv.Itoa(random.IntBetween(1, 100))
args = append(args, []string{a, b})
testCases := []struct {
a uint
b uint
want uint
}{
{42, 10, 2},
{42, 12, 6},
{14, 77, 7},
{17, 3, 1},
{12, 23, 1},
{25, 15, 5},
{23043, 122, 1},
{11, 77, 11},
}
for _, v := range args {
challenge.Program("gcd", v...)

for _, tc := range testCases {
got := student.Gcd(tc.a, tc.b)
if got != tc.want {
fmt.Printf("Gcd(%d, %d) = %d instead of %d\n", tc.a, tc.b, got, tc.want)
os.Exit(1)
}
}
}

0 comments on commit 52b72ff

Please sign in to comment.