diff --git a/interpreter/eclaType/Lib_test.go b/interpreter/eclaType/Lib_test.go new file mode 100644 index 0000000..c8a3885 --- /dev/null +++ b/interpreter/eclaType/Lib_test.go @@ -0,0 +1,268 @@ +package eclaType + +import ( + "github.com/Eclalang/Ecla/interpreter/utils" + "testing" +) + +func TestNewLib(t *testing.T) { + t1 := NewLib("console") + t2 := &Lib{"console"} + + if *t1 != *t2 { + t.Error("error when creating Lib") + } +} + +func TestLibGetValue(t *testing.T) { + t1 := NewLib("console") + result := t1.GetValue() + + if t1 != result { + t.Error("error when getting value of Lib") + } +} + +func TestLibSetValue(t *testing.T) { + t1 := NewLib("console") + result := t1.SetValue("test") + + if result == nil { + t.Error("expected error when setting value of Lib") + } +} + +func TestLibString(t *testing.T) { + t1 := NewLib("console") + result := t1.String() + + if result != "console" { + t.Error("expected \"console\", got " + result) + } +} + +func TestLibGetString(t *testing.T) { + t1 := NewLib("console") + t2 := String("console") + result := t1.GetString() + + if result != t2 { + t.Error("expected " + t2 + ", got " + result) + } +} + +func TestLibGetType(t *testing.T) { + t1 := NewLib("console") + t2 := "lib" + result := t1.GetType() + + if result != t2 { + t.Error("expected \"lib\", got " + result) + } +} + +func TestLibGetIndex(t *testing.T) { + t1 := NewLib("console") + _, err := t1.GetIndex(Int(0)) + + if err == nil { + t.Error("expected error when getting index of Lib") + } +} + +func TestLibAdd(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Add(t2) + + if err == nil { + t.Error("expected error when adding to a lib") + } +} + +func TestLibSub(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Sub(t2) + + if err == nil { + t.Error("expected error when subtracting from a lib") + } +} + +func TestLibMul(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Mul(t2) + + if err == nil { + t.Error("expected error when multiplying with a lib") + } +} + +func TestLibDiv(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Div(t2) + + if err == nil { + t.Error("expected error when dividing a lib") + } +} + +func TestLibMod(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Mod(t2) + + if err == nil { + t.Error("expected error when getting remainder of a lib") + } +} + +func TestLibDivEc(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.DivEc(t2) + + if err == nil { + t.Error("expected error when getting quotient of a lib") + } +} + +func TestLibEq(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Eq(t2) + + if err == nil { + t.Error("expected error when comparing a lib") + } +} + +func TestLibNotEq(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.NotEq(t2) + + if err == nil { + t.Error("expected error when comparing a lib") + } +} + +func TestLibGt(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Gt(t2) + + if err == nil { + t.Error("expected error when comparing a lib") + } +} + +func TestLibGtEq(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.GtEq(t2) + + if err == nil { + t.Error("expected error when comparing a lib") + } +} + +func TestLibLw(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Lw(t2) + + if err == nil { + t.Error("expected error when comparing a lib") + } +} + +func TestLibLwEq(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.LwEq(t2) + + if err == nil { + t.Error("expected error when comparing a lib") + } +} + +func TestLibAnd(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.And(t2) + + if err == nil { + t.Error("expected error when comparing a lib") + } +} + +func TestLibOr(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Or(t2) + + if err == nil { + t.Error("expected error when comparing a lib") + } +} + +func TestLibNot(t *testing.T) { + t1 := NewLib("console") + _, err := t1.Not() + + if err == nil { + t.Error("expected error when getting \"not\" of a lib") + } +} + +func TestLibXor(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Xor(t2) + + if err == nil { + t.Error("expected error when comparing a lib") + } +} + +func TestLibIsNull(t *testing.T) { + t1 := NewLib("console") + result := t1.IsNull() + + if result { + t.Error("expected false, got true") + } +} + +func TestLibAppend(t *testing.T) { + t1 := NewLib("console") + t2 := NewLib("test") + _, err := t1.Append(t2) + + if err == nil { + t.Error("expected error when appending to a lib") + } +} + +func TestLibGetSize(t *testing.T) { + t1 := NewLib("console") + t2 := utils.Sizeof(t1) + result := t1.GetSize() + + if result != t2 { + t.Errorf("expected %d, got %d", t2, result) + } +} + +func TestLibLen(t *testing.T) { + t1 := NewLib("console") + _, err := t1.Len() + + if err == nil { + t.Error("expected error when getting length of a lib") + } +} diff --git a/interpreter/eclaType/any.go b/interpreter/eclaType/any.go index 2628f17..801ebd9 100644 --- a/interpreter/eclaType/any.go +++ b/interpreter/eclaType/any.go @@ -2,7 +2,7 @@ package eclaType type Any struct { Value Type - Type string + Type string //actually the type of Value } func (a *Any) String() string { diff --git a/interpreter/eclaType/any_test.go b/interpreter/eclaType/any_test.go new file mode 100644 index 0000000..8929b26 --- /dev/null +++ b/interpreter/eclaType/any_test.go @@ -0,0 +1,392 @@ +package eclaType + +import ( + "github.com/Eclalang/Ecla/parser" + "testing" +) + +func TestNewAny(t *testing.T) { + t1 := NewAny(Bool(true)) + expected := &Any{Bool(true), parser.Bool} + + if t1.GetValue() != expected.GetValue() { + t.Error("expected \"true\", got ", t1) + } +} + +func TestNewEmptyAny(t *testing.T) { + t1, _ := NewAnyEmpty() + expected := &Any{Value: NewNull()} + + if t1.GetValue() != expected.GetValue() { + t.Error("expected \"true\", got ", t1) + } +} + +func TestAnyString(t *testing.T) { + t1 := NewAny(Bool(true)) + expected := "true" + result := t1.String() + + if result != expected { + t.Error("expected \"true\", got ", result) + } +} + +func TestAnyGetString(t *testing.T) { + t1 := NewAny(Bool(true)) + expected, _ := NewString("true") + result := t1.GetString() + + if result != expected { + t.Error("expected \"true\", got ", result) + } +} + +func TestAnyGetValue(t *testing.T) { + result := Bool(true) + t1 := NewAny(result) + + if result != t1.GetValue() { + t.Error("expected ", t1, ", got ", result) + } +} + +func TestAnySetValue(t *testing.T) { + t2, _ := NewList("[]int") + t1 := NewAny(t2) + var values []Type + values = append(values, Int(0)) + err := t1.SetValue(values) + + if err != nil { + t.Error(err) + } +} + +func TestAnySetAny(t *testing.T) { + t1 := NewAny(Bool(true)) + t2 := NewAny(Int(0)) + + err := t1.SetAny(Int(0)) + if err != nil { + t.Error(err) + } + + if t1.GetValue() != t2.GetValue() { + t.Error("expected ", t2, ", got ", t1) + } +} + +func TestGetIndexAnyInt(t *testing.T) { + t1 := NewAny(String("123")) + t2 := Int(0) + + result, err := t1.GetIndex(t2) + if err != nil { + t.Error(err) + } + if (*result).GetValue() != Char('1') { + t.Error("Expected \"1\", got ", result) + } +} + +func TestAddAnyAsInt(t *testing.T) { + t1 := NewAny(Int(1)) + t2 := NewAny(Int(2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(3) { + t.Error("Expected 3, got ", result) + } +} + +func TestSubAnyAsInt(t *testing.T) { + t1 := NewAny(Int(1)) + t2 := NewAny(Int(2)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(-1) { + t.Error("Expected -1, got ", result) + } +} + +func TestMulAnyAsInt(t *testing.T) { + t1 := NewAny(Int(3)) + t2 := NewAny(Int(2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(6) { + t.Error("Expected 6, got ", result) + } +} + +func TestDivAnyAsInt(t *testing.T) { + t1 := NewAny(Int(1)) + t2 := NewAny(Int(2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(0.5) { + t.Error("Expected 0.5, got ", result) + } +} + +func TestModAnyAsInt(t *testing.T) { + t1 := NewAny(Int(51)) + t2 := NewAny(Int(2)) + + result, err := t1.Mod(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestDivEcAnyAsInt(t *testing.T) { + t1 := NewAny(Int(9)) + t2 := NewAny(Int(2)) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(4) { + t.Error("Expected 4, got ", result) + } +} + +func TestEqAnyAsInt(t *testing.T) { + t1 := NewAny(Int(1)) + t2 := NewAny(Int(2)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqAnyAsInt(t *testing.T) { + t1 := NewAny(Int(1)) + t2 := NewAny(Int(2)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtAnyAsInt(t *testing.T) { + t1 := NewAny(Int(1)) + t2 := NewAny(Int(2)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqAnyAsInt(t *testing.T) { + t1 := NewAny(Int(1)) + t2 := NewAny(Int(2)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwAnyAsInt(t *testing.T) { + t1 := NewAny(Int(1)) + t2 := NewAny(Int(2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqAnyAsInt(t *testing.T) { + t1 := NewAny(Int(1)) + t2 := NewAny(Int(2)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndAny(t *testing.T) { + t1 := NewAny(Bool(true)) + t2 := NewAny(Bool(false)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrAny(t *testing.T) { + t1 := NewAny(Bool(true)) + t2 := NewAny(Bool(false)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorAny(t *testing.T) { + t1 := NewAny(Bool(true)) + t2 := NewAny(Bool(false)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotAny(t *testing.T) { + t1 := NewAny(Bool(false)) + + result, err := t1.Not() + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestIncrementAny(t *testing.T) { + t1 := NewAny(Int(1)) + + t1.Increment() + if t1.GetValue() != Int(2) { + t.Error("Expected 2, got ", t1) + } +} + +func TestDecrementAny(t *testing.T) { + t1 := NewAny(Int(1)) + + t1.Decrement() + if t1.GetValue() != Int(0) { + t.Error("Expected 0, got ", t1) + } +} + +func TestAppendAny(t *testing.T) { + t1 := NewAny(String("123")) + t2 := NewAny(String("4")) + + result, err := t1.Append(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != String("1234") { + t.Error("Expected \"1234\", got ", result) + } +} + +func TestIsNullAny(t *testing.T) { + t1, _ := NewAnyEmpty() + + result := t1.IsNull() + if !result { + t.Error("Expected \"true\", got ", result) + } +} + +func TestGetFunctionAny(t *testing.T) { + expected := NewFunction("test", []parser.FunctionParams{}, []parser.Node{}, []string{"int"}) + t1 := NewAny(expected) + + result := t1.GetFunction() + if result != expected { + t.Error("Expected ", expected, ", got ", result) + } +} + +//TODO @Sanegv investigate if you put a safety to prevent any of any +//func TestGetFunctionAnyAny(t *testing.T) { +// expected := NewFunction("test", []parser.FunctionParams{}, []parser.Node{}, []string{"int"}) +// a := NewAny(expected) +// t1 := NewAny(a) +// +// result := t1.GetFunction() +// if result != expected { +// t.Error("Expected ", expected, ", got ", result) +// } +//} + +func TestGetFunctionAnyNil(t *testing.T) { + t1, _ := NewAnyEmpty() + + result := t1.GetFunction() + if result != nil { + t.Error("Expected \"nil\", got ", result) + } +} + +func TestGetSizeAny(t *testing.T) { + t1 := NewAny(Int(0)) + expected := Int(0).GetSize() + result := t1.GetSize() + if result != expected { + t.Error("Expected ", expected, ", got ", result) + } +} + +func TestAnyLen(t *testing.T) { + t1 := NewAny(String("test")) + expected := 4 + + result, err := t1.Len() + if err != nil { + t.Error(err) + } + if result != expected { + t.Errorf("expected %d, got %d", expected, result) + } +} diff --git a/interpreter/eclaType/bool_test.go b/interpreter/eclaType/bool_test.go index d76260e..ad8a3fa 100644 --- a/interpreter/eclaType/bool_test.go +++ b/interpreter/eclaType/bool_test.go @@ -1,11 +1,34 @@ package eclaType import ( + "github.com/Eclalang/Ecla/interpreter/utils" "testing" ) // Bool interacts with Bool +func TestNewBool(t *testing.T) { + _, err := NewBool("true") + if err != nil { + t.Errorf("Error: %s", err) + } + + _, err = NewBool("not a bool") + if err == nil { + t.Errorf("Expected error, got nil") + } +} + +func TestBoolGetSize(t *testing.T) { + t1 := Bool(true) + expected := utils.Sizeof(t1) + + result := t1.GetSize() + if result != utils.Sizeof(t1) { + t.Errorf("expected %d, got %d", expected, result) + } +} + func TestEqBools(t *testing.T) { t1 := Bool(true) t2 := Bool(true) @@ -19,6 +42,19 @@ func TestEqBools(t *testing.T) { } } +func TestEqBoolsFalse(t *testing.T) { + t1 := Bool(true) + t2 := Bool(false) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + func TestNotEqBools(t *testing.T) { t1 := Bool(true) t2 := Bool(false) @@ -32,6 +68,19 @@ func TestNotEqBools(t *testing.T) { } } +func TestNotEqBoolsFalse(t *testing.T) { + t1 := Bool(true) + t2 := Bool(true) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + func TestAndBools(t *testing.T) { t1 := Bool(true) t2 := Bool(true) @@ -45,7 +94,59 @@ func TestAndBools(t *testing.T) { } } +func TestAndBoolsFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolsFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := Bool(false) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolsFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Bool(false) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + func TestOrBools(t *testing.T) { + t1 := Bool(true) + t2 := Bool(true) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolsFalseLeft(t *testing.T) { t1 := Bool(false) t2 := Bool(true) @@ -58,6 +159,84 @@ func TestOrBools(t *testing.T) { } } +func TestOrBoolsFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := Bool(false) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolsFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Bool(false) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBools(t *testing.T) { + t1 := Bool(true) + t2 := Bool(true) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolsFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolsFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := Bool(false) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolsFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Bool(false) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + func TestNotBools(t *testing.T) { t1 := Bool(true) @@ -85,7 +264,33 @@ func TestEqBoolFloat(t *testing.T) { } } +func TestEqBoolFloatFalse(t *testing.T) { + t1 := Bool(true) + t2 := Float(0.0) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + func TestNotEqBoolFloat(t *testing.T) { + t1 := Bool(true) + t2 := Float(0.0) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqBoolFloatFalse(t *testing.T) { t1 := Bool(true) t2 := Float(1.0) @@ -111,39 +316,37 @@ func TestAndBoolFloat(t *testing.T) { } } -func TestOrBoolFloat(t *testing.T) { - t1 := Bool(true) - t2 := Float(0.0) +func TestAndBoolFloatFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Float(1.0) - result, err := t1.Or(t2) + result, err := t1.And(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(true) { - t.Error("Expected true, got ", result) + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) } } -// Bool interacts with Int - -func TestEqBoolInt(t *testing.T) { +func TestAndBoolFloatFalseRight(t *testing.T) { t1 := Bool(true) - t2 := Int(1) + t2 := Float(0.0) - result, err := t1.Eq(t2) + result, err := t1.And(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(true) { - t.Error("Expected true, got ", result) + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) } } -func TestNotEqBoolInt(t *testing.T) { - t1 := Bool(true) - t2 := Int(1) +func TestAndBoolFloatFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Float(0.0) - result, err := t1.NotEq(t2) + result, err := t1.And(t2) if err != nil { t.Error(err) } @@ -152,11 +355,11 @@ func TestNotEqBoolInt(t *testing.T) { } } -func TestAndBoolInt(t *testing.T) { +func TestOrBoolFloat(t *testing.T) { t1 := Bool(true) - t2 := Int(1) + t2 := Float(1.0) - result, err := t1.And(t2) + result, err := t1.Or(t2) if err != nil { t.Error(err) } @@ -165,9 +368,9 @@ func TestAndBoolInt(t *testing.T) { } } -func TestOrBoolInt(t *testing.T) { - t1 := Bool(true) - t2 := Int(0) +func TestOrBoolFloatFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Float(1.0) result, err := t1.Or(t2) if err != nil { @@ -178,13 +381,11 @@ func TestOrBoolInt(t *testing.T) { } } -// Bool interacts with Char - -func TestEqBoolChar(t *testing.T) { +func TestOrBoolFloatFalseRight(t *testing.T) { t1 := Bool(true) - t2 := Char(1) + t2 := Float(0.0) - result, err := t1.Eq(t2) + result, err := t1.Or(t2) if err != nil { t.Error(err) } @@ -193,11 +394,11 @@ func TestEqBoolChar(t *testing.T) { } } -func TestNotEqBoolChar(t *testing.T) { - t1 := Bool(true) - t2 := Char(1) +func TestOrBoolFloatFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Float(0.0) - result, err := t1.NotEq(t2) + result, err := t1.Or(t2) if err != nil { t.Error(err) } @@ -206,11 +407,24 @@ func TestNotEqBoolChar(t *testing.T) { } } -func TestAndBoolChar(t *testing.T) { +func TestXorBoolFloat(t *testing.T) { t1 := Bool(true) - t2 := Char(1) + t2 := Float(1.0) - result, err := t1.And(t2) + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolFloatFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Float(1.0) + + result, err := t1.Xor(t2) if err != nil { t.Error(err) } @@ -219,11 +433,11 @@ func TestAndBoolChar(t *testing.T) { } } -func TestOrBoolChar(t *testing.T) { +func TestXorBoolFloatFalseRight(t *testing.T) { t1 := Bool(true) - t2 := Char(0) + t2 := Float(0.0) - result, err := t1.Or(t2) + result, err := t1.Xor(t2) if err != nil { t.Error(err) } @@ -232,20 +446,1078 @@ func TestOrBoolChar(t *testing.T) { } } -// Bool interacts with String +func TestXorBoolFloatFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Float(0.0) -func TestAddBoolString(t *testing.T) { - t1, err := NewBool("true") + result, err := t1.Xor(t2) if err != nil { - t.Errorf("Error: %s", err) + t.Error(err) } - t2, _ := NewString("Hello") - actual, err := t1.Add(t2) - if err != nil { + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Bool interacts with Int + +func TestEqBoolInt(t *testing.T) { + t1 := Bool(true) + t2 := Int(1) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqBoolIntFalse(t *testing.T) { + t1 := Bool(true) + t2 := Int(0) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqBoolInt(t *testing.T) { + t1 := Bool(true) + t2 := Int(0) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqBoolIntFalse(t *testing.T) { + t1 := Bool(true) + t2 := Int(1) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolInt(t *testing.T) { + t1 := Bool(true) + t2 := Int(1) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndBoolIntFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Int(1) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolIntFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := Int(0) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolIntFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Int(0) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrBoolInt(t *testing.T) { + t1 := Bool(true) + t2 := Int(1) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolIntFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Int(1) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolIntFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := Int(0) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolIntFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Int(0) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolInt(t *testing.T) { + t1 := Bool(true) + t2 := Int(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolIntFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Int(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolIntFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := Int(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolIntFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Int(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Bool interacts with Char + +func TestEqBoolChar(t *testing.T) { + t1 := Bool(true) + t2 := Char('A') + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqBoolCharFalse(t *testing.T) { + t1 := Bool(true) + t2 := Char(0) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqBoolChar(t *testing.T) { + t1 := Bool(true) + t2 := Char(0) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqBoolCharFalse(t *testing.T) { + t1 := Bool(true) + t2 := Char('A') + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolChar(t *testing.T) { + t1 := Bool(true) + t2 := Char('A') + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndBoolCharFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Char('A') + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolCharFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := Char(0) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolCharFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Char(0) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrBoolChar(t *testing.T) { + t1 := Bool(true) + t2 := Char('A') + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolCharFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Char('A') + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolCharFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := Char(0) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolCharFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Char(0) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolChar(t *testing.T) { + t1 := Bool(true) + t2 := Char('A') + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolCharFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := Char('A') + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolCharFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := Char(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolCharFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := Char(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Bool interacts with String + +func TestAddBoolString(t *testing.T) { + t1, err := NewBool("true") + if err != nil { + t.Errorf("Error: %s", err) + } + t2, _ := NewString("Hello") + actual, err := t1.Add(t2) + if err != nil { t.Errorf("Error: %s", err) } - expected, _ := NewString("trueHello") - if actual != expected { - t.Errorf("Expected %s, got %s", expected, actual) + expected, _ := NewString("trueHello") + if actual != expected { + t.Errorf("Expected %s, got %s", expected, actual) + } +} + +func TestBoolString(t *testing.T) { + t1, _ := NewBool("true") + t2 := "true" + if t1.String() != t2 { + t.Errorf("Expected \"true\", got %s", t1.String()) + } +} + +func TestBoolGetString(t *testing.T) { + t1, _ := NewBool("false") + t2 := String("false") + if t1.GetString() != t2 { + t.Errorf("Expected \"false\", got %s", t1.GetString()) + } +} + +// Bool interacts with Var + +func TestEqBoolVar(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(true)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqBoolVarFalse(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(false)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqBoolVar(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(false)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqBoolVarFalse(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(true)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolVar(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(true)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndBoolVarFalseLeft(t *testing.T) { + t1 := Bool(false) + t2, _ := NewVar("test", "bool", Bool(true)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolVarFalseRight(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(false)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolVarFalseBoth(t *testing.T) { + t1 := Bool(false) + t2, _ := NewVar("test", "bool", Bool(false)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrBoolVar(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(true)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolVarFalseLeft(t *testing.T) { + t1 := Bool(false) + t2, _ := NewVar("test", "bool", Bool(true)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolVarFalseRight(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(false)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolVarFalseBoth(t *testing.T) { + t1 := Bool(false) + t2, _ := NewVar("test", "bool", Bool(false)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolVar(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(true)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolVarFalseLeft(t *testing.T) { + t1 := Bool(false) + t2, _ := NewVar("test", "bool", Bool(true)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolVarFalseRight(t *testing.T) { + t1 := Bool(true) + t2, _ := NewVar("test", "bool", Bool(false)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolVarFalseBoth(t *testing.T) { + t1 := Bool(false) + t2, _ := NewVar("test", "bool", Bool(false)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Bool interacts with Any + +func TestEqBoolAny(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(true)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqBoolAnyFalse(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(false)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqBoolAny(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(false)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqBoolAnyFalse(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(true)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolAny(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(true)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndBoolAnyFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := NewAny(Bool(true)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolAnyFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(false)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndBoolAnyFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := NewAny(Bool(false)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrBoolAny(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(true)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolAnyFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := NewAny(Bool(true)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolAnyFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(false)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrBoolAnyFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := NewAny(Bool(false)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolAny(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(true)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorBoolAnyFalseLeft(t *testing.T) { + t1 := Bool(false) + t2 := NewAny(Bool(true)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolAnyFalseRight(t *testing.T) { + t1 := Bool(true) + t2 := NewAny(Bool(false)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorBoolAnyFalseBoth(t *testing.T) { + t1 := Bool(false) + t2 := NewAny(Bool(false)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// test bool errors + +func TestBoolSetValueErr(t *testing.T) { + t1 := Bool(false) + + err := t1.SetValue(1) + if err == nil { + t.Error("Expected error when using SetValue of Bool") + } +} + +func TestBoolGetIndexErr(t *testing.T) { + t1 := Bool(false) + + _, err := t1.GetIndex(Bool(true)) + if err == nil { + t.Error("Expected error when using SetValue of Bool") + } +} + +func TestBoolAddErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.Add(t2) + if err == nil { + t.Error("Expected error when adding a bool to a bool") + } +} + +func TestBoolSubErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.Sub(t2) + if err == nil { + t.Error("Expected error when subtracting a bool to a bool") + } +} + +func TestBoolModErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.Mod(t2) + if err == nil { + t.Error("Expected error when getting the modulo of a bool") + } +} + +func TestBoolMulErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.Mul(t2) + if err == nil { + t.Error("Expected error when multiplying a bool") + } +} + +func TestBoolDivErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.Div(t2) + if err == nil { + t.Error("Expected error when dividing a bool") + } +} + +func TestBoolDivEcErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.DivEc(t2) + if err == nil { + t.Error("Expected error when dividing ec a bool") + } +} + +func TestBoolEqErr(t *testing.T) { + t1 := Bool(false) + t2 := String("false") + + _, err := t1.Eq(t2) + if err == nil { + t.Error("Expected error when comparing a string to a bool") + } +} + +func TestBoolNotEqErr(t *testing.T) { + t1 := Bool(false) + t2 := String("false") + + _, err := t1.NotEq(t2) + if err == nil { + t.Error("Expected error when comparing a string to a bool") + } +} + +func TestBoolGtErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.Gt(t2) + if err == nil { + t.Error("Expected error when comparing size of a bool") + } +} + +func TestBoolGtEqErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.GtEq(t2) + if err == nil { + t.Error("Expected error when comparing size of a bool") + } +} + +func TestBoolLwErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.Lw(t2) + if err == nil { + t.Error("Expected error when comparing size of a bool") + } +} + +func TestBoolLwEqErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.LwEq(t2) + if err == nil { + t.Error("Expected error when comparing size of a bool") + } +} + +func TestBoolAndErr(t *testing.T) { + t1 := Bool(false) + t2 := String("false") + + _, err := t1.And(t2) + if err == nil { + t.Error("Expected error when comparing a string to a bool") + } +} + +func TestBoolOrErr(t *testing.T) { + t1 := Bool(false) + t2 := String("false") + + _, err := t1.Or(t2) + if err == nil { + t.Error("Expected error when comparing a string to a bool") + } +} + +func TestBoolXorErr(t *testing.T) { + t1 := Bool(false) + t2 := String("test") + + _, err := t1.Xor(t2) + if err == nil { + t.Error("Expected error when comparing a string to a bool") + } +} + +func TestBoolAppendErr(t *testing.T) { + t1 := Bool(false) + t2 := Bool(true) + + _, err := t1.Append(t2) + if err == nil { + t.Error("Expected error when appending a bool") + } +} + +func TestBoolLenErr(t *testing.T) { + t1 := Bool(false) + + _, err := t1.Len() + if err == nil { + t.Error("Expected error when getting length of a bool") } } diff --git a/interpreter/eclaType/char_test.go b/interpreter/eclaType/char_test.go index cfedafb..e9f46b5 100644 --- a/interpreter/eclaType/char_test.go +++ b/interpreter/eclaType/char_test.go @@ -1,39 +1,48 @@ package eclaType import ( + "github.com/Eclalang/Ecla/interpreter/utils" "testing" ) // Char interacts with Char func TestNewChar(t *testing.T) { - t1 := Char('A') + t1, _ := NewChar("c") + + if t1 != 'c' { + t.Error("Error when creating a Char") + } +} - if t1 != 'A' { - t.Error("Error when creating a char") +func TestNewEmptyChar(t *testing.T) { + t1, _ := NewChar("") + + if t1 != Char(0) { + t.Error("Error when creating a Char") } } func TestCharGetValue(t *testing.T) { - t1 := Char('A') + t1 := Char('c') result := t1.GetValue() - if result != Char('A') { - t.Error("Expected 'A', got ", result) + if result != Char('c') { + t.Error("Expected 'c', got ", result) } } func TestCharGetType(t *testing.T) { - t1 := Char('A') + t1 := Char('c') result := t1.GetType() if result != "char" { - t.Error("Expected char, got ", result) + t.Error("Expected \"char\", got ", result) } } func TestCharIsNull(t *testing.T) { - t1 := Char('A') + t1 := Char('c') result := t1.IsNull() if result != false { @@ -55,15 +64,28 @@ func TestAddChar(t *testing.T) { } func TestSubChar(t *testing.T) { - t1 := Char('b') + t1 := Char('B') t2 := Char('!') result, err := t1.Sub(t2) if err != nil { t.Error(err) } - if result.GetValue() != Char('A') { - t.Error("Expected A, got ", result) + if result.GetValue() != Char('!') { + t.Error("Expected '!', got ", result) + } +} + +func TestNegSubChar(t *testing.T) { + t1 := Char('c') + t2 := Char(-3) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char('f') { + t.Error("Expected 'f', got ", result) } } @@ -76,7 +98,7 @@ func TestModChar(t *testing.T) { t.Error(err) } if result.GetValue() != Char(32) { - t.Error("Expected 1, got ", result) + t.Error("Expected 32, got ", result) } } @@ -89,7 +111,20 @@ func TestMulCharChar(t *testing.T) { t.Error(err) } if result.GetValue() != Char('B') { - t.Error("Expected B, got ", result) + t.Error("Expected 'B', got ", result) + } +} + +func TestMulNegChar(t *testing.T) { + t1 := Char(4) + t2 := Char(-2) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-8) { + t.Error("Expected -8, got ", result) } } @@ -102,7 +137,7 @@ func TestDivChar(t *testing.T) { t.Error(err) } if result.GetValue() != Char(2) { - t.Error("Expected !, got ", result) + t.Error("Expected 2, got ", result) } } @@ -110,12 +145,25 @@ func TestDivEcChar(t *testing.T) { t1 := Char('B') t2 := Char('!') - result, err := t1.Div(t2) + result, err := t1.DivEc(t2) if err != nil { t.Error(err) } if result.GetValue() != Char(2) { - t.Error("Expected !, got ", result) + t.Error("Expected 2, got ", result) + } +} + +func TestDivEcNegChar(t *testing.T) { + t1 := Char(5) + t2 := Char(-2) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-2) { + t.Error("Expected -2, got ", result) } } @@ -211,7 +259,7 @@ func TestGtCharEq(t *testing.T) { } func TestGtEqChar(t *testing.T) { - t1 := Char('A') + t1 := Char('B') t2 := Char('A') result, err := t1.GtEq(t2) @@ -349,7 +397,7 @@ func TestAndCharFalseRight(t *testing.T) { t.Error(err) } if result != Bool(false) { - t.Error("Expected true, got ", result) + t.Error("Expected false, got ", result) } } @@ -362,7 +410,7 @@ func TestAndCharFalseLeft(t *testing.T) { t.Error(err) } if result != Bool(false) { - t.Error("Expected true, got ", result) + t.Error("Expected false, got ", result) } } @@ -375,7 +423,7 @@ func TestAndCharFalseBoth(t *testing.T) { t.Error(err) } if result != Bool(false) { - t.Error("Expected true, got ", result) + t.Error("Expected false, got ", result) } } @@ -431,8 +479,60 @@ func TestOrCharFalseBoth(t *testing.T) { } } -func TestNotCharFalse(t *testing.T) { +func TestXorChar(t *testing.T) { + t1 := Char('A') + t2 := Char('B') + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharFalseLeft(t *testing.T) { + t1 := Char(0) + t2 := Char('A') + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharFalseRight(t *testing.T) { t1 := Char('A') + t2 := Char(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharFalseBoth(t *testing.T) { + t1 := Char(0) + t2 := Char(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotCharFalse(t *testing.T) { + t1 := Char(1) result, err := t1.Not() if err != nil { @@ -455,18 +555,37 @@ func TestNotCharTrue(t *testing.T) { } } +func TestCharGetSize(t *testing.T) { + t1 := Char(0) + expected := utils.Sizeof(t1) + + result := t1.GetSize() + if result != expected { + t.Errorf("expected %d, got %d", expected, result) + } +} + +func TestCharGetValueAsInt(t *testing.T) { + t1 := Char('A') + + result := t1.GetValueAsInt() + if result != Int(65) { + t.Error("Expected 65, got ", result) + } +} + // Char interacts with Int func TestAddCharInt(t *testing.T) { t1 := Char('A') - t2 := Int(2) + t2 := Int(1) result, err := t1.Add(t2) if err != nil { t.Error(err) } - if result.GetValue() != Char('C') { - t.Error("Expected C, got ", result) + if result.GetValue() != Char('B') { + t.Error("Expected 'B', got ", result) } } @@ -479,33 +598,33 @@ func TestAddCharIntNeg(t *testing.T) { t.Error(err) } if result.GetValue() != Char('A') { - t.Error("Expected A, got ", result) + t.Error("Expected 'A', got ", result) } } func TestSubCharInt(t *testing.T) { t1 := Char('B') - t2 := Int(1) + t2 := Int(33) result, err := t1.Sub(t2) if err != nil { t.Error(err) } - if result.GetValue() != Char('A') { - t.Error("Expected A, got ", result) + if result.GetValue() != Char('!') { + t.Error("Expected '!', got ", result) } } func TestSubCharIntNeg(t *testing.T) { - t1 := Char('A') - t2 := Int(-1) + t1 := Char('!') + t2 := Int(-33) result, err := t1.Sub(t2) if err != nil { t.Error(err) } if result.GetValue() != Char('B') { - t.Error("Expected B, got ", result) + t.Error("Expected 'B', got ", result) } } @@ -522,7 +641,7 @@ func TestModCharInt(t *testing.T) { } } -func TestMulCharCharInt(t *testing.T) { +func TestMulCharInt(t *testing.T) { t1 := Char('!') t2 := Int(2) @@ -531,33 +650,33 @@ func TestMulCharCharInt(t *testing.T) { t.Error(err) } if result.GetValue() != Char('B') { - t.Error("Expected B, got ", result) + t.Error("Expected 'B', got ", result) } } func TestDivCharInt(t *testing.T) { t1 := Char('B') - t2 := Int(2) + t2 := Int(33) result, err := t1.Div(t2) if err != nil { t.Error(err) } - if result.GetValue() != Char('!') { - t.Error("Expected !, got ", result) + if result.GetValue() != Char(2) { + t.Error("Expected 2, got ", result) } } func TestDivEcCharInt(t *testing.T) { - t1 := Char('C') - t2 := Int(2) + t1 := Char('B') + t2 := Int(33) result, err := t1.DivEc(t2) if err != nil { t.Error(err) } - if result.GetValue() != Char('!') { - t.Error("Expected !, got ", result) + if result.GetValue() != Char(2) { + t.Error("Expected 2, got ", result) } } @@ -576,7 +695,7 @@ func TestEqCharInt(t *testing.T) { func TestEqCharIntFalse(t *testing.T) { t1 := Char('A') - t2 := Int(66) + t2 := Int(25) result, err := t1.Eq(t2) if err != nil { @@ -600,7 +719,7 @@ func TestNotEqCharInt(t *testing.T) { } } -func TestNotEqCharCharIntFalse(t *testing.T) { +func TestNotEqCharIntFalse(t *testing.T) { t1 := Char('!') t2 := Int(33) @@ -614,7 +733,7 @@ func TestNotEqCharCharIntFalse(t *testing.T) { } func TestGtCharIntTrue(t *testing.T) { - t1 := Char('A') + t1 := Char('B') t2 := Int(1) result, err := t1.Gt(t2) @@ -639,7 +758,7 @@ func TestGtCharIntFalse(t *testing.T) { } } -func TestGtCharIntEq(t *testing.T) { +func TestGtCharCharEq(t *testing.T) { t1 := Char('A') t2 := Int(65) @@ -654,7 +773,7 @@ func TestGtCharIntEq(t *testing.T) { func TestGtEqCharInt(t *testing.T) { t1 := Char('A') - t2 := Int(1) + t2 := Char(64) result, err := t1.GtEq(t2) if err != nil { @@ -705,8 +824,8 @@ func TestLwCharInt(t *testing.T) { } func TestLwCharIntFalse(t *testing.T) { - t1 := Char('A') - t2 := Int(64) + t1 := Char('B') + t2 := Int(1) result, err := t1.Lw(t2) if err != nil { @@ -873,56 +992,158 @@ func TestOrCharIntFalseBoth(t *testing.T) { } } +func TestXorCharInt(t *testing.T) { + t1 := Char('A') + t2 := Int(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharIntFalseLeft(t *testing.T) { + t1 := Char(0) + t2 := Int(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharIntFalseRight(t *testing.T) { + t1 := Char('A') + t2 := Int(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharIntFalseBoth(t *testing.T) { + t1 := Char(0) + t2 := Int(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + // Char interacts with String func TestAddCharString(t *testing.T) { - t1 := Char('!') + t1 := Char('a') t2 := String("hello") result, err := t1.Add(t2) if err != nil { t.Error(err) } - if result.GetValue() != String("!hello") { - t.Error("Expected 1hello, got ", result) + if result.GetValue() != String("ahello") { + t.Error("Expected ahello, got ", result) } } func TestCharString(t *testing.T) { - t1 := Char('!') - t2 := "!" + t1 := Char('0') + t2 := "0" - result := t1.String() == (t2) - if result != true { - t.Error("Expected true, got ", result) + result := t1.String() + if result != t2 { + t.Error("Expected \"0\", got ", result) } } + func TestCharGetString(t *testing.T) { - t1 := Char('!') - t2 := String("!") + t1 := Char('0') + t2 := String("0") result, err := t1.GetString().Eq(t2) if err != nil { t.Error(err) } if result.GetValue() != Bool(true) { - t.Error("Expected true, got ", result) + t.Error("Expected \"0\", got ", result) } } -func TestCharAppendChar(t *testing.T) { - t1 := Char('A') - t2 := Char('B') +// test errors Char - _, err := t1.Append(t2) +func TestCharAndErr(t *testing.T) { + t1 := Char(5) + t2 := String("test") + + _, err := t1.And(t2) if err == nil { - t.Error("Expected error") + t.Error("Expected error when checking Char and string") } } -func TestCharAppendString(t *testing.T) { - t1 := Char('A') - t2 := String("BC") +func TestCharOrErr(t *testing.T) { + t1 := Char(5) + t2 := String("test") + + _, err := t1.Or(t2) + if err == nil { + t.Error("Expected error when checking Char or string") + } +} + +func TestCharXorErr(t *testing.T) { + t1 := Char(5) + t2 := String("test") + + _, err := t1.Xor(t2) + if err == nil { + t.Error("Expected error when checking Char xor string") + } +} + +func TestCharLenErr(t *testing.T) { + t1 := Char(5) + + _, err := t1.Len() + if err == nil { + t.Error("Expected error when checking len of Char") + } +} + +func TestDivEcCharFloatErr(t *testing.T) { + t1 := Char(5) + t2 := Float(2) + + _, err := t1.DivEc(t2) + if err == nil { + t.Error("Expected error when dividing ec by float") + } +} + +func TestNewCharWithString(t *testing.T) { + _, err := NewChar("test") + + if err == nil { + t.Error("Expected error when creating a char with a string") + } +} + +func TestCharAppendErr(t *testing.T) { + t1 := Char(0) + t2 := Char(1) _, err := t1.Append(t2) if err == nil { @@ -930,143 +1151,141 @@ func TestCharAppendString(t *testing.T) { } } -// test errors Char - func TestCharSetValueErr(t *testing.T) { - t1 := Char('A') + t1 := Char(0) - err := t1.SetValue('B') + err := t1.SetValue(1) if err == nil { t.Error("Expected error") } } func TestCharGetIndexErr(t *testing.T) { - t1 := Char('A') + t1 := Char(0) - _, err := t1.GetIndex(Int(0)) + _, err := t1.GetIndex(Char(0)) if err == nil { t.Error("Expected error when indexing") } } func TestAddCharErr(t *testing.T) { - t1 := Char('A') + t1 := Char(0) t2 := Bool(true) _, err := t1.Add(t2) if err == nil { - t.Error("Expected error when adding a bool to a char") + t.Error("Expected error when adding a bool to an Char") } } func TestSubCharErr(t *testing.T) { - t1 := Char('A') + t1 := Char(0) t2 := Bool(true) _, err := t1.Sub(t2) if err == nil { - t.Error("Expected error when subtracting a bool to a char") + t.Error("Expected error when subtracting a bool to an Char") } } func TestMulCharErr(t *testing.T) { - t1 := Char('A') + t1 := Char(0) t2 := Bool(true) _, err := t1.Mul(t2) if err == nil { - t.Error("Expected error when multiplying a char by a bool") + t.Error("Expected error when multiplying an Char by a bool") } } func TestDivCharErr(t *testing.T) { - t1 := Char('A') + t1 := Char(0) t2 := Bool(true) _, err := t1.Div(t2) if err == nil { - t.Error("Expected error when dividing a char by a bool") + t.Error("Expected error when dividing an Char by a bool") } } func TestDivBy0CharErr(t *testing.T) { - t1 := Char('A') + t1 := Char(0) t2 := Char(0) _, err := t1.Div(t2) if err == nil { - t.Error("Expected error when dividing a char by 0") + t.Error("Expected error when dividing an Char by 0") } } func TestDivBy0CharIntErr(t *testing.T) { - t1 := Char('A') + t1 := Char(5) t2 := Int(0) _, err := t1.Div(t2) if err == nil { - t.Error("Expected error when dividing a char by 0") + t.Error("Expected error when dividing an Char by 0") } } func TestModCharErr(t *testing.T) { - t1 := Char('A') + t1 := Char(1) t2 := Bool(true) _, err := t1.Mod(t2) if err == nil { - t.Error("Expected error when doing a modulo of a char by a bool") + t.Error("Expected error when doing a modulo of an Char by a bool") } } func TestModBy0CharErr(t *testing.T) { - t1 := Char('A') + t1 := Char(1) t2 := Char(0) _, err := t1.Mod(t2) if err == nil { - t.Error("Expected error when modding a char by 0") + t.Error("Expected error when modding a Char by 0") } } func TestModBy0CharIntErr(t *testing.T) { - t1 := Char('A') + t1 := Char(1) t2 := Int(0) _, err := t1.Mod(t2) if err == nil { - t.Error("Expected error when modding a char by 0") + t.Error("Expected error when modding a Char by 0") } } func TestDivEcCharErr(t *testing.T) { - t1 := Char('A') + t1 := Char(1) t2 := Bool(true) _, err := t1.DivEc(t2) if err == nil { - t.Error("Expected error when dividing a char by a bool") + t.Error("Expected error when dividing a Char by a bool") } } func TestDivEcBy0CharErr(t *testing.T) { - t1 := Char('A') + t1 := Char(1) t2 := Char(0) _, err := t1.DivEc(t2) if err == nil { - t.Error("Expected error when dividing a char by 0") + t.Error("Expected error when dividing a Char by 0") } } func TestDivEcBy0CharIntErr(t *testing.T) { - t1 := Char('A') + t1 := Char(1) t2 := Int(0) _, err := t1.DivEc(t2) if err == nil { - t.Error("Expected error when dividing a char by 0") + t.Error("Expected error when dividing a Char by 0") } } @@ -1076,7 +1295,7 @@ func TestEqCharErr(t *testing.T) { _, err := t1.Eq(t2) if err == nil { - t.Error("Expected error when testing equality between char and bool") + t.Error("Expected error when testing equality between Char and bool") } } @@ -1086,7 +1305,7 @@ func TestNotEqCharErr(t *testing.T) { _, err := t1.NotEq(t2) if err == nil { - t.Error("Expected error when testing inequality between char and bool") + t.Error("Expected error when testing inequality between Char and bool") } } @@ -1096,7 +1315,7 @@ func TestGtCharErr(t *testing.T) { _, err := t1.Gt(t2) if err == nil { - t.Error("Expected error when testing if a char is greater than a bool") + t.Error("Expected error when testing if a Char is greater than a bool") } } @@ -1106,7 +1325,7 @@ func TestGtEqCharErr(t *testing.T) { _, err := t1.GtEq(t2) if err == nil { - t.Error("Expected error when testing if a char is greater or equal to a bool") + t.Error("Expected error when testing if a Char is greater or equal to a bool") } } @@ -1116,7 +1335,7 @@ func TestLwCharErr(t *testing.T) { _, err := t1.Lw(t2) if err == nil { - t.Error("Expected error when testing if a char is lower than a bool") + t.Error("Expected error when testing if a Char is lower than a bool") } } @@ -1126,23 +1345,24 @@ func TestLwEqCharErr(t *testing.T) { _, err := t1.LwEq(t2) if err == nil { - t.Error("Expected error when testing if a char is lower or equal to a bool") + t.Error("Expected error when testing if a Char is lower or equal to a bool") } } -func TestCharAppendErr(t *testing.T) { +func TestCharAppendTypeErr(t *testing.T) { t1 := Char('A') t2 := Bool(true) _, err := t1.Append(t2) if err == nil { - t.Error("Expected error when appending a bool to a char") + t.Error("Expected error when appending a bool to a Char") } } -// Char interacts with float +// Char Chareracts with float + func TestAndCharFloat(t *testing.T) { - t1 := Char('A') + t1 := Char('2') t2 := Float(1) result, err := t1.And(t2) @@ -1155,7 +1375,7 @@ func TestAndCharFloat(t *testing.T) { } func TestAndCharFloatFalseRight(t *testing.T) { - t1 := Char('A') + t1 := Char('1') t2 := Float(0) result, err := t1.And(t2) @@ -1194,7 +1414,7 @@ func TestAndCharFloatFalseBoth(t *testing.T) { } func TestOrCharFloat(t *testing.T) { - t1 := Char('A') + t1 := Char('1') t2 := Float(1) result, err := t1.Or(t2) @@ -1207,7 +1427,7 @@ func TestOrCharFloat(t *testing.T) { } func TestOrCharFloatFalseRight(t *testing.T) { - t1 := Char('A') + t1 := Char('1') t2 := Float(0) result, err := t1.Or(t2) @@ -1244,3 +1464,1231 @@ func TestOrCharFloatFalseBoth(t *testing.T) { t.Error("Expected true, got ", result) } } + +func TestXorCharFloat(t *testing.T) { + t1 := Char('1') + t2 := Float(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharFloatFalseRight(t *testing.T) { + t1 := Char('1') + t2 := Float(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorCharFloatFalseLeft(t *testing.T) { + t1 := Char(0) + t2 := Float(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorCharFloatFalseBoth(t *testing.T) { + t1 := Char(0) + t2 := Float(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +// Char interacts with Bool + +func TestAndCharBool(t *testing.T) { + t1 := Char('1') + t2 := Bool(true) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndCharBoolFalseLeft(t *testing.T) { + t1 := Char(0) + t2 := Bool(true) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndCharBoolFalseRight(t *testing.T) { + t1 := Char('1') + t2 := Bool(false) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndCharBoolFalseBoth(t *testing.T) { + t1 := Char(0) + t2 := Bool(false) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrCharBool(t *testing.T) { + t1 := Char('1') + t2 := Bool(true) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrCharBoolFalseLeft(t *testing.T) { + t1 := Char(0) + t2 := Bool(true) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestOrCharBoolFalseRight(t *testing.T) { + t1 := Char('1') + t2 := Bool(false) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestOrCharBoolFalseBoth(t *testing.T) { + t1 := Char(0) + t2 := Bool(false) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharBool(t *testing.T) { + t1 := Char('1') + t2 := Bool(true) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharBoolFalseLeft(t *testing.T) { + t1 := Char(0) + t2 := Bool(true) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharBoolFalseRight(t *testing.T) { + t1 := Char('1') + t2 := Bool(false) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharBoolFalseBoth(t *testing.T) { + t1 := Char(0) + t2 := Bool(false) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Char intreracts with Var + +func TestAddCharVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(3) { + t.Error("Expected 3, got ", result) + } +} + +func TestAddNegCharVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(-2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-1) { + t.Error("Expected -1, got ", result) + } +} + +func TestSubCharVar(t *testing.T) { + t1 := Char(4) + t2, _ := NewVar("testVar", "char", Char(3)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestNegSubCharVar(t *testing.T) { + t1 := Char(4) + t2, _ := NewVar("testVar", "char", Char(-3)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(7) { + t.Error("Expected 7, got ", result) + } +} + +func TestModCharVar(t *testing.T) { + t1 := Char(3) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Mod(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestMulCharVar(t *testing.T) { + t1 := Char(4) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(8) { + t.Error("Expected 8, got ", result) + } +} + +func TestMulNegCharVar(t *testing.T) { + t1 := Char(4) + t2, _ := NewVar("testVar", "char", Char(-2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-8) { + t.Error("Expected -8, got ", result) + } +} + +func TestDivCharVar(t *testing.T) { + t1 := Char(4) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(2) { + t.Error("Expected 2, got ", result) + } +} + +func TestDivNegCharVar(t *testing.T) { + t1 := Char(4) + t2, _ := NewVar("testVar", "char", Char(-2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-2) { + t.Error("Expected -2, got ", result) + } +} + +func TestDivEcCharVar(t *testing.T) { + t1 := Char(5) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(2) { + t.Error("Expected 2, got ", result) + } +} + +func TestDivEcNegCharVar(t *testing.T) { + t1 := Char(5) + t2, _ := NewVar("testVar", "char", Char(-2)) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-2) { + t.Error("Expected -2, got ", result) + } +} + +func TestEqCharVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(1)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqCharFalseVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqCharVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqCharVarFalse(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(1)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtCharVarTrue(t *testing.T) { + t1 := Char(2) + t2, _ := NewVar("testVar", "char", Char(1)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtCharVarFalse(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtCharVarEq(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(1)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqCharVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(0)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqCharVarEq(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(1)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqCharVarFalse(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwCharVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwCharVarFalse(t *testing.T) { + t1 := Char(2) + t2, _ := NewVar("testVar", "char", Char(1)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwCharVarEq(t *testing.T) { + t1 := Char(2) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqCharVar(t *testing.T) { + t1 := Char(2) + t2, _ := NewVar("testVar", "char", Char(3)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqCharVarFalse(t *testing.T) { + t1 := Char(2) + t2, _ := NewVar("testVar", "char", Char(1)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqCharVarEq(t *testing.T) { + t1 := Char(2) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndCharVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndCharVarFalseRight(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndCharVarFalseLeft(t *testing.T) { + t1 := Char(0) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndCharVarFalseBoth(t *testing.T) { + t1 := Char(0) + t2, _ := NewVar("testVar", "char", Char(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrCharVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrCharVarFalseRight(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrCharVarFalseLeft(t *testing.T) { + t1 := Char(0) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrCharVarFalseBoth(t *testing.T) { + t1 := Char(0) + t2, _ := NewVar("testVar", "char", Char(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestXorCharVar(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharVarFalseLeft(t *testing.T) { + t1 := Char(0) + t2, _ := NewVar("testVar", "char", Char(2)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharVarFalseRight(t *testing.T) { + t1 := Char(1) + t2, _ := NewVar("testVar", "char", Char(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharVarFalseBoth(t *testing.T) { + t1 := Char(0) + t2, _ := NewVar("testVar", "char", Char(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Char Chareracts with Any + +func TestAddCharAnt(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(3) { + t.Error("Expected 3, got ", result) + } +} + +func TestAddNegCharAny(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(-2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-1) { + t.Error("Expected -1, got ", result) + } +} + +func TestSubCharAny(t *testing.T) { + t1 := Char(4) + t2 := NewAny(Char(3)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestNegSubCharAny(t *testing.T) { + t1 := Char(4) + t2 := NewAny(Char(-3)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(7) { + t.Error("Expected 7, got ", result) + } +} + +func TestModCharAny(t *testing.T) { + t1 := Char(3) + t2 := NewAny(Char(2)) + + result, err := t1.Mod(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestMulCharAny(t *testing.T) { + t1 := Char(4) + t2 := NewAny(Char(2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(8) { + t.Error("Expected 8, got ", result) + } +} + +func TestMulNegCharAny(t *testing.T) { + t1 := Char(4) + t2 := NewAny(Char(-2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-8) { + t.Error("Expected -8, got ", result) + } +} + +func TestDivCharAny(t *testing.T) { + t1 := Char(4) + t2 := NewAny(Char(2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(2) { + t.Error("Expected 2, got ", result) + } +} + +func TestDivNegCharAny(t *testing.T) { + t1 := Char(4) + t2 := NewAny(Char(-2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-2) { + t.Error("Expected -2, got ", result) + } +} + +func TestDivEcCharAny(t *testing.T) { + t1 := Char(5) + t2 := NewAny(Char(2)) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(2) { + t.Error("Expected 2, got ", result) + } +} + +func TestDivEcNegCharAny(t *testing.T) { + t1 := Char(5) + t2 := NewAny(Char(-2)) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Char(-2) { + t.Error("Expected -2, got ", result) + } +} + +func TestEqCharAny(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(1)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqCharFalseAny(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(2)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqCharAny(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(2)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqCharAnyFalse(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(1)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtCharAnyTrue(t *testing.T) { + t1 := Char(2) + t2 := NewAny(Char(1)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtCharAnyFalse(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(2)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtCharAnyEq(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(1)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqCharAny(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(0)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqCharAnyEq(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(1)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqCharAnyFalse(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(2)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwCharAny(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwCharAnyFalse(t *testing.T) { + t1 := Char(2) + t2 := NewAny(Char(1)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwCharAnyEq(t *testing.T) { + t1 := Char(2) + t2 := NewAny(Char(2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqCharAny(t *testing.T) { + t1 := Char(2) + t2 := NewAny(Char(3)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqCharAnyFalse(t *testing.T) { + t1 := Char(2) + t2 := NewAny(Char(1)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqCharAnyEq(t *testing.T) { + t1 := Char(2) + t2 := NewAny(Char(2)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndCharAny(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(2)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndCharAnyFalseRight(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndCharAnyFalseLeft(t *testing.T) { + t1 := Char(0) + t2 := NewAny(Char(2)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndCharAnyFalseBoth(t *testing.T) { + t1 := Char(0) + t2 := NewAny(Char(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrCharAny(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(2)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrCharAnyFalseRight(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrCharAnyFalseLeft(t *testing.T) { + t1 := Char(0) + t2 := NewAny(Char(2)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrCharAnyFalseBoth(t *testing.T) { + t1 := Char(0) + t2 := NewAny(Char(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestXorCharAny(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(2)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharAnyFalseLeft(t *testing.T) { + t1 := Char(0) + t2 := NewAny(Char(2)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharAnyFalseRight(t *testing.T) { + t1 := Char(1) + t2 := NewAny(Char(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorCharAnyFalseBoth(t *testing.T) { + t1 := Char(0) + t2 := NewAny(Char(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} diff --git a/interpreter/eclaType/float_test.go b/interpreter/eclaType/float_test.go index 82c1708..a834c60 100644 --- a/interpreter/eclaType/float_test.go +++ b/interpreter/eclaType/float_test.go @@ -2,71 +2,154 @@ package eclaType import ( "fmt" - "strconv" + "github.com/Eclalang/Ecla/interpreter/utils" "testing" ) // Float interacts with Float -func TestAddFloats(t *testing.T) { - t1 := NewFloat("1.1") - t2 := NewFloat("0.2") +func TestNewFloat(t *testing.T) { + t1 := NewFloat("0.3") + + if t1 != 0.3 { + t.Error("Error when creating a Float") + } +} + +func TestFloatGetValue(t *testing.T) { + t1 := Float(2.5) + + result := t1.GetValue() + if result != Float(2.5) { + t.Error("Expected 2.5, got ", result) + } +} + +func TestFloatGetType(t *testing.T) { + t1 := Float(0) + + result := t1.GetType() + if result != "float" { + t.Error("Expected float, got ", result) + } +} + +func TestFloatIsNull(t *testing.T) { + t1 := Float(0) + + result := t1.IsNull() + if result != false { + t.Error("Expected false, got", result) + } +} + +func TestAddFloat(t *testing.T) { + t1 := Float(1.2) + t2 := Float(2.3) result, err := t1.Add(t2) if err != nil { t.Error(err) } - var expect1, _ = strconv.ParseFloat("1.1", 32) - var expect2, _ = strconv.ParseFloat("0.2", 32) - expect := expect1 + expect2 - expected := Float(expect) - if result.GetValue() != expected { - t.Errorf("Expected %v, got %v", expected, result) + if result.GetValue() != Float(3.5) { + t.Error("Expected 3.5, got ", result) } } -func TestSubFloats(t *testing.T) { - t1 := Float(1.1) - t2 := Float(2.2) +//func TestAddNegFloat(t *testing.T) { +// t1 := Float(1.5) +// t2 := Float(-2.3) +// +// result, err := t1.Add(t2) +// if err != nil { +// t.Error(err) +// } +// if result.GetValue() != Float(-0.8) { +// t.Error("Expected -0.8, got ", result) +// } +//} + +func TestSubFloat(t *testing.T) { + t1 := Float(4.6) + t2 := Float(3.1) result, err := t1.Sub(t2) if err != nil { t.Error(err) } - if result.GetValue() != Float(-1.1) { - t.Error("Expected -1.1, got ", result, " t1 = ", t1, " t2 = ", t2) + if result.GetValue() != Float(1.5) { + t.Error("Expected 2.5, got ", result) } } -func TestMulFloats(t *testing.T) { - t1 := Float(1.1) - t2 := Float(2.2) +func TestNegSubFloat(t *testing.T) { + t1 := Float(4.1) + t2 := Float(-3.5) - result, err := t1.Mul(t2) + result, err := t1.Sub(t2) if err != nil { t.Error(err) } - if result.GetValue() != Float(2.42) { - t.Error("Expected 2.42, got ", result, " t1 = ", t1, " t2 = ", t2) + if result.GetValue() != Float(7.6) { + t.Error("Expected 7.6, got ", result) } } -func TestDivFloats(t *testing.T) { - t1 := Float(1.1) - t2 := Float(2.2) +//func TestMulFloat(t *testing.T) { +// t1 := Float(1.2) +// t2 := Float(2.6) +// +// result, err := t1.Mul(t2) +// if err != nil { +// t.Error(err) +// } +// if result.GetValue() != Float(3.12) { +// t.Error("Expected 3.12, got ", result) +// } +//} + +//func TestMulNegFloat(t *testing.T) { +// t1 := Float(1.2) +// t2 := Float(-2.6) +// +// result, err := t1.Mul(t2) +// if err != nil { +// t.Error(err) +// } +// if result.GetValue() != Float(-3.12) { +// t.Error("Expected -3.12, got ", result) +// } +//} + +func TestDivFloat(t *testing.T) { + t1 := Float(3.12) + t2 := Float(1.2) result, err := t1.Div(t2) if err != nil { t.Error(err) } - if result.GetValue() != Float(0.5) { - t.Error("Expected 0.5, got ", result, " t1 = ", t1, " t2 = ", t2) + if result.GetValue() != Float(2.6) { + t.Error("Expected 2.6, got ", result) } } -func TestEqFloats(t *testing.T) { - t1 := Float(1.1) - t2 := Float(1.1) +func TestDivNegFloat(t *testing.T) { + t1 := Float(4.5) + t2 := Float(-2.0) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-2.25) { + t.Error("Expected -2.25, got ", result) + } +} + +func TestEqFloat(t *testing.T) { + t1 := Float(1.3) + t2 := Float(1.3) result, err := t1.Eq(t2) if err != nil { @@ -77,9 +160,22 @@ func TestEqFloats(t *testing.T) { } } -func TestNotEqFloats(t *testing.T) { +func TestEqFloatFalse(t *testing.T) { + t1 := Float(1.4) + t2 := Float(2.6) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqFloat(t *testing.T) { t1 := Float(1.1) - t2 := Float(2.2) + t2 := Float(2.3) result, err := t1.NotEq(t2) if err != nil { @@ -90,9 +186,22 @@ func TestNotEqFloats(t *testing.T) { } } -func TestGtFloats(t *testing.T) { - t1 := Float(3.1) - t2 := Float(2.2) +func TestNotEqFloatFalse(t *testing.T) { + t1 := Float(1.1) + t2 := Float(1.1) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtFloatTrue(t *testing.T) { + t1 := Float(2.4) + t2 := Float(2.3) result, err := t1.Gt(t2) if err != nil { @@ -103,9 +212,61 @@ func TestGtFloats(t *testing.T) { } } -func TestGtEqFloats(t *testing.T) { - t1 := Float(1.1) - t2 := Float(2.2) +func TestGtFloatFalse(t *testing.T) { + t1 := Float(2.1) + t2 := Float(2.3) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtFloatEq(t *testing.T) { + t1 := Float(4.5) + t2 := Float(4.5) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqFloat(t *testing.T) { + t1 := Float(2.3) + t2 := Float(0.6) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatEq(t *testing.T) { + t1 := Float(0.2) + t2 := Float(0.2) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatFalse(t *testing.T) { + t1 := Float(1.2) + t2 := Float(4.2) result, err := t1.GtEq(t2) if err != nil { @@ -116,9 +277,9 @@ func TestGtEqFloats(t *testing.T) { } } -func TestLwFloats(t *testing.T) { - t1 := Float(1.1) - t2 := Float(2.2) +func TestLwFloat(t *testing.T) { + t1 := Float(1.2) + t2 := Float(4.4) result, err := t1.Lw(t2) if err != nil { @@ -129,9 +290,35 @@ func TestLwFloats(t *testing.T) { } } -func TestLwEqFloats(t *testing.T) { - t1 := Float(2.2) - t2 := Float(2.2) +func TestLwFloatFalse(t *testing.T) { + t1 := Float(3.0) + t2 := Float(0.3) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwFloatEq(t *testing.T) { + t1 := Float(5.1) + t2 := Float(5.1) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqFloat(t *testing.T) { + t1 := Float(2.1) + t2 := Float(3.2) result, err := t1.LwEq(t2) if err != nil { @@ -142,215 +329,279 @@ func TestLwEqFloats(t *testing.T) { } } -// Float interacts with Int +func TestLwEqFloatFalse(t *testing.T) { + t1 := Float(2.4) + t2 := Float(1.3) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} -/* func TestAddFloatInt(t *testing.T) { - t1 := eclaType.Float(3.14) - t2 := eclaType.Int(2) +func TestLwEqFloatEq(t *testing.T) { + t1 := Float(2.0) + t2 := Float(2.0) - result, err := t1.Add(t2) + result, err := t1.LwEq(t2) if err != nil { t.Error(err) } - if result.GetValue() != eclaType.Float(5.14) { - t.Error("Expected 5.14, got ", result) + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) } -} */ +} -/* func TestSubFloatInt(t *testing.T) { - t1 := eclaType.Float(3.14) - t2 := eclaType.Int(2) +func TestAndFloat(t *testing.T) { + t1 := Float(1.4) + t2 := Float(2.3) - result, err := t1.Sub(t2) + result, err := t1.And(t2) if err != nil { t.Error(err) } - if result.GetValue() != eclaType.Float(1.14) { - t.Error("Expected 1.14, got ", result) + if result != Bool(true) { + t.Error("Expected true, got ", result) } -} */ +} -func TestMulFloatInt(t *testing.T) { - t1 := Float(3.14) - t2 := Int(2) +func TestAndFloatFalseRight(t *testing.T) { + t1 := Float(1.7) + t2 := Float(0) - result, err := t1.Mul(t2) + result, err := t1.And(t2) if err != nil { t.Error(err) } - if result.GetValue() != Float(6.28) { - t.Error("Expected 6.28, got ", result) + if result != Bool(false) { + t.Error("Expected false, got ", result) } } -func TestDivFloatInt(t *testing.T) { - t1 := Float(4.5) - t2 := Int(2) +func TestAndFloatFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Float(1.8) - result, err := t1.Div(t2) + result, err := t1.And(t2) if err != nil { t.Error(err) } - if result.GetValue() != Float(2.25) { - t.Error("Expected 2.25, got ", result) + if result != Bool(false) { + t.Error("Expected false, got ", result) } } -func TestEqFloatInt(t *testing.T) { - t1 := Float(3.000) - t2 := Int(3) +func TestAndFloatFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Float(0) - result, err := t1.Eq(t2) + result, err := t1.And(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(true) { + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrFloat(t *testing.T) { + t1 := Float(1.8) + t2 := Float(2.6) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { t.Error("Expected true, got ", result) } } -func TestNotEqFloatInt(t *testing.T) { - t1 := Float(3.14) - t2 := Int(3) +func TestOrFloatFalseRight(t *testing.T) { + t1 := Float(1.3) + t2 := Float(0) - result, err := t1.NotEq(t2) + result, err := t1.Or(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(true) { + if result != Bool(true) { t.Error("Expected true, got ", result) } } -func TestGtFloatInt(t *testing.T) { - t1 := Float(3.14) - t2 := Int(3) +func TestOrFloatFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Float(1.4) - result, err := t1.Gt(t2) + result, err := t1.Or(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(true) { + if result != Bool(true) { t.Error("Expected true, got ", result) } } -func TestGtEqFloatInt(t *testing.T) { - t1 := Float(3.14) - t2 := Int(3) +func TestOrFloatFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Float(0) - result, err := t1.GtEq(t2) + result, err := t1.Or(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(true) { + if result != Bool(false) { t.Error("Expected true, got ", result) } } -func TestLwFloatInt(t *testing.T) { - t1 := Float(3.14) - t2 := Int(3) +func TestXorFloat(t *testing.T) { + t1 := Float(0.1) + t2 := Float(1.1) - result, err := t1.Lw(t2) + result, err := t1.Xor(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(false) { + if result != Bool(false) { t.Error("Expected false, got ", result) } } -func TestLwEqFloatInt(t *testing.T) { - t1 := Float(3.14) - t2 := Int(3) +func TestXorFloatFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Float(0.9) - result, err := t1.LwEq(t2) + result, err := t1.Xor(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(false) { + if result != Bool(true) { t.Error("Expected false, got ", result) } } -// Float interacts with String +func TestXorFloatFalseRight(t *testing.T) { + t1 := Float(1.5) + t2 := Float(0) -func TestAddFloatString(t *testing.T) { - t1 := Float(3.14) - t2 := String("hello") + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} - result, err := t1.Add(t2) +func TestXorFloatFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Float(0) + + result, err := t1.Xor(t2) if err != nil { t.Error(err) } - Newexpect1 := fmt.Sprintf("%g", 3.14) - expect2 := "hello" - expect := Newexpect1 + expect2 - expected := String(expect) - fmt.Println(expected) - if result.GetValue() != expected { - t.Errorf("Expected %v, got %v", expected, result) + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotFloatFalse(t *testing.T) { + t1 := Float(1.4) + + result, err := t1.Not() + if err != nil { + t.Error(err) + } + if result != Float(0) { + t.Error("Expected false, got", result) + } +} + +func TestNotFloatTrue(t *testing.T) { + t1 := Float(0) + + result, err := t1.Not() + if err != nil { + t.Error(err) + } + if result != Float(1) { + t.Error("Expected true, got", result) + } +} + +func TestFloatGetSize(t *testing.T) { + t1 := Float(0) + expected := utils.Sizeof(t1) + + result := t1.GetSize() + if result != expected { + t.Errorf("expected %d, got %d", expected, result) } } // Float interacts with Char -/*func TestAddFloatChar(t *testing.T) { - t1 := Float(3.14) - t2 := Char(2) +func TestAddFloatChar(t *testing.T) { + t1 := Float(1) + t2 := Char('A') result, err := t1.Add(t2) if err != nil { t.Error(err) } - if result.GetValue() != Float(5.14) { - t.Error("Expected 5.14, got ", result) + if result.GetValue() != Float(66) { + t.Error("Expected 66, got ", result) } -}*/ +} -/* func TestSubFloatChar(t *testing.T) { - t1 := Float(3.14) - t2 := Char(2) +func TestSubFloatChar(t *testing.T) { + t1 := Float(90) + t2 := Char('A') result, err := t1.Sub(t2) if err != nil { t.Error(err) } - if result.GetValue() != Float(1.14) { - t.Error("Expected 1.14, got ", result) + if result.GetValue() != Float(25) { + t.Error("Expected 25, got ", result) } -} */ +} func TestMulFloatChar(t *testing.T) { - t1 := Float(3.14) - t2 := Char(2) + t1 := Float(2) + t2 := Char('!') result, err := t1.Mul(t2) if err != nil { t.Error(err) } - if result.GetValue() != Float(6.28) { - t.Error("Expected 6.28, got ", result) + if result.GetValue() != Float(66) { + t.Error("Expected 66, got ", result) } } func TestDivFloatChar(t *testing.T) { - t1 := Float(4.5) - t2 := Char(2) + t1 := Float(66) + t2 := Char('!') result, err := t1.Div(t2) if err != nil { t.Error(err) } - if result.GetValue() != Float(2.25) { - t.Error("Expected 2.25, got ", result) + if result.GetValue() != Float(2.0) { + t.Error("Expected 2.0, got ", result) } } func TestEqFloatChar(t *testing.T) { - t1 := Float(3.000) - t2 := Char(3) + t1 := Float(65) + t2 := Char('A') result, err := t1.Eq(t2) if err != nil { @@ -361,9 +612,22 @@ func TestEqFloatChar(t *testing.T) { } } +func TestEqFloatCharFalse(t *testing.T) { + t1 := Float(66) + t2 := Char('A') + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + func TestNotEqFloatChar(t *testing.T) { - t1 := Float(3.14) - t2 := Char(3) + t1 := Float(32) + t2 := Char('!') result, err := t1.NotEq(t2) if err != nil { @@ -374,24 +638,24 @@ func TestNotEqFloatChar(t *testing.T) { } } -func TestGtFloatChar(t *testing.T) { - t1 := Float(3.14) - t2 := Char(3) +func TestNotEqFloatCharFalse(t *testing.T) { + t1 := Float(33) + t2 := Char('!') - result, err := t1.Gt(t2) + result, err := t1.NotEq(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(true) { - t.Error("Expected true, got ", result) + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) } } -func TestGtEqFloatChar(t *testing.T) { - t1 := Float(3.14) - t2 := Char(3) +func TestGtFloatCharTrue(t *testing.T) { + t1 := Float(67) + t2 := Char('A') - result, err := t1.GtEq(t2) + result, err := t1.Gt(t2) if err != nil { t.Error(err) } @@ -400,11 +664,11 @@ func TestGtEqFloatChar(t *testing.T) { } } -func TestLwFloatChar(t *testing.T) { - t1 := Float(3.14) - t2 := Char(3) +func TestGtFloatCharFalse(t *testing.T) { + t1 := Float(64) + t2 := Char('A') - result, err := t1.Lw(t2) + result, err := t1.Gt(t2) if err != nil { t.Error(err) } @@ -413,11 +677,11 @@ func TestLwFloatChar(t *testing.T) { } } -func TestLwEqFloatChar(t *testing.T) { - t1 := Float(3.14) - t2 := Char(3) +func TestGtFloatCharEq(t *testing.T) { + t1 := Float(65) + t2 := Char('A') - result, err := t1.LwEq(t2) + result, err := t1.Gt(t2) if err != nil { t.Error(err) } @@ -425,3 +689,2066 @@ func TestLwEqFloatChar(t *testing.T) { t.Error("Expected false, got ", result) } } + +func TestGtEqFloatChar(t *testing.T) { + t1 := Float(66) + t2 := Char('A') + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatCharEq(t *testing.T) { + t1 := Float(66) + t2 := Char('A') + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatCharFalse(t *testing.T) { + t1 := Float(64) + t2 := Char('A') + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwFloatChar(t *testing.T) { + t1 := Float(64) + t2 := Char('A') + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwFloatCharFalse(t *testing.T) { + t1 := Float(67) + t2 := Char('A') + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwFloatCharEq(t *testing.T) { + t1 := Float(65) + t2 := Char('A') + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqFloatChar(t *testing.T) { + t1 := Float(64) + t2 := Char('A') + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqFloatCharFalse(t *testing.T) { + t1 := Float(66) + t2 := Char('A') + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqFloatCharEq(t *testing.T) { + t1 := Float(65) + t2 := Char('A') + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatChar(t *testing.T) { + t1 := Float(2.0) + t2 := Char('A') + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatCharFalseRight(t *testing.T) { + t1 := Float(3.1) + t2 := Char(0) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatCharFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Char('A') + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatCharFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Char(0) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatChar(t *testing.T) { + t1 := Float(-0.4) + t2 := Char('A') + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatCharFalseRight(t *testing.T) { + t1 := Float(8.6) + t2 := Char(0) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatCharFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Char('A') + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatCharFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Char(0) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestXorFloatChar(t *testing.T) { + t1 := Float(2.4) + t2 := Char(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatCharFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Char('A') + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatCharFalseRight(t *testing.T) { + t1 := Float(1) + t2 := Char(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatCharFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Char(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Float interacts with String + +func TestAddFloatString(t *testing.T) { + t1 := Float(3.14) + t2 := String("hello") + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + Newexpect1 := fmt.Sprintf("%g", 3.14) + expect2 := "hello" + expect := Newexpect1 + expect2 + expected := String(expect) + if result.GetValue() != expected { + t.Errorf("Expected %v, got %v", expected, result) + } +} + +// Float errors + +func TestDivEcFloatErr(t *testing.T) { + t1 := Float(4.12) + t2 := Float(2) + + _, err := t1.DivEc(t2) + if err == nil { + t.Error("Expected error when getting quotient of a Float") + } +} + +func TestNewFloatErr(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Error("Expected a panic when creating a new float with wrong argument, got nil") + } + }() + + NewFloat("not a float") +} + +func TestFloatSetValueErr(t *testing.T) { + t1 := Float(3.14) + + err := t1.SetValue(1.2) + if err == nil { + t.Error("Expected error when setting value of float, got nil") + } +} + +func TestFloatGetIndexErr(t *testing.T) { + t1 := Float(3.14) + + _, err := t1.GetIndex(Float(1.2)) + if err == nil { + t.Error("Expected error, got nil") + } +} + +func TestFloatAddErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.Add(t2) + if err == nil { + t.Error("Expected error when adding a bool and a float, got nil") + } +} + +func TestFloatSubErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.Sub(t2) + if err == nil { + t.Error("Expected error when subtracting a bool from a float, got nil") + } +} + +func TestModFloatErr(t *testing.T) { + t1 := Float(4.12) + t2 := Float(2) + + _, err := t1.Mod(t2) + if err == nil { + t.Error("Expected error when getting remainder of a Float") + } +} + +func TestFloatMulErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.Mul(t2) + if err == nil { + t.Error("Expected error when multiplying a bool and a float, got nil") + } +} + +func TestFloatDivErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.Div(t2) + if err == nil { + t.Error("Expected error when dividing a float by a bool, got nil") + } +} + +func TestFloatEqErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.Eq(t2) + if err == nil { + t.Error("Expected error when comparing a bool and a float, got nil") + } +} + +func TestFloatNotEqErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.NotEq(t2) + if err == nil { + t.Error("Expected error when comparing a bool and a float, got nil") + } +} + +func TestFloatGtErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.Gt(t2) + if err == nil { + t.Error("Expected error when comparing a bool and a float, got nil") + } +} + +func TestFloatGtEqErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.GtEq(t2) + if err == nil { + t.Error("Expected error when comparing a bool and a float, got nil") + } +} + +func TestFloatLwErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.Lw(t2) + if err == nil { + t.Error("Expected error when comparing a bool and a float, got nil") + } +} + +func TestFloatLwEqErr(t *testing.T) { + t1 := Float(3.14) + t2 := Bool(true) + + _, err := t1.LwEq(t2) + if err == nil { + t.Error("Expected error when comparing a bool and a float, got nil") + } +} + +func TestFloatAppendErr(t *testing.T) { + t1 := Float(3.14) + t2 := Float(1.0) + + _, err := t1.Append(t2) + if err == nil { + t.Error("Expected error when appending to a float, got nil") + } +} + +func TestFloatLengthErr(t *testing.T) { + t1 := Float(3.14) + + _, err := t1.Len() + if err == nil { + t.Error("Expected error when getting length of float, got nil") + } +} + +func TestFloatAndErr(t *testing.T) { + t1 := Float(3.14) + t2 := String("test") + + _, err := t1.And(t2) + if err == nil { + t.Error("Expected error when comparing a string and a float, got nil") + } +} + +func TestFloatOrErr(t *testing.T) { + t1 := Float(3.14) + t2 := String("test") + + _, err := t1.Or(t2) + if err == nil { + t.Error("Expected error when comparing a string and a float, got nil") + } +} + +func TestFloatXorErr(t *testing.T) { + t1 := Float(3.14) + t2 := String("test") + + _, err := t1.Xor(t2) + if err == nil { + t.Error("Expected error when comparing a string and a float, got nil") + } +} + +// Float interacts with Int + +func TestAddFloatInt(t *testing.T) { + t1 := Float(1.3) + t2 := Int(2) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(3.3) { + t.Error("Expected 3.3, got ", result) + } +} + +func TestAddNegFloatInt(t *testing.T) { + t1 := Float(1.5) + t2 := Int(-2) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-0.5) { + t.Error("Expected -0.5, got ", result) + } +} + +//func TestSubFloatInt(t *testing.T) { +// t1 := Float(4.2) +// t2 := Int(3) +// +// result, err := t1.Sub(t2) +// if err != nil { +// t.Error(err) +// } +// if result.GetValue() != Float(1.2) { +// t.Error("Expected 1.2, got ", result) +// } +//} + +func TestNegSubFloatInt(t *testing.T) { + t1 := Float(4.1) + t2 := Int(-3) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(7.1) { + t.Error("Expected 7.1, got ", result) + } +} + +func TestMulFloatInt(t *testing.T) { + t1 := Float(4.4) + t2 := Int(2) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(8.8) { + t.Error("Expected 8.8, got ", result) + } +} + +func TestMulNegFloatInt(t *testing.T) { + t1 := Float(4.4) + t2 := Int(-2) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-8.8) { + t.Error("Expected -8.8, got ", result) + } +} + +func TestDivFloatInt(t *testing.T) { + t1 := Float(4.2) + t2 := Int(2) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(2.1) { + t.Error("Expected 2.1, got ", result) + } +} + +func TestDivNegFloatInt(t *testing.T) { + t1 := Float(4.2) + t2 := Int(-2) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-2.1) { + t.Error("Expected -2.1, got ", result) + } +} + +func TestAndFloatInt(t *testing.T) { + t1 := Float(2.4) + t2 := Int(1) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatIntFalseRight(t *testing.T) { + t1 := Float(1.6) + t2 := Int(0) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatIntFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Int(1) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatIntFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Int(0) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatInt(t *testing.T) { + t1 := Float(1.4) + t2 := Int(1) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatIntFalseRight(t *testing.T) { + t1 := Float(1.8) + t2 := Int(0) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatIntFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Int(4) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatIntFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Int(0) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestXorFloatInt(t *testing.T) { + t1 := Float(2.9) + t2 := Int(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatIntFalseRight(t *testing.T) { + t1 := Float(1.7) + t2 := Int(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorFloatIntFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Int(7) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorFloatIntFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Int(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestEqFloatInt(t *testing.T) { + t1 := Float(4.0) + t2 := Int(4) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqFloatIntFalse(t *testing.T) { + t1 := Float(1.1) + t2 := Int(1) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqFloatInt(t *testing.T) { + t1 := Float(0.9) + t2 := Int(1) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqFloatIntFalse(t *testing.T) { + t1 := Float(1.0) + t2 := Int(1) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtFloatInt(t *testing.T) { + t1 := Float(1.1) + t2 := Int(1) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtFloatIntEq(t *testing.T) { + t1 := Float(1.0) + t2 := Int(1) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtFloatIntFalse(t *testing.T) { + t1 := Float(0.4) + t2 := Int(1) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqFloatInt(t *testing.T) { + t1 := Float(1.2) + t2 := Int(1) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatIntEq(t *testing.T) { + t1 := Float(1.0) + t2 := Int(1) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatIntFalse(t *testing.T) { + t1 := Float(0.3) + t2 := Int(1) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwFloatInt(t *testing.T) { + t1 := Float(1.4) + t2 := Int(2) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwFloatIntEq(t *testing.T) { + t1 := Float(1) + t2 := Int(1) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwFloatIntFalse(t *testing.T) { + t1 := Float(2.4) + t2 := Int(1) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqFloatInt(t *testing.T) { + t1 := Float(1) + t2 := Int(2) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqFloatIntEq(t *testing.T) { + t1 := Float(1) + t2 := Int(1) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected tue, got ", result) + } +} + +func TestLwEqFloatIntFalse(t *testing.T) { + t1 := Float(1.7) + t2 := Int(1) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Float interacts with Bool + +func TestAndFloatBool(t *testing.T) { + t1 := Float(4.1) + t2 := Bool(true) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatBoolFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Bool(true) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndFloatBoolFalseRight(t *testing.T) { + t1 := Float(3.1) + t2 := Bool(false) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndFloatBoolFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Bool(false) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrFloatBool(t *testing.T) { + t1 := Float(5.7) + t2 := Bool(true) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatBoolFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Bool(true) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestOrFloatBoolFalseRight(t *testing.T) { + t1 := Float(4.5) + t2 := Bool(false) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestOrFloatBoolFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Bool(false) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatBool(t *testing.T) { + t1 := Float(2.8) + t2 := Bool(true) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatBoolFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := Bool(true) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatBoolFalseRight(t *testing.T) { + t1 := Float(7.1) + t2 := Bool(false) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatBoolFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := Bool(false) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Int interacts with Var + +func TestAddFloatVar(t *testing.T) { + t1 := Float(1.2) + t2, _ := NewVar("testVar", "float", Float(2.1)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(3.3) { + t.Error("Expected 3.3, got ", result) + } +} + +//func TestAddNegFloatVar(t *testing.T) { +// t1 := Float(4.7) +// t2, _ := NewVar("testVar", "float", Float(-2.2)) +// +// result, err := t1.Add(t2) +// if err != nil { +// t.Error(err) +// } +// if result.GetValue() != Float(-2.5) { +// t.Error("Expected -2.5, got ", result) +// } +//} + +//func TestSubFloatVar(t *testing.T) { +// t1 := Float(1.1) +// t2, _ := NewVar("testVar", "float", Float(0.2)) +// +// result, err := t1.Sub(t2) +// if err != nil { +// t.Error(err) +// } +// if result.GetValue() != Float(0.9) { +// t.Error("Expected 0.9, got ", result) +// } +//} + +func TestNegSubFloatVar(t *testing.T) { + t1 := Float(4.2) + t2, _ := NewVar("testVar", "float", Float(-0.9)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(5.1) { + t.Error("Expected 5.1, got ", result) + } +} + +func TestMulFloatVar(t *testing.T) { + t1 := Float(5.0) + t2, _ := NewVar("testVar", "float", Float(0.1)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(0.5) { + t.Error("Expected 0.5, got ", result) + } +} + +func TestMulNegFloatVar(t *testing.T) { + t1 := Float(5.0) + t2, _ := NewVar("testVar", "float", Float(-0.1)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-0.5) { + t.Error("Expected -0.5, got ", result) + } +} + +//func TestDivFloatVar(t *testing.T) { +// t1 := Float(3.6) +// t2, _ := NewVar("testVar", "int", Int(3)) +// +// result, err := t1.Div(t2) +// if err != nil { +// t.Error(err) +// } +// if result.GetValue() != Float(1.2) { +// t.Error("Expected 1.2, got ", result) +// } +//} + +//func TestDivNegFloatVar(t *testing.T) { +// t1 := Float(3.6) +// t2, _ := NewVar("testVar", "int", Int(-3)) +// +// result, err := t1.Div(t2) +// if err != nil { +// t.Error(err) +// } +// if result.GetValue() != Float(-1.2) { +// t.Error("Expected -1.2, got ", result) +// } +//} + +func TestEqFloatVar(t *testing.T) { + t1 := Float(1.2) + t2, _ := NewVar("testVar", "float", Float(1.2)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqFloatFalseVar(t *testing.T) { + t1 := Float(1.2) + t2, _ := NewVar("testVar", "float", Float(2.1)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqFloatVar(t *testing.T) { + t1 := Float(1.4) + t2, _ := NewVar("testVar", "float", Float(4.2)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqFloatVarFalse(t *testing.T) { + t1 := Float(1.4) + t2, _ := NewVar("testVar", "float", Float(1.4)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtFloatVarTrue(t *testing.T) { + t1 := Float(2.1) + t2, _ := NewVar("testVar", "float", Float(2.01)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtFloatVarFalse(t *testing.T) { + t1 := Float(1.1) + t2, _ := NewVar("testVar", "float", Float(2.1)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtFloatVarEq(t *testing.T) { + t1 := Float(1.4) + t2, _ := NewVar("testVar", "float", Float(1.4)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqFloatVar(t *testing.T) { + t1 := Float(1.1) + t2, _ := NewVar("testVar", "float", Float(0.2)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatVarEq(t *testing.T) { + t1 := Float(1.2) + t2, _ := NewVar("testVar", "float", Float(1.2)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatVarFalse(t *testing.T) { + t1 := Float(1.2) + t2, _ := NewVar("testVar", "float", Float(2.1)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwFloatVar(t *testing.T) { + t1 := Float(1.5) + t2, _ := NewVar("testVar", "float", Float(2.5)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwFloatVarFalse(t *testing.T) { + t1 := Float(2.4) + t2, _ := NewVar("testVar", "float", Float(1.4)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwFloatVarEq(t *testing.T) { + t1 := Float(2.7) + t2, _ := NewVar("testVar", "float", Float(2.7)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqFloatVar(t *testing.T) { + t1 := Float(2.2) + t2, _ := NewVar("testVar", "float", Float(3.2)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqFloatVarFalse(t *testing.T) { + t1 := Float(2.4) + t2, _ := NewVar("testVar", "float", Float(1.4)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqFloatVarEq(t *testing.T) { + t1 := Float(2.1) + t2, _ := NewVar("testVar", "float", Float(2.1)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatVar(t *testing.T) { + t1 := Float(1.7) + t2, _ := NewVar("testVar", "float", Float(7.2)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatVarFalseRight(t *testing.T) { + t1 := Float(4.1) + t2, _ := NewVar("testVar", "float", Float(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndFloatVarFalseLeft(t *testing.T) { + t1 := Float(0) + t2, _ := NewVar("testVar", "float", Float(2.7)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndFloatVarFalseBoth(t *testing.T) { + t1 := Float(0) + t2, _ := NewVar("testVar", "float", Float(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrFloatVar(t *testing.T) { + t1 := Float(1.1) + t2, _ := NewVar("testVar", "float", Float(2.1)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatVarFalseRight(t *testing.T) { + t1 := Float(1.4) + t2, _ := NewVar("testVar", "float", Float(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatVarFalseLeft(t *testing.T) { + t1 := Float(0) + t2, _ := NewVar("testVar", "float", Float(2.7)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatVarFalseBoth(t *testing.T) { + t1 := Float(0) + t2, _ := NewVar("testVar", "float", Float(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestXorFloatVar(t *testing.T) { + t1 := Float(1.5) + t2, _ := NewVar("testVar", "float", Float(2.5)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatVarFalseLeft(t *testing.T) { + t1 := Float(0) + t2, _ := NewVar("testVar", "float", Float(2.7)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatVarFalseRight(t *testing.T) { + t1 := Float(1.3) + t2, _ := NewVar("testVar", "float", Float(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatVarFalseBoth(t *testing.T) { + t1 := Float(0) + t2, _ := NewVar("testVar", "float", Float(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Float interacts with Any + +func TestAddFloatAny(t *testing.T) { + t1 := Float(1.4) + t2 := NewAny(Float(2.2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(3.6) { + t.Error("Expected 3.6, got ", result) + } +} + +func TestAddNegFloatAny(t *testing.T) { + t1 := Float(1.1) + t2 := NewAny(Float(-2.2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-1.1) { + t.Error("Expected -1.1, got ", result) + } +} + +//func TestSubFloatAny(t *testing.T) { +// t1 := Float(4.1) +// t2 := NewAny(Float(3.0)) +// var a float32 = 4.1 +// var b float32 = 3.0 +// var expected = float32(a - b) +// +// result, err := t1.Sub(t2) +// if err != nil { +// t.Error(err) +// } +// if result.GetValue() != expected { +// t.Errorf("Expected %f, got %f", expected, result.GetValue()) +// } +//} + +func TestNegSubFloatAny(t *testing.T) { + t1 := Float(4.1) + t2 := NewAny(Float(-3.0)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(7.1) { + t.Error("Expected 7.1, got ", result) + } +} + +func TestMulFloatAny(t *testing.T) { + t1 := Float(4.1) + t2 := NewAny(Int(2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(8.2) { + t.Error("Expected 8.2, got ", result) + } +} + +func TestMulNegFloatAny(t *testing.T) { + t1 := Float(4.1) + t2 := NewAny(Int(-2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-8.2) { + t.Error("Expected -8.2, got ", result) + } +} + +func TestDivFloatAny(t *testing.T) { + t1 := Float(4.2) + t2 := NewAny(Int(2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(2.1) { + t.Error("Expected 2.1, got ", result) + } +} + +func TestDivNegFloatAny(t *testing.T) { + t1 := Float(4.2) + t2 := NewAny(Int(-2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-2.1) { + t.Error("Expected -2.1, got ", result) + } +} + +func TestEqFloatAny(t *testing.T) { + t1 := Float(1.1) + t2 := NewAny(Float(1.1)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqFloatFalseAny(t *testing.T) { + t1 := Float(1.2) + t2 := NewAny(Float(2.2)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqFloatAny(t *testing.T) { + t1 := Float(1.3) + t2 := NewAny(Float(2.3)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqFloatAnyFalse(t *testing.T) { + t1 := Float(1.4) + t2 := NewAny(Float(1.4)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtFloatAnyTrue(t *testing.T) { + t1 := Float(2.5) + t2 := NewAny(Float(1.5)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtFloatAnyFalse(t *testing.T) { + t1 := Float(1.6) + t2 := NewAny(Float(2.6)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtFloatAnyEq(t *testing.T) { + t1 := Float(1.7) + t2 := NewAny(Float(1.7)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqFloatAny(t *testing.T) { + t1 := Float(1.8) + t2 := NewAny(Float(0.8)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatAnyEq(t *testing.T) { + t1 := Float(1.9) + t2 := NewAny(Float(1.9)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqFloatAnyFalse(t *testing.T) { + t1 := Float(1.1) + t2 := NewAny(Float(2.1)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwFloatAny(t *testing.T) { + t1 := Float(1.2) + t2 := NewAny(Float(2.2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwFloatAnyFalse(t *testing.T) { + t1 := Float(2.3) + t2 := NewAny(Float(1.3)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwFloatAnyEq(t *testing.T) { + t1 := Float(2.4) + t2 := NewAny(Float(2.4)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqFloatAny(t *testing.T) { + t1 := Float(2.5) + t2 := NewAny(Float(3.5)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqFloatAnyFalse(t *testing.T) { + t1 := Float(2.6) + t2 := NewAny(Float(1.6)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqFloatAnyEq(t *testing.T) { + t1 := Float(2.7) + t2 := NewAny(Float(2.7)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatAny(t *testing.T) { + t1 := Float(1.8) + t2 := NewAny(Float(2.8)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndFloatAnyFalseRight(t *testing.T) { + t1 := Float(1.9) + t2 := NewAny(Float(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndFloatAnyFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := NewAny(Float(2.1)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndFloatAnyFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := NewAny(Float(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrFloatAny(t *testing.T) { + t1 := Float(1.2) + t2 := NewAny(Float(2.2)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatAnyFalseRight(t *testing.T) { + t1 := Float(1.3) + t2 := NewAny(Float(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatAnyFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := NewAny(Float(2.4)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrFloatAnyFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := NewAny(Float(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestXorFloatAny(t *testing.T) { + t1 := Float(1.5) + t2 := NewAny(Float(2.5)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatAnyFalseLeft(t *testing.T) { + t1 := Float(0) + t2 := NewAny(Float(2.6)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatAnyFalseRight(t *testing.T) { + t1 := Float(1.7) + t2 := NewAny(Float(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorFloatAnyFalseBoth(t *testing.T) { + t1 := Float(0) + t2 := NewAny(Float(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} diff --git a/interpreter/eclaType/function.go b/interpreter/eclaType/function.go index 13f49a2..01fab2d 100644 --- a/interpreter/eclaType/function.go +++ b/interpreter/eclaType/function.go @@ -2,6 +2,7 @@ package eclaType import ( "errors" + "github.com/Eclalang/Ecla/interpreter/eclaDecl" "github.com/Eclalang/Ecla/interpreter/utils" "github.com/Eclalang/Ecla/parser" @@ -149,9 +150,9 @@ func NewFunction(Name string, args []parser.FunctionParams, body []parser.Node, argsList = append(argsList, args) argsString := generateArgsString(args) var returnMap = make(map[string][]string) - returnMap[argsString] = ret + returnMap[argsString] = ret // associating a set of return types with args for overloading var bodyMap = make(map[string][]parser.Node) - bodyMap[argsString] = body + bodyMap[argsString] = body // associating a body with args for overloading return &Function{ Name: Name, Args: argsList, @@ -271,7 +272,7 @@ func (f *Function) TypeAndNumberOfArgsIsCorrect(args []Type, StructDecl []eclaDe if paramType == parser.Any { tp = parser.Any } - if tp != paramType { + if tp != paramType { //TODO investigate isImplemented := false for _, decl := range StructDecl { if decl.GetName() == paramType { @@ -284,7 +285,7 @@ func (f *Function) TypeAndNumberOfArgsIsCorrect(args []Type, StructDecl []eclaDe } } v, err := NewVar(paramName, tp, elem) - if err != nil { + if err != nil { //TODO we can never reach this panic(err) } argsType[paramName] = v @@ -294,7 +295,6 @@ func (f *Function) TypeAndNumberOfArgsIsCorrect(args []Type, StructDecl []eclaDe } func (f *Function) CheckReturn(ret []Type, StructDecl []eclaDecl.TypeDecl) bool { - key := generateArgsString(f.Args[f.lastIndexOfArgs]) if len(f.Return[key]) != len(ret) { return false diff --git a/interpreter/eclaType/functionBuiltIn_test.go b/interpreter/eclaType/functionBuiltIn_test.go new file mode 100644 index 0000000..e4ad4fb --- /dev/null +++ b/interpreter/eclaType/functionBuiltIn_test.go @@ -0,0 +1,439 @@ +package eclaType + +import ( + "github.com/Eclalang/Ecla/interpreter/utils" + "github.com/Eclalang/Ecla/parser" + "testing" +) + +func TestNewTypeOf(t *testing.T) { + typeof := NewTypeOf() + + // test assign + if typeof == nil { + t.Error("Expected functionBuiltIn, got ", typeof) + } + + // test name + if typeof.Name != "typeOf" || typeof.f == nil { + t.Error("typeOf assignment went wrong") + } + + // test func with int + var arr []Type + arr = append(arr, Int(0)) + result1, err1 := typeof.f(arr) + if err1 != nil { + t.Error(err1) + } + + if len(result1) != 1 { + t.Error("typeOf should return exactly one argument") + } + if result1[0].String() != parser.Int { + t.Errorf("Expected %s, got %s", parser.Int, result1[0].String()) + } + + //test func case err + arr = append(arr, Int(0)) + _, err2 := typeof.f(arr) + if err2 == nil { + t.Error("Expected error when getting type of several args") + } +} + +func TestNewSizeOf(t *testing.T) { + sizeof := NewSizeOf() + + // test assign + if sizeof == nil { + t.Error("Expected functionBuiltIn, got ", sizeof) + } + + // test name + if sizeof.Name != "sizeOf" || sizeof.f == nil { + t.Error("sizeOf assignment went wrong") + } + + // test func with int + var arr []Type + arr = append(arr, Int(0)) + result1, err1 := sizeof.f(arr) + if err1 != nil { + t.Error(err1) + } + + if len(result1) != 1 { + t.Error("sizeOf should return exactly one argument") + } + switch result1[0].(type) { + case Int: + if result1[0].(Int) != Int(Int(0).GetSize()) { + t.Errorf("Expected %d, got %d", Int(Int(0).GetSize()), result1[0].(Int)) + } + default: + t.Errorf("Expected %T, got %T", Int(0), result1[0]) + } + + //test func case err + arr = append(arr, Int(0)) + _, err2 := sizeof.f(arr) + if err2 == nil { + t.Error("Expected error when getting size of several args") + } +} + +func TestNewLen(t *testing.T) { + foo := NewLen() + + // test allocation + if foo == nil { + t.Error("Expected functionBuiltIn, got nil") + } + + // test name & f + if foo.Name != "len" || foo.f == nil { + t.Error("Error when creating Len") + } + + // test err + var arr []Type + _, err := foo.f(arr) + if err == nil { + t.Error("Expected error when calling len with no args") + } + + // test len + var list, err1 = NewList(parser.Int) + if err1 != nil { + t.Error(err1) + } + arr = append(arr, list) + result, err2 := foo.f(arr) + if err2 != nil { + t.Error(err2) + } + + if len(result) != 1 { + t.Error("len should return exactly one argument") + } + switch result[0].(type) { + case Int: + if result[0].(Int) != Int(0) { + t.Errorf("Expected %d, got %d", Int(0), result[0].(Int)) + } + default: + t.Errorf("Expected %T, got %T", Int(0), result[0]) + } +} + +func TestAppend(t *testing.T) { + foo := NewAppend() + + //test allocation + if foo == nil { + t.Error("Expected FunctionBuiltIn, got nil") + } + + //test name & f + if foo.Name != "append" || foo.f == nil { + t.Error("Error when creating append") + } + + //test number of args error + var arr []Type + _, err := foo.f(arr) + if err == nil { + t.Error("Expected error when appending without args") + } + + //test append + list, errList := NewList("[]int") + if errList != nil { + t.Error(errList) + } + arr = append(arr, list) + + listToAppend := &List{[]Type{Int(0)}, "[]" + parser.Int} + arr = append(arr, listToAppend) + + result, errResult := foo.f(arr) + if errResult != nil { + t.Error(errResult) + return + } + + if len(result) != 1 { + t.Error("Expected 1, got ", len(result)) + } + switch result[0].(type) { + case *List: + if len(result[0].(*List).Value) != 1 { + t.Error("Expected 1, got ", len(result[0].(*List).Value)) + } + } + + // test type error + var args []Type + args = append(args, Int(0)) + args = append(args, Int(0)) + _, err = foo.f(args) + if err == nil { + t.Error("Expected error when appending int to int") + } +} + +func TestCall(t *testing.T) { + typeof := NewTypeOf() + + // test assign + if typeof == nil { + t.Error("Expected functionBuiltIn, got ", typeof) + } + + // test name + if typeof.Name != "typeOf" || typeof.f == nil { + t.Error("typeOf assignment went wrong") + } + + // test func with int + var arr []Type + arr = append(arr, Int(0)) + result1, err1 := typeof.Call(arr) + if err1 != nil { + t.Error(err1) + } + + if len(result1) != 1 { + t.Error("typeOf should return exactly one argument") + } + if result1[0].String() != parser.Int { + t.Errorf("Expected %s, got %s", parser.Int, result1[0].String()) + } + + //test func case err + arr = append(arr, Int(0)) + _, err2 := typeof.f(arr) + if err2 == nil { + t.Error("Expected error when getting type of several args") + } +} + +func TestGetTypeFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + result := foo.GetType() + expected := "function()" + + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestGetStringFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + result := foo.GetString() + expected := String("function") + + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestStringFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + result := foo.String() + expected := "function" + + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestIsNullFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + result := foo.IsNull() + if result { + t.Error("expected false, got true") + } +} + +func TestGetSizeFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + result := foo.GetSize() + expected := utils.Sizeof(foo) + + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + } +} + +func TestGetValueFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + + f := foo.GetValue() + if f != foo { + t.Errorf("expected %v, got %v", foo, f) + } +} + +// test err + +func TestSetValueFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + err := foo.SetValue(nil) + if err == nil { + t.Error("Expected error when setting value of BuiltInFunction") + } +} + +func TestGetIndexFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.GetIndex(Int(0)) + if err == nil { + t.Error("Expected error when getting index of BuiltInFunction") + } +} + +func TestAddFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Add(Int(0)) + if err == nil { + t.Error("Expected error when adding to BuiltInFunction") + } +} + +func TestSubFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Sub(Int(0)) + if err == nil { + t.Error("Expected error when subtracting from BuiltInFunction") + } +} + +func TestMulFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Mul(Int(0)) + if err == nil { + t.Error("Expected error when multiplying BuiltInFunction") + } +} + +func TestDivFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Div(Int(0)) + if err == nil { + t.Error("Expected error when dividing BuiltInFunction") + } +} + +func TestDivEcFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.DivEc(Int(0)) + if err == nil { + t.Error("Expected error when getting quotient BuiltInFunction") + } +} + +func TestModFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Mod(Int(0)) + if err == nil { + t.Error("Expected error when getting remainder BuiltInFunction") + } +} + +func TestEqFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Eq(Int(0)) + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestNotEqFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.NotEq(Int(0)) + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestGtFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Gt(Int(0)) + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestGtEqFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.GtEq(Int(0)) + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestLwFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Lw(Int(0)) + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestLwEqFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.LwEq(Int(0)) + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestAndFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.And(Bool(true)) + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestOrFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Or(Bool(true)) + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestXorFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Xor(Bool(true)) + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestNotFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Not() + if err == nil { + t.Error("Expected error when comparing BuiltInFunction") + } +} + +func TestAppendFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Append(Int(0)) + if err == nil { + t.Error("Expected error when appending to BuiltInFunction") + } +} + +func TestLenFunctionBuiltIn(t *testing.T) { + foo := NewTypeOf() + _, err := foo.Len() + if err == nil { + t.Error("Expected error when getting length BuiltInFunction") + } +} diff --git a/interpreter/eclaType/function_test.go b/interpreter/eclaType/function_test.go index 7bb90fd..627a89b 100644 --- a/interpreter/eclaType/function_test.go +++ b/interpreter/eclaType/function_test.go @@ -2,77 +2,865 @@ package eclaType import ( "github.com/Eclalang/Ecla/interpreter/eclaDecl" + "github.com/Eclalang/Ecla/interpreter/utils" + "github.com/Eclalang/Ecla/lexer" + "github.com/Eclalang/Ecla/parser" "testing" ) -// Useless tests for code coverage purpose -func TestGetValue(t *testing.T) { +// function interacts with function + +func TestGenerateArgsString(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + args = append(args, parser.FunctionParams{"arg1", "string"}) + + result := generateArgsString(args) + expected := "intstring" + + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestNewFunction(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + var ret []string + ret = append(ret, "string") + var body []parser.Node + body = append( + body, parser.Literal{ + lexer.Token{ + "type", + "val", + 0, + 0}, + "type", + "val"}) + + f := NewFunction("test", args, body, ret) + + //test allocation + if f == nil { + t.Errorf("Expected function, got %v", f) + } + + //test name + if f.Name != "test" { + t.Error("Expected test, got " + f.Name) + } + + //test args + if len(f.Args) != 1 { + t.Errorf("Expected exactly 1 set of arguments, got %d", len(f.Args)) + } + for i, arg := range f.Args[0] { + if arg != args[i] { + t.Errorf("Expected %v, got %v", args[i], arg) + } + } + + //test return + if len(f.Return) != 1 { + t.Errorf("Expected exactly 1 set of return types, got %d", len(f.Return)) + } + for i, r := range f.Return["int"] { + if r != ret[i] { + t.Errorf("Expected %v, got %v", ret[i], r) + } + } + + //test body + if len(f.Body) != 1 { + t.Errorf("Expected exactly 1 body, got %d", len(f.Body)) + } + for i, b := range f.Body["int"] { + if b != body[i] { + t.Errorf("Expected %v, got %v", body[i], b) + } + } +} + +func TestNewAnonymousFunction(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + var ret []string + ret = append(ret, "string") + var body []parser.Node + body = append( + body, parser.Literal{ + lexer.Token{ + "type", + "val", + 0, + 0}, + "type", + "val"}) + + f := NewAnonymousFunction(args, body, ret) + + //test allocation + if f == nil { + t.Errorf("Expected function, got %v", f) + } + + //test name + if f.Name != "" { + t.Error("Expected \"\", got " + f.Name) + } + + //test args + if len(f.Args) != 1 { + t.Errorf("Expected exactly 1 set of arguments, got %d", len(f.Args)) + } + for i, arg := range f.Args[0] { + if arg != args[i] { + t.Errorf("Expected %v, got %v", args[i], arg) + } + } + + //test return + if len(f.Return) != 1 { + t.Errorf("Expected exactly 1 set of return types, got %d", len(f.Return)) + } + for i, r := range f.Return["int"] { + if r != ret[i] { + t.Errorf("Expected %v, got %v", ret[i], r) + } + } + + //test body + if len(f.Body) != 1 { + t.Errorf("Expected exactly 1 body, got %d", len(f.Body)) + } + for i, b := range f.Body["int"] { + if b != body[i] { + t.Errorf("Expected %v, got %v", body[i], b) + } + } +} + +func TestAddOverload(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + var ret []string + ret = append(ret, "string") + var body []parser.Node + body = append( + body, parser.Literal{ + lexer.Token{ + "type", + "val", + 0, + 0}, + "type", + "val"}) + f := NewFunction("test", nil, nil, nil) - if f.GetValue() == nil { - t.Error("Expected not nil, got nil") + if f == nil { + t.Errorf("Error when creating function") + } + + f.AddOverload(args, body, ret) + + //test args + if len(f.Args) != 2 { + t.Errorf("Expected exactly 2 set of arguments, got %d", len(f.Args)) + } + for i, arg := range f.Args[1] { + if arg != args[i] { + t.Errorf("Expected %v, got %v", args[i], arg) + } + } + + //test return + if len(f.Return) != 2 { + t.Errorf("Expected exactly 2 set of return types, got %d", len(f.Return)) + } + for i, r := range f.Return["int"] { + if r != ret[i] { + t.Errorf("Expected %v, got %v", ret[i], r) + } + } + + //test body + if len(f.Body) != 2 { + t.Errorf("Expected exactly 2 bodies, got %d", len(f.Body)) + } + for i, b := range f.Body["int"] { + if b != body[i] { + t.Errorf("Expected %v, got %v", body[i], b) + } } } -func TestSetValue(t *testing.T) { +func TestGetTypeEmptyFunction(t *testing.T) { f := NewFunction("test", nil, nil, nil) - if err := f.SetValue(nil); err == nil { - t.Errorf("Expected error, got %v", err) + if f.GetType() != "function()" { + t.Errorf("Expected function(), got %v", f.GetType()) + } +} + +func TestGetTypeWithArgsFunction(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + args = append(args, parser.FunctionParams{"arg1", "string"}) + + f := NewFunction("test", args, nil, nil) + result := f.GetType() + expected := "function(int,string)" + + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestGetTypeWithReturnsFunction(t *testing.T) { + var ret []string + ret = append(ret, "string") + ret = append(ret, "int") + + f := NewFunction("test", nil, nil, ret) + result := f.GetType() + expected := "function()(string,int)" + + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) } } -func TestString(t *testing.T) { +func TestGetValueFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if f.GetValue() != f { + t.Errorf("Expected %v, got %v", f, f.GetValue()) + } +} + +func TestStringFunction(t *testing.T) { f := NewFunction("test", nil, nil, nil) if f.String() != "function" { t.Errorf("Expected function, got %v", f.String()) } } -func TestGetString(t *testing.T) { +func TestGetStringFunction(t *testing.T) { f := NewFunction("test", nil, nil, nil) - if f.GetString() != "function" { - t.Errorf("Expected function, got %v", f.GetString().String()) + if f.GetString() != String("function") { + t.Errorf("Expected function, got %v", f.GetString()) } } -func TestGetType(t *testing.T) { +func TestIsNullFunction(t *testing.T) { f := NewFunction("test", nil, nil, nil) - if f.GetType() != "function()" { - t.Errorf("Expected function(), got %v", f.GetType()) + if f.IsNull() != false { + t.Errorf("Expected false, got %v", f.IsNull()) } } -func TestGetIndex(t *testing.T) { +func TestGetSizeFunction(t *testing.T) { f := NewFunction("test", nil, nil, nil) - expect, err := f.GetIndex(nil) - if expect != nil || err == nil { - t.Errorf("Expected nil & error, got %v & %v", expect, err) + + expected := utils.Sizeof(f) + result := f.GetSize() + if result != expected { + t.Errorf("Expected %v, got %v", expected, result) + } +} + +func TestGetBodySimple(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + var body []parser.Node + body = append( + body, parser.Literal{ + lexer.Token{ + "type", + "val", + 0, + 0}, + "type", + "val"}) + + f := NewFunction("test", args, body, nil) + + if len(f.GetBody()) != 1 { + t.Errorf("Expected exactly 1 body, got %d", len(f.Body)) + } + for i, b := range f.GetBody() { + if b != body[i] { + t.Errorf("Expected %v, got %v", body[i], b) + } + } +} + +func TestGetBodyOverload(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + var body []parser.Node + body = append( + body, parser.Literal{ + lexer.Token{ + "type", + "val", + 0, + 0}, + "type", + "val"}) + + f := NewFunction("test", args, body, nil) + f.AddOverload(nil, nil, nil) + + if len(f.GetBody()) != 1 { + t.Errorf("Expected exactly 1 bodies, got %d", len(f.GetBody())) + } + for i, b := range f.GetBody() { + if b != body[i] { + t.Errorf("Expected %v, got %v", body[i], b) + } + } +} + +func TestGetReturnSimple(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + var ret []string + ret = append(ret, "string") + + f := NewFunction("test", args, nil, ret) + + if len(f.GetReturn()) != 1 { + t.Errorf("Expected exactly 1 body, got %d", len(f.Return)) + } + for i, r := range f.GetReturn() { + if r != ret[i] { + t.Errorf("Expected %v, got %v", ret[i], r) + } + } +} + +func TestGetReturnOverload(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + var ret []string + ret = append(ret, "string") + + f := NewFunction("test", args, nil, ret) + f.AddOverload(nil, nil, nil) + + if len(f.GetReturn()) != 1 { + t.Errorf("Expected exactly 1 bodies, got %d", len(f.GetReturn())) + } + for i, r := range f.GetReturn() { + if r != ret[i] { + t.Errorf("Expected %v, got %v", ret[i], r) + } + } +} + +func TestOverride(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + f := NewFunction("test", args, nil, nil) + + var ret []string + ret = append(ret, "string") + var body []parser.Node + body = append( + body, parser.Literal{ + lexer.Token{ + "type", + "val", + 0, + 0}, + "type", + "val"}) + + err := f.Override(args, body, ret) + if err != nil { + t.Error(err) + } + + //test return + if len(f.Return) != 1 { + t.Errorf("Expected exactly 1 set of return types, got %d", len(f.Return)) + } + for i, r := range f.Return["int"] { + if r != ret[i] { + t.Errorf("Expected %v, got %v", ret[i], r) + } + } + + //test body + if len(f.Body) != 1 { + t.Errorf("Expected exactly 1 body, got %d", len(f.Body)) + } + for i, b := range f.Body["int"] { + if b != body[i] { + t.Errorf("Expected %v, got %v", body[i], b) + } + } +} + +func TestGetIndexOfArgsFalse(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "char"}) + var types []Type + types = append(types, Int(0)) + + f := NewAnonymousFunction(args, nil, nil) + + result := f.GetIndexOfArgs(types) + if result != -1 { + t.Errorf("Expected -1, got %d", result) + } +} + +func TestGetIndexOfArgsWithSeveralArgsFalse(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "char"}) + var types []Type + types = append(types, Int(0)) + types = append(types, Int(0)) + + f := NewAnonymousFunction(args, nil, nil) + + result := f.GetIndexOfArgs(types) + if result != -1 { + t.Errorf("Expected -1, got %d", result) + } +} + +func TestGetIndexOfArgsSimple(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + + f := NewAnonymousFunction(args, nil, nil) + + var types []Type + types = append(types, Int(0)) + + result := f.GetIndexOfArgs(types) + if result != 0 { + t.Errorf("Expected 0, got %d", result) + } +} + +func TestGetIndexOfArgsWithAny(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", parser.Any}) + + f := NewAnonymousFunction(args, nil, nil) + + var types []Type + types = append(types, Int(0)) + + result := f.GetIndexOfArgs(types) + if result != 0 { + t.Errorf("Expected 0, got %d", result) + } +} + +func TestGetTypeFunctionEmpty(t *testing.T) { + expected := "function()" + foo := NewFunction("test", nil, nil, nil) + result := foo.GetTypes() + + if len(result) != 1 { + t.Error("Expected exactly 1 types, got ", len(result)) + } + if expected != result[0] { + t.Errorf("Expected %s, got %s", expected, result[0]) } } -func TestIsNull(t *testing.T) { +func TestGetTypeFunctionWithArgsAndReturn(t *testing.T) { + expected := "function(int,string)(string,int)" + + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + args = append(args, parser.FunctionParams{"arg1", "string"}) + var ret []string + ret = append(ret, "string") + ret = append(ret, "int") + + foo := NewFunction("test", args, nil, ret) + result := foo.GetTypes() + + if len(result) != 1 { + t.Error("Expected exactly 1 types, got ", len(result)) + } + if expected != result[0] { + t.Errorf("Expected %s, got %s", expected, result[0]) + } +} + +func TestGetTypeFunctionsWithArgsAndReturn(t *testing.T) { + var expected []string + expected = append(expected, "function(int)(string)") + expected = append(expected, "function(double)(char)") + + var args1 []parser.FunctionParams + args1 = append(args1, parser.FunctionParams{"arg0", "int"}) + var ret1 []string + ret1 = append(ret1, "string") + + foo := NewFunction("test", args1, nil, ret1) + + var args2 []parser.FunctionParams + args2 = append(args2, parser.FunctionParams{"arg0", "double"}) + var ret2 []string + ret2 = append(ret2, "char") + + foo.AddOverload(args2, nil, ret2) + + result := foo.GetTypes() + + if len(result) != 2 { + t.Error("Expected exactly 2 types, got ", len(result)) + } + for i := 0; i < 2; i++ { + if expected[i] != result[i] { + t.Errorf("Expected \"%s\", got \"%s\"", expected[i], result[i]) + } + } +} + +func TestTypeAndNumberOfArgsIsCorrectWrongArgs(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "char"}) + foo := NewFunction("test", args, nil, nil) + + var types []Type + types = append(types, Int(0)) + + b, _ := foo.TypeAndNumberOfArgsIsCorrect(types, nil) + if b { + t.Error("Expected false, got true") + } +} + +func TestTypeAndNumberOfArgsIsCorrectSimple(t *testing.T) { + var args []parser.FunctionParams + name := "arg0" + args = append(args, parser.FunctionParams{name, "int"}) + foo := NewFunction("test", args, nil, nil) + expected := make(map[string]*Var) + v := &Var{name, Int(0)} + expected[name] = v + + var types []Type + types = append(types, Int(0)) + + b, result := foo.TypeAndNumberOfArgsIsCorrect(types, nil) + if !b { + t.Error("Expected true, got false") + } + + if len(result) != len(expected) { + t.Errorf("Expected %d, got %d", len(expected), len(result)) + } + if *(result[name]) != *(expected[name]) { + t.Errorf("Expected %v, got %v", expected[name], result[name]) + } +} + +func TestTypeAndNumberOfArgsIsCorrectSimpleVar(t *testing.T) { + var args []parser.FunctionParams + name := "arg0" + args = append(args, parser.FunctionParams{name, "int"}) + foo := NewFunction("test", args, nil, nil) + expected := make(map[string]*Var) + v := &Var{name, Int(0)} + expected[name] = v + + var types []Type + types = append(types, &Var{"test", Int(0)}) + + b, result := foo.TypeAndNumberOfArgsIsCorrect(types, nil) + if !b { + t.Error("Expected true, got false") + } + + if len(result) != len(expected) { + t.Errorf("Expected %d, got %d", len(expected), len(result)) + } + if *(result[name]) != *(expected[name]) { + t.Errorf("Expected %v, got %v", expected[name], result[name]) + } +} + +func TestTypeAndNumberOfArgsIsCorrectSimpleAny(t *testing.T) { + var args []parser.FunctionParams + name := "arg0" + args = append(args, parser.FunctionParams{name, parser.Any}) + foo := NewFunction("test", args, nil, nil) + expected := make(map[string]*Var) + v := &Var{name, &Any{Int(0), parser.Int}} + expected[name] = v + + var types []Type + types = append(types, &Any{Int(0), parser.Int}) + + b, result := foo.TypeAndNumberOfArgsIsCorrect(types, nil) + if !b { + t.Error("Expected true, got false") + } + + if len(result) != len(expected) { + t.Errorf("Expected %d, got %d", len(expected), len(result)) + } + if result[name].GetValue() != expected[name].GetValue() { + t.Errorf("Expected %v, got %v", expected[name].Value, result[name].Value) + } +} + +//func TestTypeAndNumberOfArgsIsCorrectUnimplementedArgs(t *testing.T) { +// var args []parser.FunctionParams +// structType := "test" +// args = append(args, parser.FunctionParams{"arg0", structType}) +// foo := NewFunction("test", args, nil, nil) +// +// var types []Type +// types = append(types, &Struct{nil, structType, nil}) +// +// b, _ := foo.TypeAndNumberOfArgsIsCorrect(types, nil) +// if b { +// t.Error("Expected false, got true") +// } +//} + +func TestCheckReturnDifferentLengths(t *testing.T) { + var ret []Type + ret = append(ret, Int(0)) + ret = append(ret, Int(0)) + var retStr []string + retStr = append(retStr, parser.Int) + foo := NewFunction("test", nil, nil, retStr) + + if foo.CheckReturn(ret, nil) { + t.Error("Expected false, got true") + } +} + +func TestCheckReturnSimpleCase(t *testing.T) { + var ret []Type + ret = append(ret, Int(0)) + var retStr []string + retStr = append(retStr, parser.Int) + foo := NewFunction("test", nil, nil, retStr) + + if !foo.CheckReturn(ret, nil) { + t.Error("Expected true, got false") + } +} + +func TestCheckReturnVar(t *testing.T) { + var ret []Type + ret = append(ret, &Var{"arg0", Int(0)}) + var retStr []string + retStr = append(retStr, parser.Int) + foo := NewFunction("test", nil, nil, retStr) + + if !foo.CheckReturn(ret, nil) { + t.Error("Expected true, got false") + } +} + +func TestCheckReturnAny(t *testing.T) { + var ret []Type + ret = append(ret, &Var{"arg0", Int(0)}) + var retStr []string + retStr = append(retStr, parser.Any) + foo := NewFunction("test", nil, nil, retStr) + + if !foo.CheckReturn(ret, nil) { + t.Error("Expected true, got false") + } +} + +func TestCheckReturnSimpleStructNotImplemented(t *testing.T) { + var ret []Type + ret = append(ret, Int(0)) + var retStr []string + retStr = append(retStr, "test") + foo := NewFunction("test", nil, nil, retStr) + + if foo.CheckReturn(ret, nil) { + t.Error("Expected false, got true") + } +} + +func TestCheckReturnSimpleStructImplemented(t *testing.T) { + structType := "test" + var ret []Type + ret = append(ret, Int(0)) + var retStr []string + retStr = append(retStr, structType) + foo := NewFunction("test", nil, nil, retStr) + var structDecl []eclaDecl.TypeDecl + structDecl = append(structDecl, &eclaDecl.StructDecl{nil, nil, structType}) + + if !foo.CheckReturn(ret, structDecl) { + t.Error("Expected true, got false") + } +} + +// Test errors in function + +func TestSetValueFunction(t *testing.T) { f := NewFunction("test", nil, nil, nil) - if f.IsNull() { - t.Errorf("Expected false, got %v", f.IsNull()) + if err := f.SetValue(nil); err == nil { + t.Errorf("Expected error when setting value of function") } } -func TestNewFunction(t *testing.T) { +func TestGetIndexFunction(t *testing.T) { f := NewFunction("test", nil, nil, nil) - if f == nil { - t.Errorf("Expected not nil, got %v", f) + if _, err := f.GetIndex(Int(0)); err == nil { + t.Errorf("Expected error when getting index of function") } } -func TestTypeAndNumberOfArgsIsCorrect(t *testing.T) { - var structDecl []eclaDecl.TypeDecl +func TestAddFunction(t *testing.T) { f := NewFunction("test", nil, nil, nil) - var args []Type - test, expect := f.TypeAndNumberOfArgsIsCorrect(args, structDecl) - if !test || expect == nil { - t.Errorf("Expected true, got %v", test) + if _, err := f.Add(Int(0)); err == nil { + t.Errorf("Expected error when adding an int to a function") + } +} + +func TestSubFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Sub(Int(0)); err == nil { + t.Errorf("Expected error when subtracting an int from a function") + } +} + +func TestMulFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Mul(Int(0)); err == nil { + t.Errorf("Expected error when multiplying a function by an int") + } +} + +func TestDivFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Div(Int(0)); err == nil { + t.Errorf("Expected error when dividing a function by an int") + } +} + +func TestDivEcFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.DivEc(Int(0)); err == nil { + t.Errorf("Expected error when getting quotient of a function by an int") + } +} + +func TestModFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Mod(Int(0)); err == nil { + t.Errorf("Expected error when getting remainder of a function by an int") + } +} + +func TestOrFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Or(Bool(true)); err == nil { + t.Errorf("Expected error when comparing a function") + } +} + +func TestXorFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Xor(Bool(true)); err == nil { + t.Errorf("Expected error when comparing a function") + } +} + +func TestAndFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.And(Bool(true)); err == nil { + t.Errorf("Expected error when comparing a function") + } +} + +func TestNotFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Not(); err == nil { + t.Errorf("Expected error when getting \"not\" of a function") + } +} + +func TestEqFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Eq(Int(0)); err == nil { + t.Errorf("Expected error when comparing a function") + } +} + +func TestNotEqFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.NotEq(Int(0)); err == nil { + t.Errorf("Expected error when comparing a function") + } +} + +func TestGtFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Gt(Int(0)); err == nil { + t.Errorf("Expected error when comparing a function") + } +} + +func TestGtEqFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.GtEq(Int(0)); err == nil { + t.Errorf("Expected error when comparing a function") + } +} + +func TestLwFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Lw(Int(0)); err == nil { + t.Errorf("Expected error when comparing a function") + } +} + +func TestLwEqFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.LwEq(Int(0)); err == nil { + t.Errorf("Expected error when comparing a function") + } +} + +func TestAppendFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Append(Int(0)); err == nil { + t.Errorf("Expected error when appending to a function") + } +} + +func TestLenFunction(t *testing.T) { + f := NewFunction("test", nil, nil, nil) + if _, err := f.Len(); err == nil { + t.Errorf("Expected error when getting length of a function") + } +} + +func TestOverrideError(t *testing.T) { + var args []parser.FunctionParams + args = append(args, parser.FunctionParams{"arg0", "int"}) + f := NewFunction("test", args, nil, nil) + args = append(args, parser.FunctionParams{"arg1", "string"}) + + err := f.Override(args, nil, nil) + if err == nil { + t.Errorf("Expected error when overriding non-existing prototype") } } +/* func TestCheckReturn(t *testing.T) { f := NewFunction("test", nil, nil, nil) var structDecl []eclaDecl.TypeDecl @@ -82,3 +870,4 @@ func TestCheckReturn(t *testing.T) { t.Errorf("Expected true, got %v", expect) } } +*/ diff --git a/interpreter/eclaType/int_test.go b/interpreter/eclaType/int_test.go index bfba80a..1fe21bf 100644 --- a/interpreter/eclaType/int_test.go +++ b/interpreter/eclaType/int_test.go @@ -1,6 +1,7 @@ package eclaType import ( + "github.com/Eclalang/Ecla/interpreter/utils" "testing" ) @@ -602,7 +603,7 @@ func TestSubIntChar(t *testing.T) { func TestModIntChar(t *testing.T) { t1 := Int(60) - t2 := Int('!') + t2 := Char('!') result, err := t1.Mod(t2) if err != nil { @@ -641,7 +642,7 @@ func TestDivIntChar(t *testing.T) { func TestDivEcIntChar(t *testing.T) { t1 := Int(67) - t2 := Int('!') + t2 := Char('!') result, err := t1.DivEc(t2) if err != nil { @@ -1067,8 +1068,48 @@ func TestIntGetString(t *testing.T) { } } +func TestIntGetSize(t *testing.T) { + t1 := Int(0) + expected := utils.Sizeof(t1) + + result := t1.GetSize() + if result != expected { + t.Errorf("expected %d, got %d", expected, result) + } +} + // test errors Int +func TestIntAndErr(t *testing.T) { + t1 := Int(5) + t2 := String("test") + + _, err := t1.And(t2) + if err == nil { + t.Error("Expected error when checking int and string") + } +} + +func TestIntOrErr(t *testing.T) { + t1 := Int(5) + t2 := String("test") + + _, err := t1.Or(t2) + if err == nil { + t.Error("Expected error when checking int or string") + } +} + +func TestIntXorErr(t *testing.T) { + t1 := Int(5) + t2 := String("test") + + _, err := t1.Xor(t2) + if err == nil { + t.Error("Expected error when checking int xor string") + } +} + func TestIntLenErr(t *testing.T) { t1 := Int(5) @@ -1166,7 +1207,7 @@ func TestDivBy0IntErr(t *testing.T) { } } -func TestDivEcBy0IntCharErr(t *testing.T) { +func TestDivBy0IntCharErr(t *testing.T) { t1 := Int(5) t2 := Char(0) @@ -1176,6 +1217,16 @@ func TestDivEcBy0IntCharErr(t *testing.T) { } } +func TestDivBy0IntFloatErr(t *testing.T) { + t1 := Int(5) + t2 := Float(0) + + _, err := t1.Div(t2) + if err == nil { + t.Error("Expected error when dividing an int by 0") + } +} + func TestModIntErr(t *testing.T) { t1 := Int(1) t2 := Bool(true) @@ -1226,9 +1277,9 @@ func TestDivEcBy0IntErr(t *testing.T) { } } -func TestDivEcBy0IntIntErr(t *testing.T) { - t1 := Int('A') - t2 := Int(0) +func TestDivEcBy0IntCharErr(t *testing.T) { + t1 := Int(1) + t2 := Char(0) _, err := t1.DivEc(t2) if err == nil { @@ -1516,6 +1567,58 @@ func TestOrIntFloatFalseBoth(t *testing.T) { } } +func TestXorIntFloat(t *testing.T) { + t1 := Int(1) + t2 := Float(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorIntFloatFalseRight(t *testing.T) { + t1 := Int(1) + t2 := Float(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorIntFloatFalseLeft(t *testing.T) { + t1 := Int(0) + t2 := Float(1) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorIntFloatFalseBoth(t *testing.T) { + t1 := Int(0) + t2 := Float(0) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + func TestEqIntFloat(t *testing.T) { t1 := Int(0) t2 := Float(0) @@ -1881,3 +1984,1021 @@ func TestXorIntBoolFalseBoth(t *testing.T) { t.Error("Expected false, got ", result) } } + +// Int interacts with Var + +func TestAddIntVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(3) { + t.Error("Expected 3, got ", result) + } +} + +func TestAddNegIntVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(-2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(-1) { + t.Error("Expected -1, got ", result) + } +} + +func TestSubIntVar(t *testing.T) { + t1 := Int(4) + t2, _ := NewVar("testVar", "int", Int(3)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestNegSubIntVar(t *testing.T) { + t1 := Int(4) + t2, _ := NewVar("testVar", "int", Int(-3)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(7) { + t.Error("Expected 7, got ", result) + } +} + +func TestModIntVar(t *testing.T) { + t1 := Int(3) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Mod(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestMulIntVar(t *testing.T) { + t1 := Int(4) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(8) { + t.Error("Expected 8, got ", result) + } +} + +func TestMulNegIntVar(t *testing.T) { + t1 := Int(4) + t2, _ := NewVar("testVar", "int", Int(-2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(-8) { + t.Error("Expected -8, got ", result) + } +} + +func TestDivIntVar(t *testing.T) { + t1 := Int(4) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(2.0) { + t.Error("Expected 2.0, got ", result) + } +} + +func TestDivNegIntVar(t *testing.T) { + t1 := Int(4) + t2, _ := NewVar("testVar", "int", Int(-2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-2.0) { + t.Error("Expected -2.0, got ", result) + } +} + +func TestDivEcIntVar(t *testing.T) { + t1 := Int(5) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(2) { + t.Error("Expected 2, got ", result) + } +} + +func TestDivEcNegIntVar(t *testing.T) { + t1 := Int(5) + t2, _ := NewVar("testVar", "int", Int(-2)) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(-2) { + t.Error("Expected -2, got ", result) + } +} + +func TestEqIntVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(1)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqIntFalseVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqIntVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqIntVarFalse(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(1)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtIntVarTrue(t *testing.T) { + t1 := Int(2) + t2, _ := NewVar("testVar", "int", Int(1)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtIntVarFalse(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtIntVarEq(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(1)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqIntVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(0)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqIntVarEq(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(1)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqIntVarFalse(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwIntVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwIntVarFalse(t *testing.T) { + t1 := Int(2) + t2, _ := NewVar("testVar", "int", Int(1)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwIntVarEq(t *testing.T) { + t1 := Int(2) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqIntVar(t *testing.T) { + t1 := Int(2) + t2, _ := NewVar("testVar", "int", Int(3)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqIntVarFalse(t *testing.T) { + t1 := Int(2) + t2, _ := NewVar("testVar", "int", Int(1)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqIntVarEq(t *testing.T) { + t1 := Int(2) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndIntVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndIntVarFalseRight(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndIntVarFalseLeft(t *testing.T) { + t1 := Int(0) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndIntVarFalseBoth(t *testing.T) { + t1 := Int(0) + t2, _ := NewVar("testVar", "int", Int(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrIntVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrIntVarFalseRight(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrIntVarFalseLeft(t *testing.T) { + t1 := Int(0) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrIntVarFalseBoth(t *testing.T) { + t1 := Int(0) + t2, _ := NewVar("testVar", "int", Int(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestXorIntVar(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorIntVarFalseLeft(t *testing.T) { + t1 := Int(0) + t2, _ := NewVar("testVar", "int", Int(2)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorIntVarFalseRight(t *testing.T) { + t1 := Int(1) + t2, _ := NewVar("testVar", "int", Int(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorIntVarFalseBoth(t *testing.T) { + t1 := Int(0) + t2, _ := NewVar("testVar", "int", Int(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +// Int interacts with Any + +func TestAddIntAnt(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(3) { + t.Error("Expected 3, got ", result) + } +} + +func TestAddNegIntAny(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(-2)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(-1) { + t.Error("Expected -1, got ", result) + } +} + +func TestSubIntAny(t *testing.T) { + t1 := Int(4) + t2 := NewAny(Int(3)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestNegSubIntAny(t *testing.T) { + t1 := Int(4) + t2 := NewAny(Int(-3)) + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(7) { + t.Error("Expected 7, got ", result) + } +} + +func TestModIntAny(t *testing.T) { + t1 := Int(3) + t2 := NewAny(Int(2)) + + result, err := t1.Mod(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestMulIntAny(t *testing.T) { + t1 := Int(4) + t2 := NewAny(Int(2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(8) { + t.Error("Expected 8, got ", result) + } +} + +func TestMulNegIntAny(t *testing.T) { + t1 := Int(4) + t2 := NewAny(Int(-2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(-8) { + t.Error("Expected -8, got ", result) + } +} + +func TestDivIntAny(t *testing.T) { + t1 := Int(4) + t2 := NewAny(Int(2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(2.0) { + t.Error("Expected 2.0, got ", result) + } +} + +func TestDivNegIntAny(t *testing.T) { + t1 := Int(4) + t2 := NewAny(Int(-2)) + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(-2.0) { + t.Error("Expected -2.0, got ", result) + } +} + +func TestDivEcIntAny(t *testing.T) { + t1 := Int(5) + t2 := NewAny(Int(2)) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(2) { + t.Error("Expected 2, got ", result) + } +} + +func TestDivEcNegIntAny(t *testing.T) { + t1 := Int(5) + t2 := NewAny(Int(-2)) + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(-2) { + t.Error("Expected -2, got ", result) + } +} + +func TestEqIntAny(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(1)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqIntFalseAny(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(2)) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqIntAny(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(2)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqIntAnyFalse(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(1)) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtIntAnyTrue(t *testing.T) { + t1 := Int(2) + t2 := NewAny(Int(1)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtIntAnyFalse(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(2)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtIntAnyEq(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(1)) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqIntAny(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(0)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqIntAnyEq(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(1)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqIntAnyFalse(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(2)) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwIntAny(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwIntAnyFalse(t *testing.T) { + t1 := Int(2) + t2 := NewAny(Int(1)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwIntAnyEq(t *testing.T) { + t1 := Int(2) + t2 := NewAny(Int(2)) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqIntAny(t *testing.T) { + t1 := Int(2) + t2 := NewAny(Int(3)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqIntAnyFalse(t *testing.T) { + t1 := Int(2) + t2 := NewAny(Int(1)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqIntAnyEq(t *testing.T) { + t1 := Int(2) + t2 := NewAny(Int(2)) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndIntAny(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(2)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndIntAnyFalseRight(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndIntAnyFalseLeft(t *testing.T) { + t1 := Int(0) + t2 := NewAny(Int(2)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestAndIntAnyFalseBoth(t *testing.T) { + t1 := Int(0) + t2 := NewAny(Int(0)) + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrIntAny(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(2)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrIntAnyFalseRight(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrIntAnyFalseLeft(t *testing.T) { + t1 := Int(0) + t2 := NewAny(Int(2)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestOrIntAnyFalseBoth(t *testing.T) { + t1 := Int(0) + t2 := NewAny(Int(0)) + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected true, got ", result) + } +} + +func TestXorIntAny(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(2)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestXorIntAnyFalseLeft(t *testing.T) { + t1 := Int(0) + t2 := NewAny(Int(2)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorIntAnyFalseRight(t *testing.T) { + t1 := Int(1) + t2 := NewAny(Int(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected false, got ", result) + } +} + +func TestXorIntAnyFalseBoth(t *testing.T) { + t1 := Int(0) + t2 := NewAny(Int(0)) + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} diff --git a/interpreter/eclaType/list_test.go b/interpreter/eclaType/list_test.go new file mode 100644 index 0000000..6357338 --- /dev/null +++ b/interpreter/eclaType/list_test.go @@ -0,0 +1,1288 @@ +package eclaType + +import ( + "github.com/Eclalang/Ecla/interpreter/utils" + "github.com/Eclalang/Ecla/parser" + "testing" +) + +// List interacts with List + +func TestListNewList(t *testing.T) { + t1, _ := NewList(parser.Int) + t2 := &List{[]Type{}, parser.Int} + + switch t1.(type) { + case *List: + if t1.(*List).Typ != t2.Typ { + t.Error("cannot compare list of " + t1.(*List).Typ + " with list of " + t2.Typ) + } + if len(t1.(*List).Value) != len(t2.Value) { + t.Error("error when creating a new list") + } + for i, v := range t1.(*List).Value { + if v != t2.Value[i] { + t.Error("error when creating a new list") + } + } + } +} + +func TestListGetValue(t *testing.T) { + t1, _ := NewList(parser.Int) + t2 := t1.GetValue() + + if t1 != t2 { + t.Error("error when getting value of list") + } +} + +func TestListSetValueWithList(t *testing.T) { + t1, _ := NewList(parser.Int) + var array []Type + array = append(array, Int(0)) + t2 := &List{array, parser.Int} + err := t1.SetValue(t2) + + if err != nil { + t.Error(err) + } + switch t1.(type) { + case *List: + if t1.(*List).Typ != t2.Typ { + t.Errorf("Expected %s, got %s", t1.(*List).Typ, t2.Typ) + } + if len(t1.(*List).Value) != len(t2.Value) { + t.Errorf("Expected list of length %d, got list of length %d", len(t1.(*List).Value), len(t2.Value)) + } + for i, elem := range t1.(*List).Value { + if elem != t2.Value[i] { + t.Error("The lists contain different elements") + } + } + default: + t.Errorf("Expected %T, got %T", t2, t1) + } +} + +func TestListSetValueWithSliceOfTypes(t *testing.T) { + t1 := &List{[]Type{}, "[]int"} + var array []Type + array = append(array, Int(0)) + err := t1.SetValue(array) + + if err != nil { + t.Error(err) + } + + if t1.Typ[2:] != array[0].GetType() { + t.Errorf("Expected %s, got %s", t1.Typ[2:], parser.Int) + } + if len(t1.Value) != len(array) { + t.Errorf("Expected list of length %d, got list of length %d", len(t1.Value), len(array)) + } + for i, elem := range t1.Value { + if elem != array[i] { + t.Error("The lists contain different elements") + } + } +} + +func TestListString(t *testing.T) { + t1 := &List{[]Type{Int(0), Int(1), Int(2)}, parser.Int} + expected := "[0, 1, 2]" + result := t1.String() + + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestListGetString(t *testing.T) { + t1 := &List{[]Type{Int(0), Int(1), Int(2)}, parser.Int} + expected := String("[0, 1, 2]") + result := t1.GetString() + + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestListGetType(t *testing.T) { + expected := "test type" + t1, err := NewList(expected) + if err != nil { + t.Error(err) + } + + result := t1.GetType() + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestListSetType(t *testing.T) { + expected := "test type" + t1 := &List{[]Type{}, "wrong type"} + + t1.SetType(expected) + if t1.Typ != expected { + t.Errorf("Expected %s, got %s", expected, t1.Typ) + } +} + +func TestListGetIndex(t *testing.T) { + expected := Int(5) + t1 := &List{[]Type{Int(3), expected}, parser.Int} + result, err := t1.GetIndex(Int(1)) + + if err != nil { + t.Error(err) + } + + if *result != expected { + t.Errorf("Expected %d, got %d", expected, *result) + } +} + +func TestListIsNull(t *testing.T) { + t1 := &List{[]Type{}, parser.Int} + + if t1.IsNull() { + t.Error("Expected false, got true") + } +} + +func TestListGetValueType(t *testing.T) { + expected := "test" + t1 := &List{[]Type{}, "[]" + expected} + result := t1.GetValueType() + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestIsListTrue(t *testing.T) { + if !IsList("[]int") { + t.Error("Expected true, got false") + } +} + +func TestIsListFalse(t *testing.T) { + if IsList("int") { + t.Error("Expected false, got true") + } +} + +func TestListLen(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2), Int(3)}, parser.Int} + expected := 3 + result, err := t1.Len() + if err != nil { + t.Error(err) + } + + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + } +} + +func TestListGetSize(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + expected := utils.Sizeof(t1) + result := t1.GetSize() + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + } +} + +func TestListMulInt(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + expected := &List{[]Type{Int(1), Int(2), Int(1), Int(2), Int(1), Int(2)}, parser.Int} + result, err := t1.Mul(Int(3)) + if err != nil { + t.Error(err) + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenRes, lenExp) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Error("The elements do not match") + } + } +} + +func TestListMulAny(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + expected := &List{[]Type{Int(1), Int(2), Int(1), Int(2), Int(1), Int(2)}, parser.Int} + result, err := t1.Mul(&Any{Int(3), parser.Int}) + if err != nil { + t.Error(err) + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenRes, lenExp) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Error("The elements do not match") + } + } +} + +func TestListMulVar(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + v := &Var{"test", Int(3)} + expected := &List{[]Type{Int(1), Int(2), Int(1), Int(2), Int(1), Int(2)}, parser.Int} + result, err := t1.Mul(v) + if err != nil { + t.Error(err) + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenRes, lenExp) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Error("The elements do not match") + } + } +} + +func TestListEqTrue(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(2)}, parser.Int} + + res, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListEqTrueAny(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &Any{Value: &List{[]Type{Int(1), Int(2)}, parser.Int}, Type: "[]" + parser.Int} + + res, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListEqTrueVar(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, "[]" + parser.Int} + t2 := &Var{"test", &List{[]Type{Int(1), Int(2)}, "[]" + parser.Int}} + + res, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListEqFalseLength(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1)}, parser.Int} + + res, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(false) { + t.Errorf("Expected false, got true") + } +} + +func TestListEqFalseDifferentElement(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(3)}, parser.Int} + + res, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(false) { + t.Errorf("Expected false, got true") + } +} + +func TestListNotEqTrueLength(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1)}, parser.Int} + + res, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListNotEqTrueDifferentElement(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(3)}, parser.Int} + + res, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListNotEqTrueAny(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &Any{Value: &List{[]Type{Int(1)}, parser.Int}, Type: "[]" + parser.Int} + + res, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListNotEqTrueVar(t *testing.T) { + t1 := &List{[]Type{Int(1)}, "[]" + parser.Int} + t2 := &Var{"test", &List{[]Type{Int(1), Int(2)}, "[]" + parser.Int}} + + res, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListNotEqFalse(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(2)}, parser.Int} + + res, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(false) { + t.Errorf("Expected false, got true") + } +} + +func TestListGtTrue(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1)}, parser.Int} + + res, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListGtFalse(t *testing.T) { + t1 := &List{[]Type{Int(1)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(2)}, parser.Int} + + res, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(false) { + t.Errorf("Expected false, got true") + } +} + +func TestListGtTrueAny(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &Any{Value: &List{[]Type{Int(1)}, parser.Int}, Type: "[]" + parser.Int} + + res, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListGtTrueVar(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, "[]" + parser.Int} + t2 := &Var{"test", &List{[]Type{Int(1)}, "[]" + parser.Int}} + + res, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListGtEqTrue(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1)}, parser.Int} + + res, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListGtEqTrueEq(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(2)}, parser.Int} + + res, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListGtEqFalse(t *testing.T) { + t1 := &List{[]Type{Int(1)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(2)}, parser.Int} + + res, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(false) { + t.Errorf("Expected false, got true") + } +} + +func TestListGtEqTrueAny(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &Any{Value: &List{[]Type{Int(1)}, parser.Int}, Type: "[]" + parser.Int} + + res, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListGtEqTrueVar(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, "[]" + parser.Int} + t2 := &Var{"test", &List{[]Type{Int(1)}, "[]" + parser.Int}} + + res, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} +func TestListLwTrue(t *testing.T) { + t1 := &List{[]Type{Int(1)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(2)}, parser.Int} + + res, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListLwFalse(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1)}, parser.Int} + + res, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(false) { + t.Errorf("Expected false, got true") + } +} + +func TestListLwTrueAny(t *testing.T) { + t1 := &List{[]Type{Int(1)}, parser.Int} + t2 := &Any{Value: &List{[]Type{Int(1), Int(2)}, parser.Int}, Type: "[]" + parser.Int} + + res, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListLwTrueVar(t *testing.T) { + t1 := &List{[]Type{Int(1)}, "[]" + parser.Int} + t2 := &Var{"test", &List{[]Type{Int(1), Int(2)}, "[]" + parser.Int}} + + res, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListLwEqTrue(t *testing.T) { + t1 := &List{[]Type{Int(1)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(2)}, parser.Int} + + res, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListLwEqTrueEq(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1), Int(2)}, parser.Int} + + res, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListLwEqFalse(t *testing.T) { + t1 := &List{[]Type{Int(1), Int(2)}, parser.Int} + t2 := &List{[]Type{Int(1)}, parser.Int} + + res, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(false) { + t.Errorf("Expected false, got true") + } +} + +func TestListLwEqTrueAny(t *testing.T) { + t1 := &List{[]Type{Int(1)}, parser.Int} + t2 := &Any{Value: &List{[]Type{Int(1), Int(2)}, parser.Int}, Type: "[]" + parser.Int} + + res, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListLwEqTrueVar(t *testing.T) { + t1 := &List{[]Type{Int(1)}, "[]" + parser.Int} + t2 := &Var{"test", &List{[]Type{Int(1), Int(2)}, "[]" + parser.Int}} + + res, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + + if res != Bool(true) { + t.Errorf("Expected true, got false") + } +} + +func TestListAddString(t *testing.T) { + t1 := &List{[]Type{Int(0), Int(1)}, parser.Int} + str := String("test") + expected := String("[0, 1]test") + + result, err := t1.Add(str) + if err != nil { + t.Error(err) + } + if result.(String) != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestListAddList(t *testing.T) { + t1 := &List{[]Type{Int(0)}, parser.Int} + t2 := &List{[]Type{Int(1)}, parser.Int} + expected := &List{[]Type{Int(0), Int(1)}, parser.Int} + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Error("The elements do not match") + } + } +} + +func TestListAddVar(t *testing.T) { + t1 := &List{[]Type{Int(0)}, parser.Int} + t2 := &Var{"test", &List{[]Type{Int(1)}, parser.Int}} + expected := &List{[]Type{Int(0), Int(1)}, parser.Int} + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Error("The elements do not match") + } + } +} + +func TestListAddAny(t *testing.T) { + t1 := &List{[]Type{Int(0)}, parser.Int} + t2 := &Any{&List{[]Type{Int(1)}, parser.Int}, parser.Int} + expected := &List{[]Type{Int(0), Int(1)}, parser.Int} + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Error("The elements do not match") + } + } +} + +func TestListAddListIntWithChar(t *testing.T) { + t1 := &List{[]Type{Int(0)}, "[]" + parser.Int} + t2 := &List{[]Type{Char('A')}, "[]" + parser.Char} + expected := &List{[]Type{Int(0), Char('A').GetValueAsInt()}, "[]" + parser.Int} + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + } + } +} + +func TestListAddListCharWithInt(t *testing.T) { + t1 := &List{[]Type{Int(66)}, "[]" + parser.Int} + t2 := &List{[]Type{Char('A')}, "[]" + parser.Char} + expected := &List{[]Type{Char('A'), Char('B')}, "[]" + parser.Char} + + result, err := t2.Add(t1) + if err != nil { + t.Error(err) + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + } + } +} + +func TestListAppendList(t *testing.T) { + t1 := &List{[]Type{Int(0)}, "[]" + parser.Int} + t2 := &List{[]Type{Int(1)}, "[]" + parser.Int} + expected := &List{[]Type{Int(0), Int(1)}, "[]" + parser.Int} + + result, err := t1.Append(t2) + if err != nil { + t.Error(err) + return + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + } + } +} + +func TestListAppendVar(t *testing.T) { + t1 := &List{[]Type{Int(0)}, "[]" + parser.Int} + t2 := &Var{"test", &List{[]Type{Int(1)}, "[]" + parser.Int}} + expected := &List{[]Type{Int(0), Int(1)}, "[]" + parser.Int} + + result, err := t1.Append(t2) + if err != nil { + t.Error(err) + return + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + } + } +} + +func TestListAppendAny(t *testing.T) { + t1 := &List{[]Type{Int(0)}, "[]" + parser.Int} + t2 := &Any{&List{[]Type{Int(1)}, "[]" + parser.Int}, "[]" + parser.Int} + expected := &List{[]Type{Int(0), Int(1)}, "[]" + parser.Int} + + result, err := t1.Append(t2) + if err != nil { + t.Error(err) + return + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + } + } +} + +func TestListAppendListInt(t *testing.T) { + t1 := &List{[]Type{Int(0)}, "[]" + parser.Int} + t2 := Int(1) + expected := &List{[]Type{Int(0), Int(1)}, "[]" + parser.Int} + + result, err := t1.Append(t2) + if err != nil { + t.Error(err) + return + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + } + } +} + +func TestListAppendListIntWithChar(t *testing.T) { + t1 := &List{[]Type{Char('A')}, "[]" + parser.Char} + t2 := &List{[]Type{Int(66)}, "[]" + parser.Int} + expected := &List{[]Type{Int(66), Int(65)}, "[]" + parser.Int} + + result, err := t2.Append(t1) + if err != nil { + t.Error(err) + return + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + } + } +} + +func TestListAppendListCharWithInt(t *testing.T) { + t1 := &List{[]Type{Char('A')}, "[]" + parser.Char} + t2 := &List{[]Type{Int(66)}, "[]" + parser.Int} + expected := &List{[]Type{Char('A'), Char('B')}, "[]" + parser.Char} + + result, err := t1.Append(t2) + if err != nil { + t.Error(err) + return + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + } + } +} + +func TestListAppendListIntChar(t *testing.T) { + t1 := &List{[]Type{Int(0)}, "[]" + parser.Int} + t2 := Char('A') + expected := &List{[]Type{Int(0), Int(65)}, "[]" + parser.Int} + + result, err := t1.Append(&t2) + if err != nil { + t.Error(err) + return + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + return + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + return + } + } +} + +func TestListAppendListCharInt(t *testing.T) { + t1 := &List{[]Type{Char('A')}, "[]" + parser.Char} + t2 := Int(66) + expected := &List{[]Type{Char('A'), Char('B')}, "[]" + parser.Char} + + result, err := t1.Append(&t2) + if err != nil { + t.Error(err) + return + } + + lenRes := len(result.(*List).Value) + lenExp := len(expected.Value) + if lenRes != lenExp { + t.Errorf("Expected list of size %d, got list of size %d", lenExp, lenRes) + } + for i, elem := range result.(*List).Value { + if elem != expected.Value[i] { + t.Errorf("The %dth elements do not match: %d, %d", i, expected.Value[i], elem) + } + } +} + +// Test List errors + +func TestListSetValueWithNonList(t *testing.T) { + t1, _ := NewList(parser.Int) + err := t1.SetValue(Int(0)) + + if err == nil { + t.Error("Expected error when setting list with non list value") + } +} + +func TestListSetValueWithListOfWrongType(t *testing.T) { + t1 := &List{[]Type{}, "[]char"} + t2 := &List{[]Type{Int(0)}, "[]int"} + err := t1.SetValue(t2) + + if err == nil { + t.Error("Expected error when setting value of list with another list of a different type") + } +} + +func TestListSetValueWithSliceOfWrongTypes(t *testing.T) { + t1 := &List{[]Type{}, "[]char"} + var array []Type + array = append(array, Int(0)) + err := t1.SetValue(array) + + if err == nil { + t.Error("Expected error when setting value of list of char with slice of int") + } +} + +func TestListGetIndexOutOfRange(t *testing.T) { + expected := Int(5) + t1 := &List{[]Type{Int(3), expected}, parser.Int} + _, err := t1.GetIndex(Int(42)) + + if err == nil { + t.Error("Expected error when getting index out of range") + } +} + +func TestListGetIndexWrongType(t *testing.T) { + expected := Int(5) + t1 := &List{[]Type{Int(3), expected}, parser.Int} + _, err := t1.GetIndex(String("this won't work")) + + if err == nil { + t.Error("Expected error when getting index with non int") + } +} + +func TestListSub(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.Sub(Int(0)) + if result == nil { + t.Error("Expected error when subtracting from list") + } +} + +func TestListMod(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.Mod(Int(0)) + if result == nil { + t.Error("Expected error when getting remainder of a list") + } +} + +func TestListDiv(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.Div(Int(0)) + if result == nil { + t.Error("Expected error when dividing list") + } +} + +func TestListDivEc(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.DivEc(Int(0)) + if result == nil { + t.Error("Expected error when getting quotient of a list") + } +} + +func TestListEqWithNonList(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.Eq(Int(0)) + if result == nil { + t.Error("Expected error when comparing list to int") + } +} + +func TestListEqWithWrongType(t *testing.T) { + t1, err := NewList(parser.Char) + if err != nil { + t.Error(err) + } + + _, result := t1.Eq(&List{[]Type{Int(1)}, parser.Int}) + if result == nil { + t.Error("Expected error when comparing list of char to list of int") + } +} + +func TestListNotEqWithNonList(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.NotEq(Int(0)) + if result == nil { + t.Error("Expected error when comparing list to int") + } +} + +func TestListNotEqWithWrongType(t *testing.T) { + t1, err := NewList(parser.Char) + if err != nil { + t.Error(err) + } + + _, result := t1.NotEq(&List{[]Type{Int(1)}, parser.Int}) + if result == nil { + t.Error("Expected error when comparing list of char to list of int") + } +} + +func TestListGtWithNonList(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.Gt(Int(0)) + if result == nil { + t.Error("Expected error when comparing list to int") + } +} + +func TestListGtWithWrongType(t *testing.T) { + t1, err := NewList(parser.Char) + if err != nil { + t.Error(err) + } + + _, result := t1.Gt(&List{[]Type{Int(1)}, parser.Int}) + if result == nil { + t.Error("Expected error when comparing list of char to list of int") + } +} + +func TestListGtEqWithNonList(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.GtEq(Int(0)) + if result == nil { + t.Error("Expected error when comparing list to int") + } +} + +func TestListGtEqWithWrongType(t *testing.T) { + t1, err := NewList(parser.Char) + if err != nil { + t.Error(err) + } + + _, result := t1.GtEq(&List{[]Type{Int(1)}, parser.Int}) + if result == nil { + t.Error("Expected error when comparing list of char to list of int") + } +} + +func TestListLwWithNonList(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.Lw(Int(0)) + if result == nil { + t.Error("Expected error when comparing list to int") + } +} + +func TestListLwWithWrongType(t *testing.T) { + t1, err := NewList(parser.Char) + if err != nil { + t.Error(err) + } + + _, result := t1.Lw(&List{[]Type{Int(1)}, parser.Int}) + if result == nil { + t.Error("Expected error when comparing list of char to list of int") + } +} + +func TestListLwEqWithNonList(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.LwEq(Int(0)) + if result == nil { + t.Error("Expected error when comparing list to int") + } +} + +func TestListLwEqWithWrongType(t *testing.T) { + t1, err := NewList(parser.Char) + if err != nil { + t.Error(err) + } + + _, result := t1.LwEq(&List{[]Type{Int(1)}, parser.Int}) + if result == nil { + t.Error("Expected error when comparing list of char to list of int") + } +} + +func TestListAnd(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.And(Int(0)) + if result == nil { + t.Error("Expected error when comparing a list") + } +} + +func TestListOr(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.Or(Int(0)) + if result == nil { + t.Error("Expected error when comparing a list") + } +} + +func TestListXor(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.Xor(Int(0)) + if result == nil { + t.Error("Expected error when comparing a list") + } +} + +func TestListNot(t *testing.T) { + t1, err := NewList("test") + if err != nil { + t.Error(err) + } + + _, result := t1.Not() + if result == nil { + t.Error("Expected error when comparing a list") + } +} + +func TestListAddNonList(t *testing.T) { + t1 := &List{[]Type{Int(0)}, parser.Int} + _, err := t1.Add(Int(1)) + + if err == nil { + t.Error("Expected error when adding an int to a list") + } +} + +func TestListAddWrongType(t *testing.T) { + t1 := &List{[]Type{Int(0)}, parser.Int} + t2 := &List{[]Type{Char('c')}, parser.Char} + _, err := t1.Add(t2) + + if err == nil { + t.Error("Expected error when adding a list of char to a list of int") + } +} + +func TestListMulWrongType(t *testing.T) { + t1 := &List{[]Type{Int(0)}, parser.Int} + _, err := t1.Mul(String("test")) + + if err == nil { + t.Error("Expected error when multiplying a list by a string") + } +} + +func TestListAppendNonList(t *testing.T) { + t1 := &List{[]Type{Int(0)}, parser.Int} + _, err := t1.Append(Int(1)) + + if err == nil { + t.Error("Expected error when appending an int to a list") + } +} diff --git a/interpreter/eclaType/map_test.go b/interpreter/eclaType/map_test.go new file mode 100644 index 0000000..56b4d38 --- /dev/null +++ b/interpreter/eclaType/map_test.go @@ -0,0 +1,765 @@ +package eclaType + +import ( + "github.com/Eclalang/Ecla/interpreter/utils" + "github.com/Eclalang/Ecla/parser" + "testing" +) + +func TestNewMap(t *testing.T) { + t1 := NewMap() + if len(t1.Keys) != 0 || t1.Keys == nil { + t.Error("Error when initializing Keys") + } + if len(t1.Values) != 0 || t1.Values == nil { + t.Error("Error when initializing Values") + } + if t1.Typ != "" { + t.Error("Error when initialising Typ") + } + if t1.TypKey != "" { + t.Error("Error when initialising TypKey") + } + if t1.TypVal != "" { + t.Error("Error when initialising TypVal") + } +} + +func TestMapGetValue(t *testing.T) { + t1 := NewMap() + t2 := t1.GetValue() + if t1 != t2 { + t.Errorf("Expected %s, got %s", t1, t2) + } +} + +func TestMapSetAutoTypeEmpty(t *testing.T) { + t1 := NewMap() + expected := "empty" + err := t1.SetAutoType() + if err != nil { + t.Error(err) + } + if t1.Typ != expected { + t.Errorf("Expected %s, got %s", expected, t1.Typ) + } +} + +func TestMapSetAutoTypeIntString(t *testing.T) { + t1 := &Map{[]Type{Int(1)}, []Type{String("One")}, "", parser.Int, parser.String} + err := t1.SetAutoType() + if err != nil { + t.Error(err) + } + expected := "map[int]string" + + if t1.Typ != expected { + t.Errorf("Expected %s, got %s", expected, t1.Typ) + } +} + +func TestMapSetValue(t *testing.T) { + t1 := NewMap() + t2 := &Map{[]Type{Int(1)}, []Type{String("One")}, "test", parser.Int, parser.String} + err := t1.SetValue(t2) + if err != nil { + t.Error(err) + } + if t1.Typ != t2.Typ || t1.TypVal != t2.TypVal || t1.TypKey != t2.TypKey { + t.Error("The types do not match") + } + if len(t1.Keys) != len(t2.Keys) { + t.Error("There is a different number of keys") + } + for i, elem := range t1.Keys { + if elem != t2.Keys[i] { + t.Error("The keys do not match") + } + } + if len(t1.Values) != len(t2.Values) { + t.Error("There is a different number of values") + } + for i, elem := range t1.Values { + if elem != t2.Values[i] { + t.Error("The values do not match") + } + } +} + +func TestMapSetValueAny(t *testing.T) { + t1 := NewMap() + t2 := &Map{[]Type{Int(1)}, []Type{String("One")}, "test", parser.Int, parser.String} + a := &Any{t2, t2.Typ} + err := t1.SetValue(a) + if err != nil { + t.Error(err) + } + if t1.Typ != t2.Typ || t1.TypVal != t2.TypVal || t1.TypKey != t2.TypKey { + t.Error("The types do not match") + } + if len(t1.Keys) != len(t2.Keys) { + t.Error("There is a different number of keys") + } + for i, elem := range t1.Keys { + if elem != t2.Keys[i] { + t.Error("The keys do not match") + } + } + if len(t1.Values) != len(t2.Values) { + t.Error("There is a different number of values") + } + for i, elem := range t1.Values { + if elem != t2.Values[i] { + t.Error("The values do not match") + } + } +} + +func TestMapString(t *testing.T) { + t1 := &Map{[]Type{Bool(true), Bool(false)}, []Type{Int(1), Int(0)}, "", "", ""} + expected := "{true: 1, false: 0}" + result := t1.String() + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapGetString(t *testing.T) { + t1 := &Map{[]Type{Bool(true), Bool(false)}, []Type{Int(1), Int(0)}, "", "", ""} + expected := String("{true: 1, false: 0}") + result := t1.GetString() + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapGetType(t *testing.T) { + t1 := NewMap() + expected := "test" + t1.Typ = expected + result := t1.GetType() + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapGetTypeOfKeyAndValue(t *testing.T) { + str := "map[int]string" + resultKey, resultVal := GetTypeOfKeyAndValue(str) + if resultKey != "int" { + t.Errorf("expected key: %s, got %s", "int", resultKey) + } + if resultVal != "string" { + t.Errorf("expected val: %s, got %s", "string", resultVal) + } +} + +func TestMapEqTrue(t *testing.T) { + t1 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + t2 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + b, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if b != Bool(true) { + t.Error("Expected true, got false") + } +} + +func TestMapEqTrueAny(t *testing.T) { + t1 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + t2 := &Any{&Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"}, "test"} + b, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if b != Bool(true) { + t.Error("Expected true, got false") + } +} + +func TestMapEqTrueVar(t *testing.T) { + t1 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + t2 := &Var{"var", &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"}} + b, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if b != Bool(true) { + t.Error("Expected true, got false") + } +} + +func TestMapEqFalseLength(t *testing.T) { + t1 := &Map{[]Type{Int(1), Int(2)}, []Type{String("test")}, "type", "keys", "values"} + t2 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + b, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if b != Bool(false) { + t.Error("Expected false, got true") + } +} + +func TestMapEqFalseKey(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{String("test")}, "type", "keys", "values"} + t2 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + b, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if b != Bool(false) { + t.Error("Expected false, got true") + } +} + +func TestMapEqFalseValue(t *testing.T) { + t1 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + t2 := &Map{[]Type{Int(1)}, []Type{String("fail")}, "type", "keys", "values"} + b, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if b != Bool(false) { + t.Error("Expected false, got true") + } +} + +func TestMapNotEqTrue(t *testing.T) { + t1 := &Map{[]Type{Int(1), Int(2)}, []Type{String("test")}, "type", "keys", "values"} + t2 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + b, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if b != Bool(true) { + t.Error("Expected true, got false") + } +} + +func TestMapNotEqFalse(t *testing.T) { + t1 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + t2 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + b, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if b != Bool(false) { + t.Error("Expected false, got true") + } +} + +func TestMapIsNull(t *testing.T) { + t1 := NewMap() + if t1.IsNull() { + t.Error("Expected true, got false") + } +} + +func TestIsMapTrue(t *testing.T) { + str := "map[int]string" + if !IsMap(str) { + t.Error("Expected true, got false") + } +} + +func TestIsMapFalseLength(t *testing.T) { + str := "map" + if IsMap(str) { + t.Error("Expected false, got true") + } +} + +func TestIsMapFalseType(t *testing.T) { + str := "123456" + if IsMap(str) { + t.Error("Expected false, got true") + } +} + +func TestMapGetSize(t *testing.T) { + t1 := &Map{[]Type{Int(1)}, []Type{String("test")}, "type", "keys", "values"} + expected := utils.Sizeof(t1) + result := t1.GetSize() + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + } +} + +func TestMapLen(t *testing.T) { + t1 := &Map{[]Type{Int(1), Int(2), Int(3)}, []Type{}, "", "", ""} + expected := 3 + result, err := t1.Len() + if err != nil { + t.Error(err) + } + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + } +} + +func TestMapSetType(t *testing.T) { + str := "map[int]string" + t1 := NewMap() + t1.SetType(str) + if t1.Typ != str { + t.Error("Error in type") + } + if t1.TypKey != "int" { + t.Error("Error in key type") + } + if t1.TypVal != "string" { + t.Error("Error in value type") + } +} + +func TestMapSetNewKey(t *testing.T) { + t1 := &Map{[]Type{}, []Type{}, "map[int]string", parser.Int, parser.String} + t1.Set(Int(0), String("0")) + if len(t1.Values) == 0 || len(t1.Keys) == 0 { + t.Error("Value was not added") + } +} + +func TestMapSetReplaceKey(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{String("zero")}, "map[int]string", parser.Int, parser.String} + t1.Set(Int(0), String("0")) + if len(t1.Values) == 0 || len(t1.Keys) == 0 { + t.Error("Value was not added") + } +} + +func TestMapAddKey(t *testing.T) { + t1 := &Map{[]Type{}, []Type{}, "map[int]string", parser.Int, parser.String} + err := t1.AddKey(Int(0)) + if err != nil { + t.Error(err) + } + if len(t1.Keys) != 1 || len(t1.Values) != 1 { + t.Error("The key was not added properly") + } + if t1.Keys[0] != Int(0) { + t.Errorf("Expected %s, got %s", Int(0), t1.Keys) + } + if !t1.Values[0].IsNull() { + t.Errorf("Expected null, got %s", t1.Values[0]) + } +} + +func TestMapGetTrue(t *testing.T) { + key := Int(0) + expected := String("zero") + t1 := &Map{[]Type{key}, []Type{expected}, "map[int]string", parser.Int, parser.String} + result, b := t1.Get(key) + if b != true { + t.Error("Should have found the value, but didn't") + } + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapGetFalse(t *testing.T) { + key := Int(0) + expected := String("zero") + t1 := &Map{[]Type{key}, []Type{expected}, "map[int]string", parser.Int, parser.String} + _, b := t1.Get(Int(1)) + if b != false { + t.Error("Should not have found the value, but did") + } +} + +func TestMapGetIndex(t *testing.T) { + key := Int(0) + expected := String("zero") + t1 := &Map{[]Type{key}, []Type{expected}, "map[int]string", parser.Int, parser.String} + result, err := t1.GetIndex(key) + if err != nil { + t.Error("Should have found the value, but didn't") + } + if *result != expected { + t.Errorf("Expected %s, got %s", expected, *result) + } +} + +func TestMapGetKey(t *testing.T) { + expected := Int(0) + value := String("zero") + t1 := &Map{[]Type{expected}, []Type{value}, "map[int]string", parser.Int, parser.String} + result, b := t1.GetKey(value) + if !b { + t.Error("Should have found the value, but didn't") + } + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapGetKeyFalse(t *testing.T) { + key := Int(0) + value := String("zero") + t1 := &Map{[]Type{key}, []Type{value}, "map[int]string", parser.Int, parser.String} + _, b := t1.GetKey(Int(1)) + if b { + t.Error("Should not have found the value, but did") + } +} + +func TestMapAddMap(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{String("0")}, "map[int]string", parser.Int, parser.String} + t2 := &Map{[]Type{Int(1)}, []Type{String("1")}, "map[int]string", parser.Int, parser.String} + expected := &Map{[]Type{Int(0), Int(1)}, []Type{String("0"), String("1")}, "map[int]string", parser.Int, parser.String} + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + b, e := result.Eq(expected) + if e != nil { + t.Error(e) + } + if b != Bool(true) { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapAddVar(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{String("0")}, "map[int]string", parser.Int, parser.String} + t2 := &Var{"test", &Map{[]Type{Int(1)}, []Type{String("1")}, "map[int]string", parser.Int, parser.String}} + expected := &Map{[]Type{Int(0), Int(1)}, []Type{String("0"), String("1")}, "map[int]string", parser.Int, parser.String} + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + b, e := result.Eq(expected) + if e != nil { + t.Error(e) + } + if b != Bool(true) { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapAddAny(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{String("0")}, "map[int]string", parser.Int, parser.String} + t2 := &Any{&Map{[]Type{Int(1)}, []Type{String("1")}, "map[int]string", parser.Int, parser.String}, "test"} + expected := &Map{[]Type{Int(0), Int(1)}, []Type{String("0"), String("1")}, "map[int]string", parser.Int, parser.String} + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + b, e := result.Eq(expected) + if e != nil { + t.Error(e) + } + if b != Bool(true) { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapAddString(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{String("0")}, "map[int]string", parser.Int, parser.String} + t2 := String("test") + expected := String("{0: 0}test") + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapDelete(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{String("0")}, "map[int]string", parser.Int, parser.String} + expected := NewMap() + t1.Delete(Int(0)) + b, err := t1.Eq(expected) + if err != nil { + t.Error(err) + } + if b != Bool(true) { + t.Error("The values were not removed") + } +} + +func TestMapSub(t *testing.T) { + t1 := &Map{[]Type{Int(0), Int(1)}, []Type{String("0"), String("1")}, "map[int]string", parser.Int, parser.String} + t2 := &Map{[]Type{Int(0)}, []Type{String("0")}, "map[int]string", parser.Int, parser.String} + expected := &Map{[]Type{Int(1)}, []Type{String("1")}, "map[int]string", parser.Int, parser.String} + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + b, e := result.Eq(expected) + if e != nil { + t.Error(e) + } + if b != Bool(true) { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapSubVar(t *testing.T) { + t1 := &Map{[]Type{Int(0), Int(1)}, []Type{String("0"), String("1")}, "map[int]string", parser.Int, parser.String} + t2 := &Var{"test", &Map{[]Type{Int(0)}, []Type{String("0")}, "map[int]string", parser.Int, parser.String}} + expected := &Map{[]Type{Int(1)}, []Type{String("1")}, "map[int]string", parser.Int, parser.String} + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + b, e := result.Eq(expected) + if e != nil { + t.Error(e) + } + if b != Bool(true) { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestMapSubAny(t *testing.T) { + t1 := &Map{[]Type{Int(0), Int(1)}, []Type{String("0"), String("1")}, "map[int]string", parser.Int, parser.String} + t2 := &Any{&Map{[]Type{Int(0)}, []Type{String("0")}, "map[int]string", parser.Int, parser.String}, "test"} + expected := &Map{[]Type{Int(1)}, []Type{String("1")}, "map[int]string", parser.Int, parser.String} + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + b, e := result.Eq(expected) + if e != nil { + t.Error(e) + } + if b != Bool(true) { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +// Test Map errors + +func TestMapSetAutoTypeErrorKeys(t *testing.T) { + t1 := &Map{[]Type{Int(1), Bool(false)}, []Type{Int(1), Int(2)}, "", "", ""} + err := t1.SetAutoType() + if err == nil { + t.Error("Expected error when creating map with wrong type of keys") + } +} + +func TestMapSetAutoTypeErrorValues(t *testing.T) { + t1 := &Map{[]Type{Int(1), Int(2)}, []Type{Int(1), Bool(false)}, "", "", ""} + err := t1.SetAutoType() + if err == nil { + t.Error("Expected error when creating map with wrong type of keys") + } +} + +func TestMapSetValueNonMap(t *testing.T) { + t1 := NewMap() + err := t1.SetValue(Int(0)) + if err == nil { + t.Error("Expected error when setting value with Int") + } +} + +func TestMapSetErrKeys(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Error("Expected a panic when adding a bool as key in a map[test]int") + } + }() + + t1 := &Map{[]Type{}, []Type{}, "", "test", parser.Int} + t1.Set(Bool(false), Int(0)) +} + +func TestMapSetErrValues(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Error("Expected a panic when adding a bool as value in a map[int]test") + } + }() + + t1 := &Map{[]Type{}, []Type{}, "", parser.Int, "test"} + t1.Set(Int(0), Bool(false)) +} + +func TestAddKeyError(t *testing.T) { + t1 := &Map{[]Type{}, []Type{}, "", "test", parser.Int} + err := t1.AddKey(Int(0)) + if err == nil { + t.Error("Expected error when adding int as key of map[test]int") + } +} + +func TestGetIndexErrorType(t *testing.T) { + t1 := &Map{[]Type{}, []Type{}, "", parser.Int, parser.Int} + _, err := t1.GetIndex(Bool(false)) + if err == nil { + t.Error("Expected error when getting index of bool in map[int]int") + } +} + +func TestGetIndexNotFound(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{Int(1)}, "", parser.Int, parser.Int} + _, err := t1.GetIndex(Int(-1)) + if err == nil { + t.Error("Expected error when getting index of non existent key") + } +} + +func TestMapAddNonMap(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{Int(1)}, "", parser.Int, parser.Int} + _, err := t1.Add(Int(0)) + if err == nil { + t.Error("Expected error when adding int to map") + } +} + +func TestMapAddMapWrongType(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{Int(1)}, "test", parser.Int, parser.Int} + t2 := &Map{[]Type{Bool(true)}, []Type{Int(1)}, "different type", parser.Bool, parser.Int} + _, err := t1.Add(t2) + if err == nil { + t.Error("Expected error when adding int to map") + } +} + +func TestMapSubMapWrongType(t *testing.T) { + t1 := &Map{[]Type{Int(0)}, []Type{Int(1)}, "test", parser.Int, parser.Int} + t2 := &Map{[]Type{Bool(true)}, []Type{Int(1)}, "different type", parser.Bool, parser.Int} + _, err := t1.Sub(t2) + if err == nil { + t.Error("Expected error when adding int to map") + } +} + +func TestMapMulErr(t *testing.T) { + t1 := NewMap() + _, err := t1.Mul(Int(2)) + if err == nil { + t.Error("Expected error when multiplying a map") + } +} + +func TestMapDivErr(t *testing.T) { + t1 := NewMap() + _, err := t1.Div(Int(2)) + if err == nil { + t.Error("Expected error when dividing a map") + } +} + +func TestMapModErr(t *testing.T) { + t1 := NewMap() + _, err := t1.Mod(Int(2)) + if err == nil { + t.Error("Expected error when getting remainder of a map") + } +} + +func TestMapDivEcErr(t *testing.T) { + t1 := NewMap() + _, err := t1.DivEc(Int(2)) + if err == nil { + t.Error("Expected error when getting quotient of a map") + } +} + +func TestMapEqNonMap(t *testing.T) { + t1 := NewMap() + _, err := t1.Eq(Int(0)) + if err == nil { + t.Error("Expected error when comparing map to int") + } +} + +func TestMapNotEqErr(t *testing.T) { + t1 := &Map{[]Type{Int(1), Int(2)}, []Type{String("test")}, "type", "keys", "values"} + _, err := t1.NotEq(Int(0)) + if err == nil { + t.Error("Expected error when comparing map to int") + } +} + +func TestMapAndErr(t *testing.T) { + t1 := NewMap() + _, err := t1.And(Int(0)) + if err == nil { + t.Error("Expected error when comparing map") + } +} + +func TestMapOrErr(t *testing.T) { + t1 := NewMap() + _, err := t1.Or(Int(0)) + if err == nil { + t.Error("Expected error when comparing map") + } +} + +func TestMapXorErr(t *testing.T) { + t1 := NewMap() + _, err := t1.Xor(Int(0)) + if err == nil { + t.Error("Expected error when comparing map") + } +} + +func TestMapGtErr(t *testing.T) { + t1 := NewMap() + _, err := t1.Gt(Int(0)) + if err == nil { + t.Error("Expected error when comparing map") + } +} + +func TestMapGtEqErr(t *testing.T) { + t1 := NewMap() + _, err := t1.GtEq(Int(0)) + if err == nil { + t.Error("Expected error when comparing map") + } +} + +func TestMapLwErr(t *testing.T) { + t1 := NewMap() + _, err := t1.Lw(Int(0)) + if err == nil { + t.Error("Expected error when comparing map") + } +} + +func TestMapLwEqErr(t *testing.T) { + t1 := NewMap() + _, err := t1.LwEq(Int(0)) + if err == nil { + t.Error("Expected error when comparing map") + } +} + +func TestMapAppendErr(t *testing.T) { + t1 := NewMap() + _, err := t1.Append(Int(0)) + if err == nil { + t.Error("Expected error when comparing map") + } +} + +func TestMapNotErr(t *testing.T) { + t1 := NewMap() + _, err := t1.Not() + if err == nil { + t.Error("Expected error when getting \"not\" of map") + } +} + +func TestMapSubErr(t *testing.T) { + t1 := &Map{[]Type{Int(0), Int(1)}, []Type{String("0"), String("1")}, "map[int]string", parser.Int, parser.String} + _, err := t1.Sub(Int(0)) + if err == nil { + t.Error("Expected error when subtracting an int from a map") + } +} diff --git a/interpreter/eclaType/null_test.go b/interpreter/eclaType/null_test.go new file mode 100644 index 0000000..eef1e14 --- /dev/null +++ b/interpreter/eclaType/null_test.go @@ -0,0 +1,347 @@ +package eclaType + +import ( + "github.com/Eclalang/Ecla/interpreter/utils" + "github.com/Eclalang/Ecla/parser" + "testing" +) + +// Null interacts with Null + +func TestNewNull(t *testing.T) { + t1 := NewNull() + t2 := Null{""} + + if t1 != t2 { + t.Error("error when creating null") + } +} + +func TestNewNullType(t *testing.T) { + t1 := NewNullType(parser.Int) + t2 := Null{parser.Int} + + if t1 != t2 { + t.Error("error when creating null type") + } +} + +func TestEmptyNullGetType(t *testing.T) { + t1 := NewNull() + t2 := "null" + + result := t1.GetType() + + if result != t2 { + t.Error("Expected ", t2, ", got ", result) + } +} + +func TestNullString(t *testing.T) { + t1 := NewNull() + t2 := "null" + + result := t1.String() + + if result != t2 { + t.Error("expected \"null\", got " + result) + } +} + +func TestNullGetString(t *testing.T) { + t1 := NewNull() + t2 := String("null") + + result := t1.GetString() + + if result != t2 { + t.Error("expected \"null\", got " + result) + } +} + +func TestNullGetType(t *testing.T) { + t1 := NewNullType(parser.Int) + t2 := Int(0) + + result := t1.GetType() + + if result != t2.GetType() { + t.Error("expected \"int\", got " + result) + } +} + +func TestNullEq(t *testing.T) { + t1 := Null{parser.Int} + t2 := Null{parser.String} + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + + if result != Bool(true) { + t.Error("expected true, got" + result.String()) + } +} + +func TestNullEqFalse(t *testing.T) { + t1 := Null{parser.Int} + t2 := Int(0) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + + if result != Bool(false) { + t.Error("expected false, got" + result.String()) + } +} + +func TestNullNotEq(t *testing.T) { + t1 := Null{parser.Int} + t2 := Int(0) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + + if result != Bool(true) { + t.Error("expected true, got" + result.String()) + } +} + +func TestNullNotEqFalse(t *testing.T) { + t1 := Null{parser.Int} + t2 := Null{parser.String} + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + + if result != Bool(false) { + t.Error("expected false, got" + result.String()) + } +} + +func TestNullIsNull(t *testing.T) { + t1 := Null{""} + + result := t1.IsNull() + if result != true { + t.Error("expected \"true\", got false") + } +} + +func TestNullGetSize(t *testing.T) { + t1 := Null{""} + + result := t1.GetSize() + if result != utils.Sizeof(t1) { + t.Errorf("expected %d, got %d", utils.Sizeof(t1), result) + } +} + +// Null test errors + +func TestNullGetValue(t *testing.T) { + t1 := NewNull() + result := t1.GetValue() + + if t1 != result { + t.Error("Expected ", t1, ", got ", result) + } +} + +func TestNullSetValue(t *testing.T) { + t1 := NewNull() + err := t1.SetValue(0) + + if err == nil { + t.Error("expected error when setting value of null") + } +} + +func TestNullGetIndex(t *testing.T) { + t1 := NewNull() + _, err := t1.GetIndex(Int(0)) + + if err == nil { + t.Error("expected error when getting index of null") + } +} + +func TestNullAdd(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Add(t2) + if err == nil { + t.Error("expected error when adding null with non-string") + } +} + +func TestNullSub(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Sub(t2) + if err == nil { + t.Error("expected error when subtracting from null") + } +} + +func TestNullMul(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Mul(t2) + if err == nil { + t.Error("expected error when multiplying with null") + } +} + +func TestNullDiv(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Div(t2) + if err == nil { + t.Error("expected error when dividing null") + } +} + +func TestNullMod(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Mod(t2) + if err == nil { + t.Error("expected error when getting remainder of null") + } +} + +func TestNullDivEc(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.DivEc(t2) + if err == nil { + t.Error("expected error when getting quotient of null") + } +} + +func TestNullAnd(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.And(t2) + if err == nil { + t.Error("expected error when comparing to null") + } +} + +func TestNullOr(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Or(t2) + if err == nil { + t.Error("expected error when comparing to null") + } +} +func TestNullXor(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Xor(t2) + if err == nil { + t.Error("expected error when comparing to null") + } +} + +func TestNullNot(t *testing.T) { + t1 := Null{""} + + _, err := t1.Not() + if err == nil { + t.Error("expected error when getting \"not\" of null") + } +} + +func TestNullGt(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Gt(t2) + if err == nil { + t.Error("expected error when comparing to null") + } +} + +func TestNullGtEq(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.GtEq(t2) + if err == nil { + t.Error("expected error when comparing to null") + } +} + +func TestNullLw(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Lw(t2) + if err == nil { + t.Error("expected error when comparing to null") + } +} + +func TestNullLwEq(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.LwEq(t2) + if err == nil { + t.Error("expected error when comparing to null") + } +} + +func TestNullAppend(t *testing.T) { + t1 := Null{""} + t2 := Int(0) + + _, err := t1.Append(t2) + if err == nil { + t.Error("expected error when appending to null") + } +} + +func TestNullGetLength(t *testing.T) { + t1 := Null{""} + + _, err := t1.Len() + if err == nil { + t.Error("expected error when getting length of null") + } +} + +// Null interacts with String + +func TestNullStringAdd(t *testing.T) { + t1 := Null{""} + t2 := String("test") + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + + if result != String("nulltest") { + t.Error("expected \"nulltest\", got " + result.String()) + } +} diff --git a/interpreter/eclaType/string.go b/interpreter/eclaType/string.go index f4e3370..bd7a659 100644 --- a/interpreter/eclaType/string.go +++ b/interpreter/eclaType/string.go @@ -198,6 +198,7 @@ func (s String) GtEq(other Type) (Type, error) { } // Lw returns true if s is lower than other + func (s String) Lw(other Type) (Type, error) { switch other.(type) { case *Var: diff --git a/interpreter/eclaType/string_test.go b/interpreter/eclaType/string_test.go index 930cf34..db04638 100644 --- a/interpreter/eclaType/string_test.go +++ b/interpreter/eclaType/string_test.go @@ -2,6 +2,8 @@ package eclaType import ( "fmt" + "github.com/Eclalang/Ecla/interpreter/utils" + "github.com/Eclalang/Ecla/parser" "testing" ) @@ -48,6 +50,19 @@ func TestMulStringInt(t *testing.T) { } } +func TestGetIndexStringInt(t *testing.T) { + t1 := String("123") + t2 := Int(0) + + result, err := t1.GetIndex(t2) + if err != nil { + t.Error(err) + } + if (*result).GetValue() != Char('1') { + t.Error("Expected \"1\", got ", result) + } +} + // String interacts with Float func TestAddStringFloat(t *testing.T) { @@ -67,8 +82,562 @@ func TestAddStringFloat(t *testing.T) { } } +// String interacts with Any + +func TestAddStringAny(t *testing.T) { + t1 := String("Hello") + t2 := NewAny(Int(1)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != String("Hello1") { + t.Error("Expected Hello1, got ", result) + } +} + +func TestMulStringAny(t *testing.T) { + t1 := String("Hello") + t2 := NewAny(Int(2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != String("HelloHello") { + t.Error("Expected HelloHello, got ", result) + } +} + +func TestEqStringAny(t *testing.T) { + t1 := String("Hello") + t2 := NewAny(String("Hello")) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqStringAnyFalse(t *testing.T) { + t1 := String("Hello") + t2 := NewAny(String(" world")) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqStringAny(t *testing.T) { + t1 := String("Hello") + t2 := NewAny(String(" world")) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqStringAnyFalse(t *testing.T) { + t1 := String("Hello") + t2 := NewAny(String("Hello")) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtStringAny(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("12")) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtStringAnyFalse(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("1234")) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtStringAnyEq(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("123")) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqStringAny(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("12")) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqStringAnyFalse(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("1234")) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqStringAnyEq(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("123")) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwStringAny(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("1234")) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwStringAnyFalse(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("12")) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwStringAnyEq(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("123")) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqStringAny(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("1234")) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqStringAnyFalse(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("12")) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqStringAnyEq(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("123")) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAppendStringAny(t *testing.T) { + t1 := String("123") + t2 := NewAny(String("4")) + + result, err := t1.Append(t2) + if err != nil { + t.Error(err) + } + if result != String("1234") { + t.Error("Expected \"1234\", got ", result) + } +} + +func TestGetIndexStringAny(t *testing.T) { + t1 := String("123") + t2 := NewAny(Int(0)) + + result, err := t1.GetIndex(t2) + if err != nil { + t.Error(err) + } + if (*result).GetValue() != Char('1') { + t.Error("Expected \"1\", got ", result) + } +} + +// String interacts with Var + +func TestAddStringVar(t *testing.T) { + t1 := String("Hello") + t2, _ := NewVar("test", parser.Int, Int(1)) + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != String("Hello1") { + t.Error("Expected Hello1, got ", result) + } +} + +func TestMulStringVar(t *testing.T) { + t1 := String("Hello") + t2, _ := NewVar("test", parser.Int, Int(2)) + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != String("HelloHello") { + t.Error("Expected HelloHello, got ", result) + } +} + +func TestEqStringVar(t *testing.T) { + t1 := String("Hello") + t2, _ := NewVar("test", parser.String, String("Hello")) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestEqStringVarFalse(t *testing.T) { + t1 := String("Hello") + t2, _ := NewVar("test", parser.String, String(" world")) + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqStringVar(t *testing.T) { + t1 := String("Hello") + t2, _ := NewVar("test", parser.String, String(" world")) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotEqStringVarFalse(t *testing.T) { + t1 := String("Hello") + t2, _ := NewVar("test", parser.String, String("Hello")) + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtStringVar(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("12")) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtStringVarFalse(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("1234")) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtStringVarEq(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("123")) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqStringVar(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("12")) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtEqStringVarFalse(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("1234")) + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqStringVarEq(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("123")) + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwStringVar(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("1234")) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwStringVarFalse(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("12")) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwStringVarEq(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("123")) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqStringVar(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("1234")) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqStringVarFalse(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("12")) + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwEqStringVarEq(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.String, String("123")) + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGetIndexStringVar(t *testing.T) { + t1 := String("123") + t2, _ := NewVar("test", parser.Int, Int(0)) + + result, err := t1.GetIndex(t2) + if err != nil { + t.Error(err) + } + if (*result).GetValue() != Char('1') { + t.Error("Expected \"1\", got ", result) + } +} + // String interacts with String +func TestStringGetSize(t *testing.T) { + t1 := String("test") + expected := utils.Sizeof(t1) + + result := t1.GetSize() + if result != expected { + t.Errorf("expected %d, got %d", expected, result) + } +} + +func TestStringLen(t *testing.T) { + t1 := String("test") + expected := 4 + + result, err := t1.Len() + if err != nil { + t.Error(err) + } + if result != expected { + t.Errorf("expected %d, got %d", expected, result) + } +} + +func TestIsNullString(t *testing.T) { + t1 := String("test") + result := t1.IsNull() + + if result == true { + t.Error("expected false, got ", result) + } +} + +func TestStringString(t *testing.T) { + t1 := String("test") + t2 := "test" + + result := t1.String() + + if result != t2 { + t.Errorf("expected %s, got %s", t2, result) + } +} + func TestAddStrings(t *testing.T) { t1 := String("hello") t2 := String(" world") @@ -82,29 +651,29 @@ func TestAddStrings(t *testing.T) { } } -func TestMulStrings(t *testing.T) { +func TestEqStrings(t *testing.T) { t1 := String("hello") - t2 := Int(3) + t2 := String("hello") - result, err := t1.Mul(t2) + result, err := t1.Eq(t2) if err != nil { t.Error(err) } - if result.GetValue() != String("hellohellohello") { + if result.GetValue() != Bool(true) { t.Error("Expected true, got ", result) } } -func TestEqStrings(t *testing.T) { +func TestEqStringsFalse(t *testing.T) { t1 := String("hello") - t2 := String("hello") + t2 := String(" world") result, err := t1.Eq(t2) if err != nil { t.Error(err) } - if result.GetValue() != Bool(true) { - t.Error("Expected true, got ", result) + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) } } @@ -213,3 +782,222 @@ func TestMulStringChar(t *testing.T) { t.Error("Expected HelloHello, got ", result) } } + +func TestAppendStringChar(t *testing.T) { + t1 := String("123") + t2 := Char('4') + + result, err := t1.Append(t2) + if err != nil { + t.Error(err) + } + if result != String("1234") { + t.Error("Expected \"1234\", got ", result) + } +} + +// test String errors + +func TestStringSetValue(t *testing.T) { + t1, _ := NewString("test") + result := t1.SetValue("err") + + if result == nil { + t.Error("expected error when setting value of string") + } +} + +func TestStringNewStringEscapeErr(t *testing.T) { + _, err := NewString("\n") + + if err == nil { + t.Error("expected error when creating string with only escape char") + } +} + +func TestStringNewStringErr(t *testing.T) { + _, err := NewString("'\"\"'") + + if err == nil { + t.Error("expected error when creating invalid strings") + } +} + +func TestGetIndexStringOutOfRangeErr(t *testing.T) { + t1 := String("123") + t2 := Int(3) + + _, err := t1.GetIndex(t2) + if err == nil { + t.Error("Expected error when indexing out of range") + } +} + +func TestGetIndexStringTypeErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.GetIndex(t2) + if err == nil { + t.Error("Expected error when indexing string with bool") + } +} + +func TestSubStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Sub(t2) + if err == nil { + t.Error("Expected error when subtracting from string") + } +} + +func TestModStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Mod(t2) + if err == nil { + t.Error("Expected error when getting remainder of string") + } +} + +func TestMulStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Mul(t2) + if err == nil { + t.Error("Expected error when multiplying string with bool") + } +} + +func TestDivStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Div(t2) + if err == nil { + t.Error("Expected error when dividing string") + } +} + +func TestDivEcStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.DivEc(t2) + if err == nil { + t.Error("Expected error when dividing string") + } +} + +func TestEqStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Eq(t2) + if err == nil { + t.Error("Expected error when comparing string and bool") + } +} + +func TestNotEqStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.NotEq(t2) + if err == nil { + t.Error("Expected error when comparing string and bool") + } +} + +func TestGtStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Gt(t2) + if err == nil { + t.Error("Expected error when comparing string and bool") + } +} + +func TestGtEqStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.GtEq(t2) + if err == nil { + t.Error("Expected error when comparing string and bool") + } +} + +func TestLwStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Lw(t2) + if err == nil { + t.Error("Expected error when comparing string and bool") + } +} + +func TestLwEqStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.LwEq(t2) + if err == nil { + t.Error("Expected error when comparing string and bool") + } +} + +func TestAndStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.And(t2) + if err == nil { + t.Error("Expected error when comparing string and bool") + } +} + +func TestOrStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Or(t2) + if err == nil { + t.Error("Expected error when comparing string and bool") + } +} + +func TestXorStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Xor(t2) + if err == nil { + t.Error("Expected error when comparing string and bool") + } +} + +func TestNotStringErr(t *testing.T) { + t1 := String("123") + + _, err := t1.Not() + if err == nil { + t.Error("Expected error when getting \"not\" of string") + } +} + +func TestAppendStringErr(t *testing.T) { + t1 := String("123") + t2 := Bool(true) + + _, err := t1.Append(t2) + if err == nil { + t.Error("Expected error when appending bool to string") + } +} diff --git a/interpreter/eclaType/struct_test.go b/interpreter/eclaType/struct_test.go new file mode 100644 index 0000000..d25a8ee --- /dev/null +++ b/interpreter/eclaType/struct_test.go @@ -0,0 +1,768 @@ +package eclaType + +import ( + "github.com/Eclalang/Ecla/interpreter/eclaDecl" + "github.com/Eclalang/Ecla/interpreter/utils" + "github.com/Eclalang/Ecla/parser" + "testing" +) + +func TestNewStruct(t *testing.T) { + m := make(map[string]string) + fName := "field1" + vName := "value1" + m[fName] = vName + sName := "struct" + + decl := &eclaDecl.StructDecl{m, []string{fName}, sName} + t1 := NewStruct(decl) + if t1.Typ != sName { + t.Errorf("Expected %s, got %s", sName, t1.Typ) + return + } + if t1.Definition != decl { + t.Errorf("Expected %s, got %s", decl, t1.Definition) + return + } + if t1.Fields == nil { + t.Error("Expected map, got nil") + return + } + if len(t1.Fields) != 0 { + t.Error("The maps do not match") + return + } +} + +func TestStructAddField(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{}, []string{"field0"}, "testStruct"} + s := &Struct{map[string]*Type{}, "testType", decl} + + i := Int(42) + s.AddField(0, i) + + if len(s.Fields) == 0 { + t.Error("Field was not added") + } + expected := *(s.Fields[decl.Order[0]]) + if expected != i { + t.Errorf("Expected %d, got %d", i, expected) + } +} + +func TestStructVerify(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{}, []string{"field0"}, "testStruct"} + var v Type = Int(42) + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + err := s.Verify() + if err != nil { + t.Error(err) + } +} + +func TestStructGetValue(t *testing.T) { + s := &Struct{nil, "", nil} + result := s.GetValue() + if result != s { + t.Error("The two structs do not match") + } +} + +func TestStructGetType(t *testing.T) { + expected := "testing get type" + s := &Struct{nil, expected, nil} + result := s.GetType() + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestStructString(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + expected := "{42, test}" + result := s.String() + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestStructGetString(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + expected := String("{42, test}") + result := s.GetString() + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestStructSetType(t *testing.T) { + oldType := "old type" + s := &Struct{nil, oldType, nil} + newType := "new type" + s.SetType(newType) + if (s.Typ == oldType) || (s.Typ != newType) { + t.Errorf("Expected %s, got %s", newType, oldType) + } +} + +func TestStructGet(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + result, err := s.Get("field0") + if err != nil { + t.Error(err) + } + if result != i { + t.Errorf("Expected %d, got %s", i, result) + } +} + +func TestStructGetIndex(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + result, err := s.GetIndex(String("field0")) + if err != nil { + t.Error(err) + } + if *result != i { + t.Errorf("Expected %d, got %s", i, *result) + } +} + +func TestStructIsNull(t *testing.T) { + s := &Struct{nil, "", nil} + result := s.IsNull() + if result { + t.Error("Expected false, got true") + } +} + +func TestStructGetField(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + result := s.GetField("field0") + if *result != i { + t.Errorf("Expected %d, got %s", i, *result) + } +} + +func TestStructGetFieldFalse(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{}, ""} + s := &Struct{map[string]*Type{}, "", decl} + result := s.GetField("field0") + if result != nil { + t.Errorf("Expected nil, got %s", *result) + } +} + +func TestStructLen(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + result, err := s.Len() + if err != nil { + t.Error(err) + } + if result != 2 { + t.Errorf("Expected %d, got %d", 2, result) + } +} + +func TestStructGetSize(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + expected := utils.Sizeof(s) + result := s.GetSize() + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + } +} + +func TestIsStruct(t *testing.T) { + str := "struct test" + result := IsStruct(str) + if !result { + t.Errorf("%s should be a valid struct type", str) + } +} + +func TestIsStructFalseLen(t *testing.T) { + str := "s" + result := IsStruct(str) + if result { + t.Errorf("%s should not be a valid struct type", str) + } +} + +func TestIsStructFalse(t *testing.T) { + str := "not a valid type" + result := IsStruct(str) + if result { + t.Errorf("%s should not be a valid struct type", str) + } +} + +func TestSetValueStruct(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + expected := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + s := &Struct{nil, "", nil} + err := s.SetValue(expected) + if err != nil { + t.Error(err) + return + } + + if s.Typ != expected.Typ || s.Definition != expected.Definition || len(s.Fields) != len(expected.Fields) { + t.Errorf("Expected %s, got %s", expected, s) + return + } + for k, arg := range expected.Fields { + if s.Fields[k] != arg { + t.Error("The fields are not the same") + return + } + } +} + +func TestSetValueStructVar(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + v := &Var{"", &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl}} + s := &Struct{nil, "", nil} + err := s.SetValue(v) + if err != nil { + t.Error(err) + return + } + + expected := v.Value.(*Struct) + if s.Typ != expected.Typ || s.Definition != expected.Definition || len(s.Fields) != len(expected.Fields) { + t.Errorf("Expected %s, got %s", expected, s) + return + } + for k, arg := range expected.Fields { + if s.Fields[k] != arg { + t.Error("The fields are not the same") + return + } + } +} + +func TestSetValueStructAny(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + a := &Any{&Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl}, ""} + s := &Struct{nil, "", nil} + err := s.SetValue(a) + if err != nil { + t.Error(err) + return + } + + expected := a.Value.(*Struct) + if s.Typ != expected.Typ || s.Definition != expected.Definition || len(s.Fields) != len(expected.Fields) { + t.Errorf("Expected %s, got %s", expected, s) + return + } + for k, arg := range expected.Fields { + if s.Fields[k] != arg { + t.Error("The fields are not the same") + return + } + } +} + +func TestSetStruct(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0"}, ""} + var i Type = Int(0) + expected := Int(42) + fieldName := "field0" + s := &Struct{map[string]*Type{fieldName: &i}, "", decl} + err := s.Set(fieldName, expected) + if err != nil { + t.Error(err) + } + result := *s.Fields[fieldName] + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + } +} + +func TestSetStructVar(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0"}, ""} + var i Type = Int(0) + expected := Int(42) + fieldName := "field0" + s := &Struct{map[string]*Type{fieldName: &i}, "", decl} + err := s.Set(fieldName, &Var{"", expected}) + if err != nil { + t.Error(err) + } + result := *s.Fields[fieldName] + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + } +} + +func TestSetStructAnyArg(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0"}, ""} + var i Type = Int(0) + expected := Int(42) + fieldName := "field0" + s := &Struct{map[string]*Type{fieldName: &i}, "", decl} + err := s.Set(fieldName, &Any{expected, ""}) + if err != nil { + t.Error(err) + } + result := *s.Fields[fieldName] + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + } +} + +func TestSetStructAnyField(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": parser.Any}, []string{"field0"}, ""} + var i Type = &Any{Int(0), "test"} + expected := Int(42) + fieldName := "field0" + s := &Struct{map[string]*Type{fieldName: &i}, "", decl} + err := s.Set(fieldName, expected) + if err != nil { + t.Error(err) + return + } + result := (*s.Fields[fieldName]).(*Any).Value + if result != expected { + t.Errorf("Expected %d, got %d", expected, result) + return + } +} + +func TestAddStructString(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + result, err := s.Add(String("test")) + if err != nil { + t.Error(err) + } + expected := String("{42, test}test") + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestAddStructVar(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + result, err := s.Add(&Var{"", String("test")}) + if err != nil { + t.Error(err) + } + expected := String("{42, test}test") + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestAddStructAny(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{"field0", "field1"}, ""} + var i Type = Int(42) + var str Type = String("test") + s := &Struct{map[string]*Type{"field0": &i, "field1": &str}, "", decl} + result, err := s.Add(&Any{String("test"), ""}) + if err != nil { + t.Error(err) + } + expected := String("{42, test}test") + if result != expected { + t.Errorf("Expected %s, got %s", expected, result) + } +} + +func TestStructEqFalseType(t *testing.T) { + s1 := &Struct{nil, "test", nil} + s2 := &Struct{nil, "other struct", nil} + result, err := s1.Eq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got true") + } +} + +func TestStructEqFalseFieldsLength(t *testing.T) { + var arg1 Type = Int(0) + var arg2 Type = Int(0) + s1 := &Struct{map[string]*Type{"1": &arg1}, "", nil} + s2 := &Struct{map[string]*Type{"1": &arg1, "2": &arg2}, "", nil} + result, err := s1.Eq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got true") + } +} + +func TestStructEqFalseValues(t *testing.T) { + var arg1 Type = Int(0) + s1 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + s2 := &Struct{map[string]*Type{"2": &arg1}, "test", nil} + result, err := s1.Eq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got true") + } +} + +func TestStructEqTrue(t *testing.T) { + var arg1 Type = Int(0) + s1 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + s2 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + result, err := s1.Eq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got false") + } +} + +func TestStructEqTrueVar(t *testing.T) { + var arg1 Type = Int(0) + s1 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + s2 := &Var{"", &Struct{map[string]*Type{"1": &arg1}, "test", nil}} + result, err := s1.Eq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got false") + } +} + +func TestStructEqTrueAny(t *testing.T) { + var arg1 Type = Int(0) + s1 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + s2 := &Any{&Struct{map[string]*Type{"1": &arg1}, "test", nil}, ""} + result, err := s1.Eq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got false") + } +} + +func TestStructNotEqTrueValues(t *testing.T) { + var arg1 Type = Int(0) + s1 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + s2 := &Struct{map[string]*Type{"2": &arg1}, "test", nil} + result, err := s1.NotEq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(true) { + t.Error("Expected true, got false") + } +} + +func TestStructNotEqFalse(t *testing.T) { + var arg1 Type = Int(0) + s1 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + s2 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + result, err := s1.NotEq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got true") + } +} + +func TestStructNotEqTrueVar(t *testing.T) { + var arg1 Type = Int(0) + s1 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + s2 := &Var{"", &Struct{map[string]*Type{"1": &arg1}, "test", nil}} + result, err := s1.NotEq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got true") + } +} + +func TestStructNotEqTrueAny(t *testing.T) { + var arg1 Type = Int(0) + s1 := &Struct{map[string]*Type{"1": &arg1}, "test", nil} + s2 := &Any{&Struct{map[string]*Type{"1": &arg1}, "test", nil}, ""} + result, err := s1.NotEq(s2) + if err != nil { + t.Error(err) + } + if result != Bool(false) { + t.Error("Expected false, got true") + } +} + +// Tests struct errors + +func TestStructVerifyError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{}, []string{"field0"}, "testStruct"} + s := &Struct{map[string]*Type{}, "testStruct", decl} + err := s.Verify() + if err == nil { + t.Error("Expected error when the number of fields did not match") + } +} + +func TestStructGetError(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{}, ""} + s := &Struct{map[string]*Type{}, "", decl} + _, err := s.Get("field0") + if err == nil { + t.Error("Expected error when getting non existent field") + } +} + +func TestStructGetIndexErrorType(t *testing.T) { + s := &Struct{nil, "", nil} + _, err := s.GetIndex(Int(0)) + if err == nil { + t.Error("Expected error when getting index with non string arg") + } +} + +func TestStructGetIndexGetError(t *testing.T) { + decl := &eclaDecl.StructDecl{nil, []string{}, ""} + s := &Struct{map[string]*Type{}, "", decl} + _, err := s.GetIndex(String("field0")) + if err == nil { + t.Error("Expected error when getting non existent field") + } +} + +func TestStructAddError(t *testing.T) { + s := &Struct{nil, "", nil} + _, err := s.Add(Int(0)) + if err == nil { + t.Error("Expected error when adding int to struct") + } +} + +func TestStructSubError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Sub(Int(0)) + if err == nil { + t.Error("Expected error when subtracting int from struct") + } +} + +func TestStructMulError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Mul(Int(0)) + if err == nil { + t.Error("Expected error when multiplying int with struct") + } +} + +func TestStructDivError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Div(Int(0)) + if err == nil { + t.Error("Expected error when dividing struct by int") + } +} + +func TestStructDivEcError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.DivEc(Int(0)) + if err == nil { + t.Error("Expected error when getting quotient of struct") + } +} + +func TestStructModError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Mod(Int(0)) + if err == nil { + t.Error("Expected error when getting remainder of struct") + } +} + +func TestStructEqError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Eq(Int(0)) + if err == nil { + t.Error("Expected error when comparing struct to int") + } +} + +func TestStructNotEqError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.NotEq(Int(0)) + if err == nil { + t.Error("Expected error when comparing struct to int") + } +} + +func TestStructAndError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.And(Int(0)) + if err == nil { + t.Error("Expected error when comparing struct to int") + } +} + +func TestStructOrError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Or(Int(0)) + if err == nil { + t.Error("Expected error when comparing struct to int") + } +} + +func TestStructXorError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Xor(Int(0)) + if err == nil { + t.Error("Expected error when comparing struct to int") + } +} + +func TestStructGtError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Gt(Int(0)) + if err == nil { + t.Error("Expected error when comparing struct to int") + } +} + +func TestStructGtEqError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.GtEq(Int(0)) + if err == nil { + t.Error("Expected error when comparing struct to int") + } +} + +func TestStructLwError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Lw(Int(0)) + if err == nil { + t.Error("Expected error when comparing struct to int") + } +} + +func TestStructLwEqError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.LwEq(Int(0)) + if err == nil { + t.Error("Expected error when comparing struct to int") + } +} + +func TestStructNotError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Not() + if err == nil { + t.Error("Expected error when getting \"not\" of struct") + } +} + +func TestStructAppendError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + _, err := s.Append(Int(0)) + if err == nil { + t.Error("Expected error when appending to struct") + } +} + +func TestStructSetValueError(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + err := s.SetValue(Int(0)) + if err == nil { + t.Error("Expected error when setting value of struct with int") + } +} + +func TestStructSetErrorNotExist(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + err := s.Set("wrong", Int(0)) + if err == nil { + t.Error("Expected error when appending to struct") + } +} + +func TestStructSetErrorType(t *testing.T) { + decl := &eclaDecl.StructDecl{map[string]string{"field0": "string"}, []string{"field0"}, "testStruct"} + var v Type = String("test") + s := &Struct{map[string]*Type{"field0": &v}, "testStruct", decl} + err := s.Set("field0", Int(0)) + if err == nil { + t.Error("Expected error when appending to struct") + } +} diff --git a/interpreter/eclaType/var_test.go b/interpreter/eclaType/var_test.go new file mode 100644 index 0000000..f997a0f --- /dev/null +++ b/interpreter/eclaType/var_test.go @@ -0,0 +1,755 @@ +package eclaType + +import ( + "github.com/Eclalang/Ecla/parser" + "testing" +) + +func TestNewVarEmpty(t *testing.T) { + t1, err := NewVarEmpty("test", "int") + if err != nil { + t.Error(err) + } + + expected := &Var{Name: "test", Value: NewNullType("int")} + + if *t1 != *expected { + t.Error("expected ", expected, ", got ", t1) + } +} + +func TestNewVarString(t *testing.T) { + t1, err := NewVar("test", parser.String, String("value")) + if err != nil { + t.Error(err) + } + + expected := &Var{"test", String("value")} + + if *t1 != *expected { + t.Error("expected ", expected, ", got ", t1) + } +} + +func TestNewVarFloat(t *testing.T) { + t1, err := NewVar("test", parser.Float, Int(1)) + if err != nil { + t.Error(err) + } + + expected := &Var{"test", Float(1.0)} + + if *t1 != *expected { + t.Error("expected ", expected, ", got ", t1) + } +} + +func TestNewVarAny(t *testing.T) { + tmp := NewAny(Int(0)) + t1, err := NewVar("test", parser.Any, Int(0)) + if err != nil { + t.Error(err) + } + + expected := &Var{"test", tmp} + + if t1.Value == expected.Value { + t.Error("expected ", expected, ", got ", t1) + } +} + +func TestNewVarNull(t *testing.T) { + t1, err := NewVar("test", parser.Int, NewNullType(parser.Int)) + if err != nil { + t.Error(err) + } + + expected := &Var{"test", NewNullType(parser.Int)} + + if *t1 != *expected { + t.Error("expected ", expected, ", got ", t1) + } +} + +func TestNewVarEmptyType(t *testing.T) { + t1, err := NewVar("test", "", Int(0)) + if err != nil { + t.Error(err) + } + + expected := &Var{"test", Int(0)} + + if *t1 != *expected { + t.Error("expected ", expected, ", got ", t1) + } +} + +func TestNewVarErr(t *testing.T) { + _, err := NewVar("test", parser.Bool, Int(1)) + + if err == nil { + t.Error("Expected error when creating a var with the wrong type") + } +} + +func TestNewVarVar(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + + t2, e := NewVar("test", parser.Int, t1) + if e != nil { + t.Error(e) + } + + if *t1 != *t2 { + t.Error("Expected ", t1, ", got ", t2) + } +} + +func TestVarString(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + expected := "test = 0" + + result := t1.String() + + if result != expected { + t.Error("Expected ", expected, ", got ", result) + } +} + +func TestVarGetString(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + expected := String("0") + + result := t1.GetString() + + if result != expected { + t.Error("Expected ", expected, ", got ", result) + } +} + +func TestVarGetValue(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + + result := t1.GetValue() + + if result != Int(0) { + t.Error("Expected ", Int(0), ", got ", result) + } +} + +func TestVarSetValue(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + + result := t1.SetValue(Int(0)) + + switch result.(type) { + case error: + return + } + t.Error("Expected error, got ", result) +} + +func TestVarGetType(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + + result := t1.GetType() + + if result != parser.Int { + t.Error("Expected ", parser.Int, ", got ", result) + } +} + +func TestGetIndexVarInt(t *testing.T) { + t1, err := NewVar("test", parser.String, String("123")) + if err != nil { + t.Error(err) + } + t2 := Int(0) + + result, e := t1.GetIndex(t2) + if e != nil { + t.Error(e) + } + if (*result).GetValue() != Char('1') { + t.Error("Expected \"1\", got ", result) + } +} + +func TestSetVarInt(t *testing.T) { + t1, err := NewVarEmpty("test", parser.Int) + if err != nil { + t.Error(err) + } + t2 := Int(0) + + e := t1.SetVar(t2) + if e != nil { + t.Error(e) + } + if t1.Value != t2 { + t.Error("Expected ", t2, ", got ", t1.Value) + } +} + +func TestSetVarVarInt(t *testing.T) { + t1, err := NewVarEmpty("test", parser.Int) + if err != nil { + t.Error(err) + } + t2, err2 := NewVar("i", parser.Int, Int(0)) + if err2 != nil { + t.Error(err2) + } + + e := t1.SetVar(t2) + if e != nil { + t.Error(e) + } + if t1.Value != t2.Value { + t.Error("Expected ", t2, ", got ", t1.Value) + } +} + +func TestSetVarNull(t *testing.T) { + t1, err := NewVarEmpty("test", parser.Int) + if err != nil { + t.Error(err) + } + t2 := NewNullType(parser.Int) + + e := t1.SetVar(t2) + if e != nil { + t.Error(e) + } + if t1.Value != t2 { + t.Error("Expected ", t2, ", got ", t1.Value) + } +} + +func TestSetVarErr(t *testing.T) { + t1, err := NewVarEmpty("test", parser.Int) + if err != nil { + t.Error(err) + } + t2 := Bool(true) + + e := t1.SetVar(t2) + if e == nil { + t.Error("expected error when assigning boolean to int") + } +} + +func TestAddVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Add(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(3) { + t.Error("Expected 3, got ", result) + } +} + +func TestSubVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Sub(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(-1) { + t.Error("Expected -1, got ", result) + } +} + +func TestMulVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(2)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(3)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Mul(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(6) { + t.Error("Expected 6, got ", result) + } +} + +func TestDivVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Div(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Float(0.5) { + t.Error("Expected 0.5, got ", result) + } +} + +func TestModVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(23)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Mod(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(1) { + t.Error("Expected 1, got ", result) + } +} + +func TestDivEcVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(9)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.DivEc(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Int(4) { + t.Error("Expected 4, got ", result) + } +} + +func TestEqVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(9)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Eq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestNotEqVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.NotEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestGtVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Gt(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestGtEqVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.GtEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestLwVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Lw(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestLwEqVarAsInt(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Int, Int(2)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.LwEq(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestAndVar(t *testing.T) { + t1, err1 := NewVar("a", parser.Bool, Bool(true)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Bool, Bool(false)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.And(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(false) { + t.Error("Expected false, got ", result) + } +} + +func TestOrVar(t *testing.T) { + t1, err1 := NewVar("a", parser.Bool, Bool(true)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Bool, Bool(false)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Or(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestXorVar(t *testing.T) { + t1, err1 := NewVar("a", parser.Bool, Bool(true)) + if err1 != nil { + t.Error(err1) + } + t2, err2 := NewVar("b", parser.Bool, Bool(false)) + if err2 != nil { + t.Error(err2) + } + + result, err := t1.Xor(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestNotVar(t *testing.T) { + t1, err1 := NewVar("a", parser.Bool, Bool(false)) + if err1 != nil { + t.Error(err1) + } + + result, err := t1.Not() + if err != nil { + t.Error(err) + } + if result.GetValue() != Bool(true) { + t.Error("Expected true, got ", result) + } +} + +func TestIncrementVar(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + + t1.Increment() + if t1.GetValue() != Int(2) { + t.Error("Expected 2, got ", t1) + } +} + +func TestDecrementVar(t *testing.T) { + t1, err1 := NewVar("a", parser.Int, Int(1)) + if err1 != nil { + t.Error(err1) + } + + t1.Decrement() + if t1.GetValue() != Int(0) { + t.Error("Expected 0, got ", t1) + } +} + +func TestAppendVar(t *testing.T) { + t1, err1 := NewVar("test", parser.String, String("123")) + if err1 != nil { + t.Error(err1) + } + t2 := String("4") + + result, err := t1.Append(t2) + if err != nil { + t.Error(err) + } + if result.GetValue() != String("1234") { + t.Error("Expected \"1234\", got ", result) + } +} + +func TestIsNullVar(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + + result := t1.IsNull() + if result { + t.Error("Expected false, got ", result) + } +} + +func TestIsFunctionVarFalse(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + + result := t1.IsFunction() + if result { + t.Error("Expected false, got ", result) + } +} + +func TestIsFunctionVarFunction(t *testing.T) { + foo := NewFunction("foo", []parser.FunctionParams{}, []parser.Node{}, []string{"int"}) + t1, err := NewVar("test", foo.GetType(), foo) + if err != nil { + t.Error(err) + } + + result := t1.IsFunction() + if !result { + t.Error("Expected true, got ", result) + } +} + +func TestIsFunctionVarAnyFunction(t *testing.T) { + foo := NewFunction("foo", []parser.FunctionParams{}, []parser.Node{}, []string{"int"}) + a := NewAny(foo) + t1, err := NewVar("test", a.GetType(), a) + if err != nil { + t.Error(err) + } + + result := t1.IsFunction() + if !result { + t.Error("Expected true, got ", result) + } +} + +func TestIsAnyTrue(t *testing.T) { + a := NewAny(Int(0)) + t1, err := NewVar("test", a.GetType(), a) + if err != nil { + t.Error(err) + } + + result := t1.IsAny() + if !result { + t.Error("expected true, got ", result) + } +} + +func TestIsAnyFalse(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + + result := t1.IsAny() + if result { + t.Error("expected false, got ", result) + } +} + +func TestGetFunctionVar(t *testing.T) { + expected := NewFunction("test", []parser.FunctionParams{}, []parser.Node{}, []string{"int"}) + t1, err := NewVar("foo", expected.GetType(), expected) + if err != nil { + t.Error(err) + } + + result := t1.GetFunction() + if result != expected { + t.Error("Expected ", expected, ", got ", result) + } +} + +func TestGetFunctionVarAny(t *testing.T) { + expected := NewFunction("foo", []parser.FunctionParams{}, []parser.Node{}, []string{"int"}) + a := NewAny(expected) + t1, err := NewVar("test", a.GetType(), a) + if err != nil { + t.Error(err) + } + + result := t1.GetFunction() + if result != expected { + t.Error("Expected ", expected, ", got ", result) + } +} + +//TODO @Sanegv investigate if you put a safety to prevent any of any +//func TestGetFunctionVarAnyAny(t *testing.T) { +// expected := NewFunction("foo", []parser.FunctionParams{}, []parser.Node{}, []string{"int"}) +// a := NewAny(expected) +// a2 := NewAny(a) +// t1, err := NewVar("test", a2.GetType(), a2) +// if err != nil { +// t.Error(err) +// } +// +// result := t1.GetFunction() +// if result != expected { +// t.Error("Expected ", expected, ", got ", result) +// } +//} + +func TestGetFunctionVarNil(t *testing.T) { + t1, _ := NewVarEmpty("test", parser.Int) + + result := t1.GetFunction() + if result != nil { + t.Error("Expected \"nil\", got ", result) + } +} + +func TestGetSizeVar(t *testing.T) { + t1, err := NewVar("test", parser.Int, Int(0)) + if err != nil { + t.Error(err) + } + + expected := Int(0).GetSize() + result := t1.GetSize() + if result != expected { + t.Error("Expected ", expected, ", got ", result) + } +} + +func TestVarLen(t *testing.T) { + t1, err := NewVar("test", parser.String, String("1234")) + if err != nil { + t.Error(err) + } + expected := 4 + + result, err := t1.Len() + if err != nil { + t.Error(err) + } + if result != expected { + t.Errorf("expected %d, got %d", expected, result) + } +}