// Copyright 2020-2026 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <assert.h>
#include <stdio.h>
#include <stdbool.h>

#include "crc.h"

bool verbose = false;

int main(void)
{
  const unsigned char example[] = {
    0x3F, 0x33, 0x31, 0xBB, 0xDF, 0xB6, 0xD4, 0xDC,
    0x99, 0x46, 0x2B, 0x74, 0xC4, 0xD0, 0x61, 0xD4,
    0xEA, 0xAB, 0x6B, 0x9B, 0xA5, 0x65, 0x50, 0x44,
    0x16, 0x58, 0xD0, 0x8C, 0x87, 0x6F, 0x23, 0xF5,
    0xD5, 0xBB, 0x80, 0xB0, 0xB3, 0xD6, 0x00, 0x6E,
    0x19, 0x21, 0x67, 0x20, 0x9B, 0xA0, 0x13, 0xF1,
    0x82, 0xAD, 0x9C, 0x20, 0xB3, 0x11, 0x0F, 0xC7,
    0x92, 0x34, 0x0B, 0x7D, 0x75, 0x17, 0xA5,
  };
  const unsigned expected = 0xD9AE9FF7;

  unsigned crc, final;

  crc = crc_init();
  for (size_t i = 0; i < sizeof(example); i++) {
    crc_step(&crc, example[i]);
  }
  for (size_t i = 0; i < 4; i++) {
    crc_step(&crc, 0); // checksum zeroed out
  }
  final = crc_finish(crc);

  assert(final == expected);

  crc = crc_init();
  for (size_t i = 0; i < sizeof(example); i++) {
    crc_step(&crc, example[i]);
  }
  for (size_t i = 0; i < 4; i++) {
    crc_step(&crc, (final >> (8 * i)) & 0xFF);
  }
  assert(crc_finish(crc) == 0);

  printf("PASS\n");
  return 0;
}
