Collect all of the various spread out 3rd party licenses to a single file.
[SquirrelJME.git] / nanocoat / tests / testStreamWriteValueJ.c
blob70673f5d0d29c22d726a618a8ed42c077cfebc7f
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <string.h>
12 #include "mock.h"
13 #include "proto.h"
14 #include "test.h"
15 #include "unit.h"
16 #include "sjme/stream.h"
18 #define DATA_LEN 58
20 /** Expected test data. */
21 static const sjme_jubyte testData[DATA_LEN] =
23 0x00, 0x01, 0x04, 0xD2, 0x11, 0xD7, 0x00,
24 0x73, 0x00, 0x71, 0x00, 0xBC, 0x61, 0x4E,
25 0x05, 0x39, 0x7F, 0xB1, 0x12, 0x34, 0x56,
26 0x78, 0x12, 0x34, 0x56, 0x78, 0x87, 0x65,
27 0x43, 0x21, 0x87, 0x65, 0x43, 0x21, 0x12,
28 0x34, 0x56, 0x78, 0x87, 0x65, 0x43, 0x21,
29 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56,
30 0x78, 0x87, 0x65, 0x43, 0x21, 0x87, 0x65,
31 0x43, 0x21
34 #define WRITE_SEQ(type) \
35 if (sjme_error_is(sjme_stream_outputWriteValueJP(stream, \
36 SJME_TOKEN_PASTE_PP(SJME_BASIC_TYPE_ID_, type), \
37 &value))) \
38 sjme_unit_fail(test, "Could not write %s?", #type) \
40 #define STREAM_SEQ(type, structMember, what) \
41 memset(&value, 0, sizeof(value)); \
42 value.structMember = what; \
43 WRITE_SEQ(type)
45 #define STREAM_SEQ2(type, hiMember, loMember, hiWhat, loWhat) \
46 memset(&value, 0, sizeof(value)); \
47 value.hiMember = hiWhat; \
48 value.loMember = loWhat; \
49 WRITE_SEQ(type)
51 /**
52 * Tests writing Java values to the output.
54 * @since 2024/01/09
56 SJME_TEST_DECLARE(testStreamWriteValueJ)
58 sjme_jvalue value;
59 sjme_stream_output stream;
60 void* buf;
62 /* Setup buffer to write to. */
63 buf = sjme_alloca(DATA_LEN);
64 if (buf == NULL)
65 return sjme_unit_fail(test, "Could not output buffer.");
67 /* Clear buffer. */
68 memset(buf, 0, DATA_LEN);
70 /* Open stream to write all the data in. */
71 stream = NULL;
72 if (sjme_error_is(sjme_stream_outputOpenMemory(test->pool,
73 &stream, buf, DATA_LEN)) ||
74 stream == NULL)
75 return sjme_unit_fail(test, "Could not open initial stream.");
77 /* dos.writeBoolean(false); */
78 STREAM_SEQ(BOOLEAN, z, SJME_JNI_FALSE);
80 /* dos.writeBoolean(true); */
81 STREAM_SEQ(BOOLEAN, z, SJME_JNI_TRUE);
83 /* dos.writeShort(1234); */
84 STREAM_SEQ(SHORT, s, 1234);
86 /* dos.writeShort(4567); */
87 STREAM_SEQ(SHORT, s, 4567);
89 /* dos.writeChar('s'); */
90 STREAM_SEQ(CHARACTER, c, 's');
92 /* dos.writeChar('q'); */
93 STREAM_SEQ(CHARACTER, c, 'q');
95 /* dos.writeInt(12345678); */
96 STREAM_SEQ(INTEGER, i, 12345678);
98 /* dos.writeInt(87654321); */
99 STREAM_SEQ(INTEGER, i, 87654321);
101 /* dos.writeLong(0x1234567812345678L); */
102 STREAM_SEQ2(LONG, j.hi, j.lo, 0x12345678, 0x12345678);
104 /* dos.writeLong(0x8765432187654321L); */
105 STREAM_SEQ2(LONG, j.hi, j.lo, 0x87654321, 0x87654321);
107 /* dos.writeFloat(Float.intBitsToFloat(0x12345678)); */
108 STREAM_SEQ(FLOAT, f.value, 0x12345678);
110 /* dos.writeFloat(Float.intBitsToFloat(0x87654321)); */
111 STREAM_SEQ(FLOAT, f.value, 0x87654321);
113 /* dos.writeDouble(Double.longBitsToDouble(0x1234567812345678L)); */
114 STREAM_SEQ2(DOUBLE, d.hi, d.lo, 0x12345678, 0x12345678);
116 /* dos.writeDouble(Double.longBitsToDouble(0x8765432187654321L)); */
117 STREAM_SEQ2(DOUBLE, d.hi, d.lo, 0x87654321, 0x87654321);
119 /* All the buffer bytes should match. */
120 sjme_unit_equalI(test,
121 0, memcmp(buf, testData, DATA_LEN),
122 "Written buffer does not match?");
124 /* The write count should be the buffer size. */
125 sjme_unit_equalI(test,
126 DATA_LEN, stream->totalWritten,
127 "Number of written bytes incorrect?");
129 /* Close stream. */
130 if (sjme_error_is(sjme_stream_outputClose(stream, NULL)))
131 return sjme_unit_fail(test, "Could not close output stream.");
133 /* Success! */
134 return SJME_TEST_RESULT_PASS;