213 lines
5.9 KiB
C
213 lines
5.9 KiB
C
/* - | Copyright / About | ----------------------------------------------------
|
|
Copyright (c) 2026 Randy Jordan
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
the Software without restriction, including without limitation the rights to
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
of the Software, and to permit persons to whom the Software is furnished to do
|
|
so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
|
|
* --------------------------------------------------------------------------*/
|
|
#define DEBUG
|
|
#include "../include/platform/mem/ci2_pack.h"
|
|
#include <float.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
static inline void
|
|
packu64(uint8_t* buf, uint64_t v)
|
|
{
|
|
buf[0] = (uint8_t)(v >> 56);
|
|
buf[1] = (uint8_t)(v >> 48);
|
|
buf[2] = (uint8_t)(v >> 40);
|
|
buf[3] = (uint8_t)(v >> 32);
|
|
buf[4] = (uint8_t)(v >> 24);
|
|
buf[5] = (uint8_t)(v >> 16);
|
|
buf[6] = (uint8_t)(v >> 8);
|
|
buf[7] = (uint8_t)(v);
|
|
}
|
|
|
|
static void
|
|
test_integers(void)
|
|
{
|
|
uint8_t buf[1024];
|
|
|
|
int8_t g1, g2;
|
|
uint8_t G1, G2;
|
|
int16_t h1, h2;
|
|
uint16_t H1, H2;
|
|
int32_t i1, i2;
|
|
uint32_t I1, I2;
|
|
int64_t j1, j2;
|
|
uint64_t J1, J2;
|
|
|
|
size_t n = ci2_pack(buf,
|
|
"gG hH iI jJ",
|
|
INT8_MIN,
|
|
UINT8_MAX,
|
|
INT16_MIN,
|
|
UINT16_MAX,
|
|
INT32_MIN,
|
|
UINT32_MAX,
|
|
INT64_MIN,
|
|
UINT64_MAX);
|
|
size_t m = ci2_pre_pack("gG hH iI jJ",
|
|
INT8_MIN,
|
|
UINT8_MAX,
|
|
INT16_MIN,
|
|
UINT16_MAX,
|
|
INT32_MIN,
|
|
UINT32_MAX,
|
|
INT64_MIN,
|
|
UINT64_MAX);
|
|
|
|
CI2_ASSERT(n == 1 + 1 + 2 + 2 + 4 + 4 + 8 + 8);
|
|
CI2_ASSERT(n == m);
|
|
ci2_unpack(buf, "gG hH iI jJ", &g1, &G1, &h1, &H1, &i1, &I1, &j1, &J1);
|
|
|
|
CI2_ASSERT(g1 == INT8_MIN);
|
|
CI2_ASSERT(G1 == UINT8_MAX);
|
|
CI2_ASSERT(h1 == INT16_MIN);
|
|
CI2_ASSERT(H1 == UINT16_MAX);
|
|
CI2_ASSERT(i1 == INT32_MIN);
|
|
CI2_ASSERT(I1 == UINT32_MAX);
|
|
CI2_ASSERT(j1 == INT64_MIN);
|
|
CI2_ASSERT(J1 == UINT64_MAX);
|
|
|
|
/* also test the other extremes */
|
|
n = ci2_pack(
|
|
buf, "gG hH iI jJ", INT8_MAX, 0, INT16_MAX, 0, INT32_MAX, 0, INT64_MAX, 0);
|
|
|
|
ci2_unpack(buf, "gG hH iI jJ", &g2, &G2, &h2, &H2, &i2, &I2, &j2, &J2);
|
|
|
|
CI2_ASSERT(g2 == INT8_MAX);
|
|
CI2_ASSERT(G2 == 0);
|
|
CI2_ASSERT(h2 == INT16_MAX);
|
|
CI2_ASSERT(H2 == 0);
|
|
CI2_ASSERT(i2 == INT32_MAX);
|
|
CI2_ASSERT(I2 == 0);
|
|
CI2_ASSERT(j2 == INT64_MAX);
|
|
CI2_ASSERT(J2 == 0);
|
|
}
|
|
|
|
static void
|
|
test_floats(void)
|
|
{
|
|
uint8_t buf[1024];
|
|
float f1, f2;
|
|
double d1, d2;
|
|
|
|
size_t n = ci2_pack(buf, "fF", 0.0f, 0.0);
|
|
size_t m = ci2_pre_pack("fF", 0.0f, 0.0);
|
|
CI2_ASSERT(n == m);
|
|
CI2_ASSERT(n == 4 + 8);
|
|
ci2_unpack(buf, "fF", &f1, &d1);
|
|
CI2_ASSERT(f1 == 0.0f);
|
|
CI2_ASSERT(d1 == 0.0);
|
|
|
|
n = ci2_pack(buf, "fF", -123.5f, 3490.5);
|
|
m = ci2_pre_pack("fF", -123.5f, 3490.5);
|
|
CI2_ASSERT(n == m);
|
|
ci2_unpack(buf, "fF", &f2, &d2);
|
|
CI2_ASSERT(f2 == -123.5f);
|
|
CI2_ASSERT(d2 == 3490.5);
|
|
|
|
/* IEEE-754 helpers support them, test extremes too */
|
|
n = ci2_pack(buf, "fF", FLT_MAX, DBL_MAX);
|
|
m = ci2_pre_pack("fF", FLT_MAX, DBL_MAX);
|
|
CI2_ASSERT(n == m);
|
|
ci2_unpack(buf, "fF", &f1, &d1);
|
|
CI2_ASSERT(f1 == FLT_MAX);
|
|
CI2_ASSERT(d1 == DBL_MAX);
|
|
}
|
|
|
|
static void
|
|
test_strings(void)
|
|
{
|
|
uint8_t buf[1024];
|
|
char in[] = "Great unmitigated Zot! You've found the Runestaff!";
|
|
char out[128];
|
|
char out2[16];
|
|
|
|
size_t n = ci2_pack(buf, "s", in);
|
|
size_t m = ci2_pre_pack("s", in);
|
|
CI2_ASSERT(n == m);
|
|
CI2_ASSERT(n == 8 + strlen(in));
|
|
|
|
ci2_unpack(buf, "s", out);
|
|
CI2_ASSERT(strcmp(out, in) == 0);
|
|
|
|
/* max string length handling: format "15s" means cap at 15 chars incl NUL */
|
|
ci2_unpack(buf, "15s", out2);
|
|
CI2_ASSERT(strcmp(out2, "Great unmitiga") ==
|
|
0); /* 15 chars total incl '\0' => 14 copied */
|
|
CI2_ASSERT(out2[15 - 1] == '\0');
|
|
}
|
|
|
|
static void
|
|
test_mixed_packet(void)
|
|
{
|
|
uint8_t buf[1024];
|
|
|
|
uint8_t magic;
|
|
uint64_t ps2;
|
|
int16_t monkeycount;
|
|
char* s = "Great unmitigated Zot! You've found the Runestaff!";
|
|
char s2[96];
|
|
double absurdityfactor;
|
|
size_t packetsize;
|
|
size_t packetsize2;
|
|
|
|
packetsize = ci2_pre_pack("JGHsF",
|
|
0, /* placeholder for size */
|
|
'B',
|
|
37,
|
|
s,
|
|
-3490.5);
|
|
packetsize2 = ci2_pack(buf,
|
|
"JGHsF",
|
|
0, /* placeholder for size */
|
|
'B',
|
|
37,
|
|
s,
|
|
-3490.5);
|
|
|
|
CI2_ASSERT(packetsize == packetsize2);
|
|
packu64(buf, (uint64_t)packetsize);
|
|
|
|
ci2_unpack(buf, "JGHsF", &ps2, &magic, &monkeycount, s2, &absurdityfactor);
|
|
|
|
CI2_ASSERT(ps2 == packetsize);
|
|
CI2_ASSERT(magic == 'B');
|
|
CI2_ASSERT(monkeycount == 37);
|
|
CI2_ASSERT(strcmp(s2, s) == 0);
|
|
CI2_ASSERT(absurdityfactor == -3490.5);
|
|
}
|
|
|
|
int
|
|
main(int argc, char* argv[])
|
|
{
|
|
(void)argc;
|
|
(void)argv;
|
|
|
|
test_integers();
|
|
test_floats();
|
|
test_strings();
|
|
test_mixed_packet();
|
|
|
|
puts("All tests passed.");
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|