#include "numbers.h"
#include "unity.h"
#include "unity_internals.h"

void test_createNumbers_no_null(void) 
{
    unsigned int *arr = createNumbers(10);
    TEST_ASSERT_NOT_NULL(arr);
    free(arr);
}

void test_createNumbers_has_exactly_one_duplicate(void) 
{
    unsigned int len = 100;
    unsigned int *arr = createNumbers(len);
    TEST_ASSERT_NOT_NULL(arr);

    unsigned int dup = getDuplicate(arr, len);
    TEST_ASSERT_NOT_EQUAL(0, dup);

    int count = 0;
    for (unsigned int i = 0; i < len; i++) 
    {
        if (arr[i] == dup) 
            count++;
    }
    
    TEST_ASSERT_EQUAL(2, count);

    free(arr);
}

void test_createNumbers_has_correct_value_range(void)
{
    unsigned int len = 10;
    unsigned int *arr = createNumbers(len);
    TEST_ASSERT_NOT_NULL(arr);

    // Check all numbers are within valid range (1 to 2*len)
    for (unsigned int i = 0; i < len; i++)
    {
        TEST_ASSERT_TRUE(arr[i] >= 1);
        TEST_ASSERT_TRUE(arr[i] <= 2 * len);
    }

    free(arr);
}

void test_getDuplicate_returns_correct_value(void) 
{
    unsigned int arr[6] = {4, 1, 7, 7, 3, 2};
    unsigned int d = getDuplicate(arr, 6);
    TEST_ASSERT_EQUAL_UINT(7, d);
}

void test_complete_function_of_numbers(void) 
{
    unsigned int len = 50;
    unsigned int *arr = createNumbers(len);
    TEST_ASSERT_NOT_NULL(arr);

    unsigned int d1 = getDuplicate(arr, len);
    TEST_ASSERT_NOT_EQUAL(0, d1);

    int count = 0;

    for (unsigned int i = 0; i < len; i++) 
    {
        if (arr[i] == d1) 
            count++;
    }

    TEST_ASSERT_EQUAL(2, count);

    free(arr);
}

int main(void) 
{
    UNITY_BEGIN();

    RUN_TEST(test_createNumbers_no_null);
    RUN_TEST(test_createNumbers_has_exactly_one_duplicate);
    RUN_TEST(test_createNumbers_has_correct_value_range);
    RUN_TEST(test_getDuplicate_returns_correct_value);
    RUN_TEST(test_complete_function_of_numbers);

    return UNITY_END();
}