#include <stdio.h>
#include <string.h>
#include <math.h>

#include <munit/munit.h>

#include <echidna.h>
#include <ll.h>
#include <macros.h>
#include <runtime.h>
#include <symbol.h>

#include <suite.h>


//  xxd -i src/rtc.txt

static char _Source[] = {
    0x50, 0x52, 0x4f, 0x47, 0x52, 0x41, 0x4d, 0x20, 
    0x74, 0x65, 0x73, 0x74, 0x0a, 0x56, 0x41, 0x52, 
    0x20, 0x63, 0x6c, 0x6f, 0x63, 0x6b, 0x3a, 0x20, 
    0x72, 0x74, 0x63, 0x3b, 0x20, 0x45, 0x4e, 0x44, 
    0x5f, 0x56, 0x41, 0x52, 0x0a, 0x56, 0x41, 0x52, 
    0x20, 0x6e, 0x6f, 0x77, 0x3a, 0x20, 0x44, 0x41, 
    0x54, 0x45, 0x5f, 0x41, 0x4e, 0x44, 0x5f, 0x54, 
    0x49, 0x4d, 0x45, 0x20, 0x3a, 0x3d, 0x20, 0x44, 
    0x54, 0x23, 0x31, 0x39, 0x37, 0x30, 0x2d, 0x30, 
    0x31, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x30, 0x3a, 
    0x30, 0x30, 0x3a, 0x30, 0x30, 0x3b, 0x20, 0x45, 
    0x4e, 0x44, 0x5f, 0x56, 0x41, 0x52, 0x0a, 0x0a, 
    0x43, 0x41, 0x4c, 0x20, 0x63, 0x6c, 0x6f, 0x63, 
    0x6b, 0x0a, 0x0a, 0x4c, 0x44, 0x20, 0x63, 0x6c, 
    0x6f, 0x63, 0x6b, 0x2e, 0x43, 0x44, 0x54, 0x0a, 
    0x53, 0x54, 0x20, 0x6e, 0x6f, 0x77, 0x0a, 0x5f, 
    0x65, 0x78, 0x69, 0x74, 0x0a, 0x0a, 0x45, 0x4e, 
    0x44, 0x5f, 0x50, 0x52, 0x4f, 0x47, 0x52, 0x41, 
    0x4d, 0x0a, 0x0a, 0x43, 0x4f, 0x4e, 0x46, 0x49, 
    0x47, 0x55, 0x52, 0x41, 0x54, 0x49, 0x4f, 0x4e, 
    0x20, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x31, 
    0x0a, 0x54, 0x41, 0x53, 0x4b, 0x20, 0x74, 0x61, 
    0x73, 0x6b, 0x31, 0x20, 0x28, 0x50, 0x52, 0x49, 
    0x4f, 0x52, 0x49, 0x54, 0x59, 0x20, 0x3a, 0x3d, 
    0x20, 0x31, 0x29, 0x3b, 0x0a, 0x50, 0x52, 0x4f, 
    0x47, 0x52, 0x41, 0x4d, 0x20, 0x70, 0x72, 0x6f, 
    0x67, 0x72, 0x61, 0x6d, 0x31, 0x3a, 0x20, 0x74, 
    0x65, 0x73, 0x74, 0x3b, 0x0a, 0x45, 0x4e, 0x44, 
    0x5f, 0x43, 0x4f, 0x4e, 0x46, 0x49, 0x47, 0x55, 
    0x52, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x0a, 0x00
};


static void
_test_rtc_callback(ECHIDNA *Context, void *Arg, void *User) {
    RUNTIME *pRun;
    RUNTIME_CONTEXT *pContext;
    SYMBOL *pSymbol;
    VALUE sValue;
    struct timespec sTp;
    struct tm sTime, sNow;

    clock_gettime(CLOCK_REALTIME, &sTp);
    gmtime_r(&sTp.tv_sec, &sTime);

    /* Arg == &RUNTIME_CONTEXT.Stats */
    munit_assert_not_null(Arg);
    munit_assert_not_null(pContext = container_of(Arg, RUNTIME_CONTEXT, Stats));
    munit_assert_not_null(pRun = (RUNTIME *) pContext->Parent);

    munit_assert_not_null(pSymbol = symbol_search(Context, "config1", "__Single", "program1", "now"));
    value_copy(&sValue, &pSymbol->Value);
    memcpy((char *) &sValue.Value.Pointer, &pRun->Memory[pSymbol->Offset], sValue.Length);
    munit_assert_uint32(sValue.Type, ==, TYPE_DT);
    gmtime_r((const time_t *) &sValue.Value.DateTime, &sNow);
    munit_assert_int(sNow.tm_year, ==, sTime.tm_year);
    munit_assert_int(sNow.tm_mon, ==, sTime.tm_mon);
    munit_assert_int(sNow.tm_mday, ==, sTime.tm_mday);
    munit_assert_int(sNow.tm_hour, ==, sTime.tm_hour);
    munit_assert_int(sNow.tm_min, ==, sTime.tm_min);
}


MunitResult 
test_rtc(const MunitParameter Parameters[], void *Fixture) {
    ECHIDNA *pContext;

    pContext = (ECHIDNA *) Fixture;
    munit_assert_not_null(pContext);

    munit_assert_int(test_parse(pContext, _Source), ==, 0);
    pContext->Option |= (OPTION_COMPILE | OPTION_RUN);
    pContext->Output = NULL;
    munit_assert_int(echidna_compile(pContext), ==, 0);
    munit_assert_int(echidna_callback(pContext, CALLBACK_CYCLE_FINISH, NULL, _test_rtc_callback), ==, 0);
    munit_assert_int(echidna_run(pContext), ==, 0);

    return MUNIT_OK;
}


