// Copyright 2026 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.

/* Was AUTOGENERATED FILE. From lib_unity. */

/*=======Automagically Detected Files To Include=====*/
#include "unity.h"
#include "dfu_flash.h"
#include <platform.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <xcore/hwtimer.h>

#define XASSERT_ENABLE_DEBUG 1
#define XASSERT_ENABLE_LINE_NUMBERS 1
#include "xassert.h"

/*=======External Functions This Runner Calls=====*/
extern void setUp(void);
extern void tearDown(void);
extern void test_dfu_image_analysis(void);
extern void test_dfu_flash_is_suitable(void);
extern void test_dfu_flash_prepare_slot_reports_OK(void);
extern void test_dfu_flash_write_reports_OK(void);
extern void test_dfu_flash_verify_reports_OK(void);


/*=======Mock Management=====*/
static void CMock_Init(void)
{
}
static void CMock_Verify(void)
{
}
static void CMock_Destroy(void)
{
}

/*=======Test Reset Options=====*/
void resetTest(void);
void resetTest(void)
{
  tearDown();
  CMock_Verify();
  CMock_Destroy();
  CMock_Init();
  setUp();
}
void verifyTest(void);
void verifyTest(void)
{
  CMock_Verify();
}

/*=======Test Runner Used To Run Each Test=====*/
static void run_test(UnityTestFunction func, const char* name, UNITY_LINE_TYPE line_num)
{
    Unity.CurrentTestName = name;
    Unity.CurrentTestLineNumber = line_num;
#ifdef UNITY_USE_COMMAND_LINE_ARGS
    if (!UnityTestMatches())
        return;
#endif
    Unity.NumberOfTests++;
    UNITY_CLR_DETAILS();
    UNITY_EXEC_TIME_START();
    CMock_Init();
    if (TEST_PROTECT())
    {
        setUp();
        func();
    }
    if (TEST_PROTECT())
    {
        tearDown();
        CMock_Verify();
    }
    CMock_Destroy();
    UNITY_EXEC_TIME_STOP();
    UnityConcludeTest();
}

extern uint32_t erase_timing_threshold_ms;
extern uint32_t write_timing_threshold_ms;
extern uint8_t* upgrade_mem;

#define READ_BLOCK_SIZE_BYTES 1024

/*=======MAIN=====*/
int main(int argc, char * argv[])
{
  xassert((argc == 4) && msg("Usage: <file>.xe <upgrade-image-file> <erase-timing> <write-timing>"));

  FILE* upgrade_file = fopen(argv[1], "rb");
  xassert(upgrade_file != NULL && msg("Error: failed to open upgrade image file"));
  upgrade_mem = malloc(20 * READ_BLOCK_SIZE_BYTES);
  xassert(upgrade_mem != NULL && msg("Error: failed to allocate memory"));

  size_t read = fread(upgrade_mem, 1, 20 * READ_BLOCK_SIZE_BYTES, upgrade_file);
  xassert(feof(upgrade_file) && msg("Error: failed to read entire upgrade image file"));
  printf("Read total %zu\n", read);

  int32_t time_temp;
  int scan = sscanf(argv[2], "%ld", &time_temp);
  xassert(scan == 1);
  xassert((time_temp > 0) && msg("Error: timing threshold must be positive"));
  erase_timing_threshold_ms = (uint32_t)time_temp;

  scan = sscanf(argv[3], "%ld", &time_temp);
  xassert(scan == 1);
  xassert((time_temp > 0) && msg("Error: timing threshold must be positive"));
  write_timing_threshold_ms = (uint32_t)time_temp;

  /* Unity runner */
  UnityBegin("src/test_prepare_upgrade_slot.c");
  run_test(test_dfu_image_analysis, "test_dfu_image_analysis", 92);
  run_test(test_dfu_flash_is_suitable, "test_dfu_flash_is_suitable", 99);
  run_test(test_dfu_flash_prepare_slot_reports_OK, "test_dfu_flash_prepare_slot_reports_OK", 107);
  run_test(test_dfu_flash_write_reports_OK, "test_dfu_flash_write_reports_OK", 133);
  run_test(test_dfu_flash_verify_reports_OK, "test_dfu_flash_verify_reports_OK", 149);

  fclose(upgrade_file);
  return UnityEnd(); 
}
