tests: add an operator overloading test

pull/4345/head
Swastik Baranwal 2020-04-11 17:09:51 +05:30 committed by GitHub
parent c408223bb5
commit 6ecb6cfe8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,75 @@
struct Vec {
x int
y int
}
pub fn (a Vec) str() string {
return '{$a.x, $a.y}'
}
fn (a Vec) + (b Vec) Vec {
return Vec {
a.x + b.x,
a.y + b.y
}
}
fn (a Vec) - (b Vec) Vec {
return Vec {
a.x - b.x,
a.y - b.y
}
}
fn (a Vec) * (b Vec) Vec {
return Vec {
a.x * b.x,
a.y * b.y
}
}
fn (a Vec) / (b Vec) Vec {
return Vec {
a.x / b.x,
a.y / b.y
}
}
fn (a Vec) % (b Vec) Vec {
return Vec {
a.x % b.x,
a.y % b.y
}
}
fn test_operator_overloading_with_string_interpolation() {
a := Vec{2, 3}
b := Vec{4, 5}
c := a + b
assert a.x + b.x == c.x
assert a.y + b.y == c.y
d := a - b
assert a.x - b.x == d.x
assert a.y - b.y == d.y
e := a * b
assert a.x * b.x == e.x
assert a.y * b.y == e.y
f := a / b
assert a.x / b.x == f.x
assert a.y / b.y == f.y
g := a % b
assert a.x % b.x == g.x
assert a.y % b.y == g.y
assert c.str() == '{6, 8}'
assert d.str() == '{-2, -2}'
assert e.str() == '{8, 15}'
assert f.str() == '{0, 0}'
assert g.str() == '{2, 3}'
}