gocollatz/collatz/calculator_test.go

49 lines
1.0 KiB
Go
Raw Normal View History

2024-04-24 20:04:34 +00:00
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)
2024-04-25 02:41:39 +00:00
sequence, err := calculator.CalculateCollatz()
2024-04-24 20:04:34 +00:00
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
2024-04-25 02:41:39 +00:00
if sequence.Iterations != 8 {
t.Errorf("Expected 8 iterations, got %v", sequence.Iterations)
}
2024-04-24 20:04:34 +00:00
}