1 module utils.binary.tests;
2 
3 import
4 		std.meta,
5 		std.file,
6 
7 		utils.misc,
8 		utils.binary,
9 		utils.binary.helpers;
10 
11 
12 unittest
13 {
14 	static struct Test
15 	{
16 		enum X = 10;
17 
18 		enum Y
19 		{
20 			i = 12
21 		}
22 
23 		static struct S
24 		{
25 			uint k = 4;
26 		}
27 
28 		static int sx = 1;
29 		__gshared int gx = 2;
30 
31 		Y y;
32 		static Y sy;
33 
34 		static void f() {}
35 		static void f2() pure nothrow @nogc @safe {}
36 
37 		shared void g() {}
38 
39 		static void function() fp;
40 		__gshared void function() gfp;
41 		void function() fpm;
42 
43 		void delegate() dm;
44 		static void delegate() sd;
45 
46 		void m() {}
47 		final void m2() const pure nothrow @nogc @safe {}
48 
49 		inout(int) iom() inout { return 10; }
50 		static inout(int) iosf(inout int x) { return x; }
51 
52 		@property int p() { return 10; }
53 		static @property int sp() { return 10; }
54 
55 		union
56 		{
57 			int a = 11;
58 			float b;
59 			long u;
60 			double gg;
61 		}
62 
63 		S s;
64 		static immutable char[4] c = `ABCD`;
65 		string d = `abc`;
66 
67 		@(`uint`) int[] e = [ 1, 2, 3 ];
68 		@(`length`, `3`) int[] r = [ 4, 5, 6 ];
69 	}
70 
71 	static assert(fieldsToProcess!Test == [ `y`, `u`, `s`, `c`, `d`, `e`, `r` ]);
72 
73 	ubyte[] data =
74 	[
75 		12, 0, 0, 0,				// y
76 		11, 0, 0, 0, 0, 0, 0, 0,	// a
77 		4, 0, 0, 0,					// S.k
78 		65, 66, 67, 68,				// c
79 		97, 98, 99, 0,				// d, null terminated
80 		3, 0, 0, 0,					// e.length
81 		1, 0, 0, 0,					// e[0]
82 		2, 0, 0, 0,					// e[1]
83 		3, 0, 0, 0,					// e[3]
84 		4, 0, 0, 0,					// r[0], length is set by the user
85 		5, 0, 0, 0,					// r[1]
86 		6, 0, 0, 0					// r[2]
87 	];
88 
89 	Test t;
90 
91 	assert(t.binaryWrite == data);
92 	assert(data.binaryRead!Test == t);
93 
94 	auto name = `__tmp`;
95 	binaryWriteFile(name, t);
96 
97 	assert(read(name) == data);
98 	assert(binaryReadFile!Test(name) == t);
99 
100 	remove(name);
101 }