// 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_write_upgrade_boot_only(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 int transfer_size_bytes;
extern FILE* dfu_file;

#define READ_BLOCK_SIZE_BYTES 1024

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

  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"));
  dfu_file = upgrade_file;

//   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 transfer_size_arg = 0;
  int scan = sscanf(argv[2], "%ld", &transfer_size_arg);
  xassert(scan == 1);
  xassert((transfer_size_arg > 0) && msg("Error: transfer block size must be positive"));
  transfer_size_bytes = transfer_size_arg;

  /* Unity runner */
  UnityBegin("src/test_write_upgrade_boot_only.c");
  run_test(test_write_upgrade_boot_only, "test_write_upgrade_boot_only", 92);

  fclose(upgrade_file);
  return UnityEnd(); 
}
