RetroArch/libretro-db/rmsgpack_test.c

177 lines
3.1 KiB
C
Raw Normal View History

2015-01-19 21:47:09 +00:00
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include "rmsgpack.h"
2015-01-27 03:02:10 +00:00
struct stub_state
{
2015-01-19 21:47:09 +00:00
int i;
uint64_t stack[256];
};
2015-01-27 03:02:10 +00:00
static void stub_state_push_map(struct stub_state *s, uint32_t size)
{
2015-01-19 21:47:09 +00:00
s->i++;
s->stack[s->i] = 1;
s->i++;
s->stack[s->i] = size * 2;
printf("{");
}
2015-01-27 03:02:10 +00:00
static void stub_state_push_array(struct stub_state *s, uint32_t size)
{
2015-01-19 21:47:09 +00:00
s->i++;
s->stack[s->i] = 2;
s->i++;
s->stack[s->i] = size;
printf("[");
}
2015-01-27 03:02:10 +00:00
static void stub_state_pre_print(struct stub_state *s)
{
}
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
static void stub_state_post_print(struct stub_state *s)
{
switch (s->stack[s->i - 1])
{
case 1:
if (s->stack[s->i] % 2 == 0)
{
printf(": ");
s->stack[s->i]--;
}
else if (s->stack[s->i] == 1)
{
printf("}");
s->i -= 2;
stub_state_post_print(s);
}
else
{
printf(", ");
s->stack[s->i]--;
}
break;
case 2:
if (s->stack[s->i] == 1)
{
printf("]");
s->i -= 2;
stub_state_post_print(s);
}
else
{
printf(", ");
s->stack[s->i]--;
}
break;
}
2015-01-19 21:47:09 +00:00
}
2015-01-27 03:02:10 +00:00
static int stub_read_map_start(uint32_t size, void *data)
{
stub_state_push_map(data, size);
return 0;
2015-01-19 21:47:09 +00:00
}
2015-01-27 03:02:10 +00:00
static int stub_read_array_start(uint32_t size, void *data)
{
2015-01-19 21:47:09 +00:00
stub_state_push_array(data, size);
return 0;
}
2015-01-27 03:02:10 +00:00
static int stub_read_string(char *s, uint32_t len, void *data)
{
stub_state_pre_print(data);
printf("'%s'", s);
stub_state_post_print(data);
free(s);
return 0;
2015-01-19 21:47:09 +00:00
}
static int stub_read_bin(
void * s,
uint32_t len,
void * data
){
stub_state_pre_print(data);
printf("b'%s'", s);
stub_state_post_print(data);
free(s);
return 0;
}
2015-01-27 03:02:10 +00:00
static int stub_read_uint(uint64_t value, void *data)
{
stub_state_pre_print(data);
2015-01-20 21:11:00 +00:00
#ifdef _WIN32
printf("%I64u", (unsigned long long)value);
#else
2015-01-27 03:02:10 +00:00
printf("%llu", (unsigned long long)value);
2015-01-20 21:11:00 +00:00
#endif
2015-01-27 03:02:10 +00:00
stub_state_post_print(data);
return 0;
2015-01-19 21:47:09 +00:00
}
2015-01-27 03:02:10 +00:00
static int stub_read_nil(void * data)
{
stub_state_pre_print(data);
printf("nil");
stub_state_post_print(data);
return 0;
2015-01-19 21:47:09 +00:00
}
2015-01-27 03:02:10 +00:00
static int stub_read_int(int64_t value, void * data)
{
stub_state_pre_print(data);
2015-01-20 21:11:00 +00:00
#ifdef _WIN32
printf("%I64d", (signed long long)value);
#else
2015-01-27 03:02:10 +00:00
printf("%lld", (signed long long)value);
2015-01-20 21:11:00 +00:00
#endif
2015-01-27 03:02:10 +00:00
stub_state_post_print(data);
return 0;
2015-01-19 21:47:09 +00:00
}
2015-01-27 03:02:10 +00:00
static int stub_read_bool(int value, void * data)
{
stub_state_pre_print(data);
if (value)
printf("true");
else
printf("false");
stub_state_post_print(data);
return 0;
2015-01-19 21:47:09 +00:00
}
static struct rmsgpack_read_callbacks stub_callbacks = {
stub_read_nil,
stub_read_bool,
stub_read_int,
stub_read_uint,
stub_read_string,
stub_read_bin,
stub_read_map_start,
stub_read_array_start
};
2015-01-27 03:02:10 +00:00
int main(void)
{
struct stub_state state;
RFILE *fd = retro_fopen("test.msgpack", RFILE_MODE_READ, 0);
2015-01-27 03:02:10 +00:00
state.i = 0;
state.stack[0] = 0;
rmsgpack_read(fd, &stub_callbacks, &state);
2015-09-17 05:01:34 +00:00
printf("Test succeeded.\n");
retro_fclose(fd);
2015-01-27 03:02:10 +00:00
return 0;
2015-01-19 21:47:09 +00:00
}