Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fraction 테스트 추가 #8

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions core/math/fraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestFraction_Add(t *testing.T) {
{"1/-2 + 1/-3", 1, -2, 1, -3, -5, 6},
{"2/1 + 15/7", 2, 1, 15, 7, 29, 7},
{"2/1 + -15/7", 2, 1, -15, 7, -1, 7},
{"1000000/1000001 + 1000000/1000001", 1000000, 1000001, 1000000, 1000001, 2000000, 1000001},
}

for _, tt := range tests {
Expand Down Expand Up @@ -84,6 +85,7 @@ func TestFraction_Sub(t *testing.T) {
{-1, 2, 1, 3, -5, 6},
{0, 1, 16, 2, 8, -1},
{1, 2, 1, 2, 0, 1},
{1000000, 1000001, 1, 1000001, 999999, 1000001},
}

for _, tt := range tests {
Expand All @@ -102,19 +104,56 @@ func TestFraction_Sub(t *testing.T) {
}

func TestFraction_Mul(t *testing.T) {
t.Parallel()
tests := []struct {
numerator1, denominator1 int64
numerator2, denominator2 int64
expectedNumerator, expectedDenominator int64
shouldPanic bool
}{
{1, 2, 1, 3, 1, 6},
{-100, 10, 256, -10, 256, 1},
{1, 2, 1, 3, 1, 6, false},
{-100, 10, 256, -10, 256, 1, false},
{0, 1, 1, 1, 0, 1, false},
{1, 2, 0, 1, 0, 1, false},
{1, 2, 1, 0, 0, 0, true},
{1, 2, -1, 2, -1, 4, false},
{-1, 2, 1, 2, -1, 4, false},
{1, 3, 1, 2, 1, 6, false},
{2, 3, 3, 2, 1, 1, false},
{2, 3, -3, 2, -1, 1, false},
}

for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if tt.shouldPanic {
return
}
t.Fatalf("Mul: unexpected panic: %v", r)
}
}()
fraction1 := NewFraction(tt.numerator1, tt.denominator1)

defer func() {
if r := recover(); r != nil {
if tt.shouldPanic {
return
}
t.Fatalf("Mul: unexpected panic: %v", r)
}
}()
fraction2 := NewFraction(tt.numerator2, tt.denominator2)

defer func() {
if r := recover(); r != nil {
if tt.shouldPanic {
return
}
t.Fatalf("Mul: unexpected panic: %v", r)
}
}()
result := fraction1.Mul(fraction2)
expected := NewFraction(tt.expectedNumerator, tt.expectedDenominator)

Expand All @@ -126,24 +165,41 @@ func TestFraction_Mul(t *testing.T) {
}

func TestFraction_Div(t *testing.T) {
t.Parallel()
tests := []struct {
numerator1, denominator1 int64
numerator2, denominator2 int64
expectedNumerator, expectedDenominator int64
expectedError bool
}{
{1, 2, 1, 3, 3, 2},
{-100, 10, 256, -10, 100, 256},
{1, 2, 1, 3, 3, 2, false},
{-100, 10, 256, -10, 100, 256, false},
{0, 1, 1, 1, 0, 1, false},
{1, 2, 1, 0, 0, 0, true},
{-1, 2, 1, 2, -1, 1, false},
{1, 3, 1, 2, 2, 3, false},
{2, 3, 3, 2, 4, 9, false},
}

for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if tt.denominator2 == 0 {
tolelom marked this conversation as resolved.
Show resolved Hide resolved
if !tt.expectedError {
t.Fatalf("Div: unexpected panic: %v", r)
}
}
}
}()
fraction1 := NewFraction(tt.numerator1, tt.denominator1)
fraction2 := NewFraction(tt.numerator2, tt.denominator2)
result := fraction1.Div(fraction2)
expected := NewFraction(tt.expectedNumerator, tt.expectedDenominator)

if result.Numerator.Cmp(expected.Numerator) != 0 || result.Denominator.Cmp(expected.Denominator) != 0 {
t.Fatalf("Div: expected %v, got %v", expected, result)
t.Fatalf("Div: expected %v/%v, got %v/%v", expected.Numerator, expected.Denominator, result.Numerator, result.Denominator)
}
})
}
Expand Down
Loading