Improvements
All checks were successful
Run Tests / test (push) Successful in 25s

This commit is contained in:
Lars M. Rogne 2024-04-24 22:04:34 +02:00
parent c73fa4e72b
commit 99eeeedc65
Signed by: Logiar
SSH Key Fingerprint: SHA256:tq77C31em1ZG4oELIHC3k62wq5UzPSXmhqH8g62whIY
10 changed files with 157 additions and 28 deletions

22
.github/workflows/run_tests.yaml vendored Normal file
View File

@ -0,0 +1,22 @@
name: Run Tests
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '^1.22.1'
- name: Run tests
run: go test ./...

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

1
README
View File

@ -1 +0,0 @@
Run with `go run collatz.go 1234` or download the exe and pass a number `collatz 1234`.

1
README.md Normal file
View File

@ -0,0 +1 @@
Run with `go run collatz.go 1234` or download the executable and pass a number `collatz 1234`.

View File

@ -2,10 +2,14 @@ package main
import ( import (
"collatz/collatz" "collatz/collatz"
"collatz/io" "collatz/parsing"
"log"
) )
func main() { func main() {
calculator := collatz.NewCalculator(new(io.Cliargs)) calculator := collatz.NewCalculator(new(parsing.Cliargs))
calculator.Collatz() err := calculator.CalculateCollatz()
if err != nil {
log.Fatalf("Error calculating Collatz: %v", err)
}
} }

View File

@ -1,53 +1,59 @@
package collatz package collatz
import ( import (
"collatz/io" "collatz/parsing"
"fmt" "fmt"
"log"
"math/big" "math/big"
) )
type Calculator struct { type Calculator struct {
parser *io.Cliargs parser parsing.Parser
three *big.Int three *big.Int
two *big.Int two *big.Int
one *big.Int one *big.Int
iterations int
} }
func NewCalculator(parser *io.Cliargs) Calculator { func NewCalculator(parser parsing.Parser) *Calculator {
r := Calculator{ r := &Calculator{
parser: parser, parser: parser,
three: new(big.Int).SetInt64(3), three: big.NewInt(3),
two: new(big.Int).SetInt64(2), two: big.NewInt(2),
one: new(big.Int).SetInt64(1), one: big.NewInt(1),
iterations: 0,
} }
return r return r
} }
func (o Calculator) Collatz() { func (o *Calculator) CalculateCollatz() error {
number, err := o.parser.Parse() number, err := o.parser.Parse()
if err != nil { if err != nil {
log.Panicf("Couldn't parse input: %s\n", err) return fmt.Errorf("couldn't parse input: %w", err)
} }
fmt.Print(number.Text(10)) fmt.Print(number.Text(10))
o.nextNumber(number) err = o.nextNumber(*number)
if err != nil {
return err
}
fmt.Println() fmt.Println()
fmt.Println("Iterations:", o.iterations)
return nil
} }
func (o Calculator) nextNumber(in *big.Int) { func (o *Calculator) nextNumber(in big.Int) error {
if in.Cmp(o.one) < 0 { if in.Cmp(o.one) < 0 {
log.Panicln("Must be greater than 0") return fmt.Errorf("must be greater than 0")
} }
if in.Cmp(o.one) == 0 { if in.Cmp(o.one) == 0 {
return return nil
} }
var newNumber *big.Int var newNumber *big.Int
if new(big.Int).And(in, o.one).Cmp(o.one) == 0 { if new(big.Int).And(&in, o.one).Cmp(o.one) == 0 {
newNumber = new(big.Int).Add(new(big.Int).Mul(in, o.three), o.one) newNumber = new(big.Int).Add(new(big.Int).Mul(&in, o.three), o.one)
} else { } else {
newNumber = new(big.Int).Div(in, o.two) newNumber = new(big.Int).Div(&in, o.two)
} }
o.iterations++
fmt.Printf(" -> %s", newNumber.Text(10)) fmt.Printf(" -> %s", newNumber.Text(10))
o.nextNumber(newNumber) return o.nextNumber(*newNumber)
} }

View File

@ -0,0 +1,45 @@
package collatz
import (
"math/big"
"testing"
)
type MockParser struct {
value *big.Int
}
func (m *MockParser) Parse() (*big.Int, error) {
return m.value, nil
}
func TestNewCalculator(t *testing.T) {
parser := &MockParser{}
calculator := NewCalculator(parser)
if calculator.parser != parser {
t.Errorf("Expected parser to be %v, got %v", parser, calculator.parser)
}
if calculator.one.Cmp(big.NewInt(1)) != 0 {
t.Errorf("Expected one to be 1, got %v", calculator.one)
}
if calculator.two.Cmp(big.NewInt(2)) != 0 {
t.Errorf("Expected two to be 2, got %v", calculator.two)
}
if calculator.three.Cmp(big.NewInt(3)) != 0 {
t.Errorf("Expected three to be 3, got %v", calculator.three)
}
}
func TestCalculateCollatz(t *testing.T) {
parser := &MockParser{value: big.NewInt(6)}
calculator := NewCalculator(parser)
err := calculator.CalculateCollatz()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}

View File

@ -1,4 +1,4 @@
package io package parsing
import ( import (
"errors" "errors"

44
parsing/cliargs_test.go Normal file
View File

@ -0,0 +1,44 @@
package parsing
import (
"math/big"
"os"
"testing"
)
func TestCliargs_Parse(t *testing.T) {
tests := []struct {
name string
args []string
want *big.Int
wantErr bool
}{
{
name: "Test with valid integer",
args: []string{"", "10"}, // the first argument is the program name
want: big.NewInt(10),
wantErr: false,
},
{
name: "Test with non-integer",
args: []string{"", "abc"}, // the first argument is the program name
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
parser := &Cliargs{}
got, err := parser.Parse()
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got.Cmp(tt.want) != 0 {
t.Errorf("Parse() = %v, want %v", got, tt.want)
}
})
}
}

7
parsing/interface.go Normal file
View File

@ -0,0 +1,7 @@
package parsing
import "math/big"
type Parser interface {
Parse() (*big.Int, error)
}