1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-18 08:06:04 +02:00
Beef/IDEHelper/Tests/src/Unions.bf

60 lines
848 B
Beef
Raw Normal View History

2019-08-23 11:56:54 -07:00
using System;
namespace Tests
{
class Unions
{
// Not really a union
[Union]
struct UnionA
{
public int32 mInt32;
}
[Union]
struct UnionB
{
public int32 mInt32;
public float mFloat;
}
2020-07-18 06:47:47 -07:00
[Union]
struct UnionC
{
public int32 mInt32 = 0;
public float mFloat;
public this()
{
}
}
2022-08-24 16:54:22 -07:00
[Union]
struct UnionD : UnionC
{
public int16 mInt16;
}
2019-08-23 11:56:54 -07:00
[Test]
static void TestBasics()
{
UnionA ua = .();
ua.mInt32 = 123;
Test.Assert(sizeof(UnionA) == 4);
UnionB ub = .();
ub.mInt32 = 123;
*((float*)&ub.mInt32) = 1.2f;
Test.Assert(ub.mFloat == 1.2f);
Test.Assert(sizeof(UnionB) == 4);
2022-08-24 16:54:22 -07:00
UnionD ud = .();
ud.mInt32 = 123;
ud.mInt16 = 234;
Test.Assert(sizeof(UnionD) == 6);
Test.Assert(alignof(UnionD) == 4);
2022-08-24 17:02:55 -07:00
Test.Assert(((int16*)&ud)[2] == 234);
2019-08-23 11:56:54 -07:00
}
}
}