harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 63d84a6ad670c83a4e8eaf145bfde82061f3a6bf
parent 67b27b5da56cb0a30d366c375c0c0fab6d717f0b
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Tue, 19 Jan 2021 11:21:11 -0500

Add enum tests

Diffstat:
Atests/15-enums.ha | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/configure | 3++-
2 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/tests/15-enums.ha b/tests/15-enums.ha @@ -0,0 +1,53 @@ +type implicit_values = enum { + ZERO, + ONE, + TWO, +}; + +fn implicit() void = { + // TODO: Type promotion + assert(implicit_values::ZERO == 0: implicit_values); + assert(implicit_values::ONE == 1: implicit_values); + assert(implicit_values::TWO == 2: implicit_values); +}; + +type explicit_values = enum { + NEGONE = -1, + SIXTEEN = 16, + SEVENTEEN, + EIGHTEEN, + FIFTY = 50, +}; + +fn explicit() void = { + // TODO: Type promotion + assert(explicit_values::NEGONE == -1: explicit_values); + assert(explicit_values::SIXTEEN == 16: explicit_values); + assert(explicit_values::SEVENTEEN == 17: explicit_values); + assert(explicit_values::EIGHTEEN == 18: explicit_values); + assert(explicit_values::FIFTY == 50: explicit_values); +}; + +type with_storage = enum u16 { + CAFE = 0xCAFEu16, + BABE = 0xBABEu16, + DEAD = 0xDEADu16, + BEEF = 0xBEEFu16, +}; + +fn storage() void = { + assert(size(explicit_values) == size(int)); + assert(size(with_storage) == 2z); + const val = 0xBEEFu16; + const is_little = (&val: *[2]u8)[0] == 0xEFu8; + assert(with_storage::CAFE: u8 == (if (is_little) 0xFEu8 else 0xCAu8)); + assert(with_storage::BABE: u8 == (if (is_little) 0xBEu8 else 0xBAu8)); + assert(with_storage::DEAD: u8 == (if (is_little) 0xADu8 else 0xDEu8)); + assert(with_storage::BEEF: u8 == (if (is_little) 0xEFu8 else 0xBEu8)); +}; + +export fn main() void = { + implicit(); + explicit(); + storage(); +}; diff --git a/tests/configure b/tests/configure @@ -17,7 +17,8 @@ tests() { 11-globals \ 12-loops \ 13-tagged \ - 14-switch + 14-switch \ + 15-enums do cat <<EOF tests/$t: libhart.a tests/$t.ha