-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First batch of UTs for the Float class
- Loading branch information
1 parent
23e9d3d
commit 57934b0
Showing
2 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package vm | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFloatClassSuperclass(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{`Float.class.name`, "Class"}, | ||
{`Float.superclass.name`, "Object"}, | ||
} | ||
|
||
for i, tt := range tests { | ||
v := initTestVM() | ||
evaluated := v.testEval(t, tt.input, getFilename()) | ||
checkExpected(t, i, evaluated, tt.expected) | ||
v.checkCFP(t, i, 0) | ||
v.checkSP(t, i, 1) | ||
} | ||
} | ||
|
||
func TestFloatArithmeticOperation(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected interface{} | ||
}{ | ||
{`'1'.to_f + '2'.to_f`, 3.0}, | ||
{`'10'.to_f + '0'.to_f`, 10.0}, | ||
{`'22'.to_f - '10'.to_f`, 12.0}, | ||
{`'2'.to_f - '10'.to_f`, -8.0}, | ||
{`'5'.to_f * '20'.to_f`, 100.0}, | ||
{`'4'.to_f % '2'.to_f`, 0.0}, | ||
{`'10'.to_f % '3'.to_f`, 1.0}, | ||
{`'6'.to_f % '4'.to_f`, 2.0}, | ||
{`'25'.to_f / '5'.to_f`, 5.0}, | ||
{`'5'.to_f ** '4'.to_f`, 625.0}, | ||
{`'25'.to_f / '5'.to_f`, 5.0}, | ||
{`'1'.to_f / '1'.to_f + '1'.to_f`, 2.0}, | ||
{`'0'.to_f / ('1'.to_f + '1000'.to_f)`, 0.0}, | ||
{`'5'.to_f ** ('3'.to_f * '2'.to_f) + '21'.to_f`, 15646.0}, | ||
{`('3'.to_f - '1'.to_f) ** '4'.to_f / '2'.to_f`, 8.0}, | ||
{`('25'.to_f / '5'.to_f + '5'.to_f) * '3'.to_f`, 30.0}, | ||
{`('25'.to_f / '5'.to_f + '5'.to_f) * '2'.to_f`, 20.0}, | ||
} | ||
|
||
for i, tt := range tests { | ||
v := initTestVM() | ||
evaluated := v.testEval(t, tt.input, getFilename()) | ||
checkExpected(t, i, evaluated, tt.expected) | ||
v.checkCFP(t, i, 0) | ||
v.checkSP(t, i, 1) | ||
} | ||
} | ||
|
||
func TestFloatArithmeticOperationFail(t *testing.T) { | ||
testsFail := []errorTestCase{ | ||
{`'1'.to_f + "p"`, "TypeError: Expect argument to be Float. got: String", 1}, | ||
{`'1'.to_f - "m"`, "TypeError: Expect argument to be Float. got: String", 1}, | ||
{`'1'.to_f ** "p"`, "TypeError: Expect argument to be Float. got: String", 1}, | ||
{`'1'.to_f / "t"`, "TypeError: Expect argument to be Float. got: String", 1}, | ||
} | ||
|
||
for i, tt := range testsFail { | ||
v := initTestVM() | ||
evaluated := v.testEval(t, tt.input, getFilename()) | ||
checkError(t, i, evaluated, tt.expected, getFilename(), tt.errorLine) | ||
v.checkCFP(t, i, 1) | ||
v.checkSP(t, i, 1) | ||
} | ||
} | ||
|
||
func TestFloatComparison(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected interface{} | ||
}{ | ||
{`'25'.to_f > '5'.to_f`, true}, | ||
{`'4'.to_f > '6'.to_f`, false}, | ||
{`'-5'.to_f < '-4'.to_f`, true}, | ||
{`'100'.to_f < '81'.to_f`, false}, | ||
{`'25'.to_f > '5'.to_f`, true}, | ||
{`'4'.to_f > '6'.to_f`, false}, | ||
{`'4'.to_f >= '4'.to_f`, true}, | ||
{`'2'.to_f >= '5'.to_f`, false}, | ||
{`'-5'.to_f < '-4'.to_f`, true}, | ||
{`'100'.to_f < '81'.to_f`, false}, | ||
{`'10'.to_f <= '10'.to_f`, true}, | ||
{`'10'.to_f <= '0'.to_f`, false}, | ||
{`'10'.to_f <=> '0'.to_f`, 1}, | ||
{`'1'.to_f <=> '2'.to_f`, -1}, | ||
{`'4'.to_f <=> '4'.to_f`, 0}, | ||
{`'123'.to_f == '123'.to_f`, true}, | ||
{`'123'.to_f == '124'.to_f`, false}, | ||
{`'123'.to_f == "'123'.to_f"`, false}, | ||
{`'123'.to_f == (1..3)`, false}, | ||
{`'123'.to_f == { a: '1'.to_f, b: '2'.to_f }`, false}, | ||
{`'123'.to_f == ['1'.to_f, "String", true, 2..5]`, false}, | ||
{`'123'.to_f == Float`, false}, | ||
{`'123'.to_f != '123'.to_f`, false}, | ||
{`'123'.to_f != '124'.to_f`, true}, | ||
{`'123'.to_f != "'123'.to_f"`, true}, | ||
{`'123'.to_f != (1..3)`, true}, | ||
{`'123'.to_f != { a: '1'.to_f, b: '2'.to_f }`, true}, | ||
{`'123'.to_f != ['1'.to_f, "String", true, 2..5]`, true}, | ||
{`'123'.to_f != Float`, true}, | ||
} | ||
|
||
for i, tt := range tests { | ||
v := initTestVM() | ||
evaluated := v.testEval(t, tt.input, getFilename()) | ||
checkExpected(t, i, evaluated, tt.expected) | ||
v.checkCFP(t, i, 0) | ||
v.checkSP(t, i, 1) | ||
} | ||
} | ||
|
||
func TestFloatComparisonFail(t *testing.T) { | ||
testsFail := []errorTestCase{ | ||
{`'1'.to_f > "m"`, "TypeError: Expect argument to be Float. got: String", 1}, | ||
{`'1'.to_f >= "m"`, "TypeError: Expect argument to be Float. got: String", 1}, | ||
{`'1'.to_f < "m"`, "TypeError: Expect argument to be Float. got: String", 1}, | ||
{`'1'.to_f <= "m"`, "TypeError: Expect argument to be Float. got: String", 1}, | ||
{`'1'.to_f <=> "m"`, "TypeError: Expect argument to be Float. got: String", 1}, | ||
} | ||
|
||
for i, tt := range testsFail { | ||
v := initTestVM() | ||
evaluated := v.testEval(t, tt.input, getFilename()) | ||
checkError(t, i, evaluated, tt.expected, getFilename(), tt.errorLine) | ||
v.checkCFP(t, i, 1) | ||
v.checkSP(t, i, 1) | ||
} | ||
} | ||
|
||
func TestFloatConversion(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected interface{} | ||
}{ | ||
{`'100'.to_f.to_i`, 100}, | ||
{`'100'.to_f.to_s`, "100"}, | ||
} | ||
|
||
for i, tt := range tests { | ||
v := initTestVM() | ||
evaluated := v.testEval(t, tt.input, getFilename()) | ||
checkExpected(t, i, evaluated, tt.expected) | ||
v.checkCFP(t, i, 0) | ||
v.checkSP(t, i, 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters