2020-09-25 12:02:32 +02:00
|
|
|
type MyInt = int
|
2020-08-11 01:05:34 +02:00
|
|
|
|
2019-11-05 23:02:50 +01:00
|
|
|
fn test_shift_operators() {
|
2020-04-12 01:41:26 +02:00
|
|
|
// check that shift works with all integer types
|
2019-11-05 23:02:50 +01:00
|
|
|
// as the right-hand side operand
|
|
|
|
a := 1
|
|
|
|
b := 1024
|
|
|
|
i := 10
|
|
|
|
assert b == a << i8(i)
|
|
|
|
assert b == a << byte(i)
|
|
|
|
assert b == a << i16(i)
|
|
|
|
assert b == a << u16(i)
|
|
|
|
assert b == a << int(i)
|
|
|
|
assert b == a << u32(i)
|
|
|
|
assert b == a << i64(i)
|
|
|
|
assert b == a << u64(i)
|
|
|
|
assert a == b >> i8(i)
|
|
|
|
assert a == b >> byte(i)
|
|
|
|
assert a == b >> i16(i)
|
|
|
|
assert a == b >> u16(i)
|
|
|
|
assert a == b >> int(i)
|
|
|
|
assert a == b >> u32(i)
|
|
|
|
assert a == b >> i64(i)
|
|
|
|
assert a == b >> u64(i)
|
|
|
|
// check that shift operation result type is
|
|
|
|
// the same as the type of the left-hand side operand
|
|
|
|
mut c := u64(0)
|
|
|
|
d := u64(1)
|
|
|
|
c = d << i8(63)
|
|
|
|
assert c == 9223372036854775808
|
2019-11-06 01:37:32 +01:00
|
|
|
// check that shift-assign works with all types
|
|
|
|
// of integers on the right-hand side
|
|
|
|
mut e := 1
|
|
|
|
e <<= i8(i)
|
|
|
|
assert e == b
|
|
|
|
e >>= i8(i)
|
|
|
|
assert e == a
|
|
|
|
e <<= i16(i)
|
|
|
|
assert e == b
|
|
|
|
e >>= i16(i)
|
|
|
|
assert e == a
|
|
|
|
e <<= int(i)
|
|
|
|
assert e == b
|
|
|
|
e >>= int(i)
|
|
|
|
assert e == a
|
2020-05-24 21:07:32 +02:00
|
|
|
mut e2 := i64(1)
|
|
|
|
e2 <<= i64(i)
|
|
|
|
assert e2 == b
|
|
|
|
e2 >>= i64(i)
|
|
|
|
assert e2 == a
|
2019-11-06 01:37:32 +01:00
|
|
|
e <<= byte(i)
|
|
|
|
assert e == b
|
|
|
|
e >>= byte(i)
|
|
|
|
assert e == a
|
|
|
|
e <<= u16(i)
|
|
|
|
assert e == b
|
|
|
|
e >>= u16(i)
|
|
|
|
assert e == a
|
2020-05-24 21:07:32 +02:00
|
|
|
mut e3 := u64(1)
|
|
|
|
e3 <<= u32(i)
|
|
|
|
assert e3 == b
|
|
|
|
e3 >>= u32(i)
|
2019-11-06 01:37:32 +01:00
|
|
|
assert e == a
|
2020-05-24 21:07:32 +02:00
|
|
|
e3 <<= u64(i)
|
|
|
|
assert e3 == b
|
|
|
|
e3 >>= u64(i)
|
|
|
|
assert e3 == a
|
2020-08-11 01:05:34 +02:00
|
|
|
// Test shifts with custom int types
|
|
|
|
x := MyInt(2)
|
|
|
|
assert x << 2 == 8
|
2019-11-10 01:08:53 +01:00
|
|
|
}
|