From cabe1055c4dede77f5c82268ca6e6661fae1d62f Mon Sep 17 00:00:00 2001 From: Alexis Beingessner Date: Wed, 25 Apr 2018 02:08:50 -0400 Subject: [PATCH] Add tests for optional enum convenience constructors --- tests/expectations/annotation.c | 50 +++++++++++- tests/expectations/annotation.cpp | 118 ++++++++++++++++++++++++++- tests/expectations/both/annotation.c | 50 +++++++++++- tests/expectations/tag/annotation.c | 50 +++++++++++- tests/rust/annotation.rs | 5 +- 5 files changed, 268 insertions(+), 5 deletions(-) diff --git a/tests/expectations/annotation.c b/tests/expectations/annotation.c index fe97544..ca66f5a 100644 --- a/tests/expectations/annotation.c +++ b/tests/expectations/annotation.c @@ -17,4 +17,52 @@ typedef struct { float y; } B; -void root(A x, B y, C z); +enum F_Tag { + Foo, + Bar, + Baz, +}; +typedef uint8_t F_Tag; + +typedef struct { + F_Tag tag; + int16_t _0; +} Foo_Body; + +typedef struct { + F_Tag tag; + uint8_t x; + int16_t y; +} Bar_Body; + +typedef union { + F_Tag tag; + Foo_Body foo; + Bar_Body bar; +} F; + +enum H_Tag { + Hello, + There, + Everyone, +}; +typedef uint8_t H_Tag; + +typedef struct { + int16_t _0; +} Hello_Body; + +typedef struct { + uint8_t x; + int16_t y; +} There_Body; + +typedef struct { + H_Tag tag; + union { + Hello_Body hello; + There_Body there; + }; +} H; + +void root(A x, B y, C z, F f, H h); diff --git a/tests/expectations/annotation.cpp b/tests/expectations/annotation.cpp index 53dc7d0..35cf843 100644 --- a/tests/expectations/annotation.cpp +++ b/tests/expectations/annotation.cpp @@ -26,8 +26,124 @@ struct B { float y; }; +union F { + enum class Tag : uint8_t { + Foo, + Bar, + Baz, + }; + + struct Foo_Body { + Tag tag; + int16_t _0; + }; + + struct Bar_Body { + Tag tag; + uint8_t x; + int16_t y; + }; + + struct { + Tag tag; + }; + Foo_Body foo; + Bar_Body bar; + + static F Foo(int16_t const& a0) { + F result; + result.foo._0 = a0; + result.tag = Tag::Foo; + return result; + } + + static F Bar(uint8_t const& aX, + int16_t const& aY) { + F result; + result.bar.x = aX; + result.bar.y = aY; + result.tag = Tag::Bar; + return result; + } + + static F Baz() { + F result; + result.tag = Tag::Baz; + return result; + } + + bool IsFoo() const { + return tag == Tag::Foo; + } + + bool IsBar() const { + return tag == Tag::Bar; + } + + bool IsBaz() const { + return tag == Tag::Baz; + } +}; + +struct H { + enum class Tag : uint8_t { + Hello, + There, + Everyone, + }; + + struct Hello_Body { + int16_t _0; + }; + + struct There_Body { + uint8_t x; + int16_t y; + }; + + Tag tag; + union { + Hello_Body hello; + There_Body there; + }; + + static H Hello(int16_t const& a0) { + H result; + result.hello._0 = a0; + result.tag = Tag::Hello; + return result; + } + + static H There(uint8_t const& aX, + int16_t const& aY) { + H result; + result.there.x = aX; + result.there.y = aY; + result.tag = Tag::There; + return result; + } + + static H Everyone() { + H result; + result.tag = Tag::Everyone; + return result; + } + + bool IsHello() const { + return tag == Tag::Hello; + } + + bool IsThere() const { + return tag == Tag::There; + } + + bool IsEveryone() const { + return tag == Tag::Everyone; + } +}; + extern "C" { -void root(A x, B y, C z); +void root(A x, B y, C z, F f, H h); } // extern "C" diff --git a/tests/expectations/both/annotation.c b/tests/expectations/both/annotation.c index 384c827..fa87efe 100644 --- a/tests/expectations/both/annotation.c +++ b/tests/expectations/both/annotation.c @@ -17,4 +17,52 @@ typedef struct B { float y; } B; -void root(A x, B y, C z); +enum F_Tag { + Foo, + Bar, + Baz, +}; +typedef uint8_t F_Tag; + +typedef struct Foo_Body { + F_Tag tag; + int16_t _0; +} Foo_Body; + +typedef struct Bar_Body { + F_Tag tag; + uint8_t x; + int16_t y; +} Bar_Body; + +typedef union F { + F_Tag tag; + Foo_Body foo; + Bar_Body bar; +} F; + +enum H_Tag { + Hello, + There, + Everyone, +}; +typedef uint8_t H_Tag; + +typedef struct Hello_Body { + int16_t _0; +} Hello_Body; + +typedef struct There_Body { + uint8_t x; + int16_t y; +} There_Body; + +typedef struct H { + H_Tag tag; + union { + Hello_Body hello; + There_Body there; + }; +} H; + +void root(A x, B y, C z, F f, H h); diff --git a/tests/expectations/tag/annotation.c b/tests/expectations/tag/annotation.c index 86bdca5..2d7ad94 100644 --- a/tests/expectations/tag/annotation.c +++ b/tests/expectations/tag/annotation.c @@ -17,4 +17,52 @@ struct B { float y; }; -void root(struct A x, struct B y, C z); +enum F_Tag { + Foo, + Bar, + Baz, +}; +typedef uint8_t F_Tag; + +struct Foo_Body { + F_Tag tag; + int16_t _0; +}; + +struct Bar_Body { + F_Tag tag; + uint8_t x; + int16_t y; +}; + +union F { + enum F_Tag tag; + struct Foo_Body foo; + struct Bar_Body bar; +}; + +enum H_Tag { + Hello, + There, + Everyone, +}; +typedef uint8_t H_Tag; + +struct Hello_Body { + int16_t _0; +}; + +struct There_Body { + uint8_t x; + int16_t y; +}; + +struct H { + enum H_Tag tag; + union { + struct Hello_Body hello; + struct There_Body there; + }; +}; + +void root(struct A x, struct B y, C z, union F f, struct H h); diff --git a/tests/rust/annotation.rs b/tests/rust/annotation.rs index 27cf4d0..7070592 100644 --- a/tests/rust/annotation.rs +++ b/tests/rust/annotation.rs @@ -36,5 +36,8 @@ enum H { pub extern "C" fn root( x: A, y: B, - z: C + z: C, + f: F, + h: H, ) { } +