using LeetCode.Logic;

namespace LeetCode.Tests;

public class LinkedListMergerTests
{
    private readonly LinkedListMerger _merger = new();

    [Fact]
    public void MergeTwoLists_BothEmpty_ReturnsNull()
    {
        var result = _merger.MergeTwoLists(null, null);
        Assert.Null(result);
    }

    [Fact]
    public void MergeTwoLists_OneEmpty_ReturnsOther()
    {
        var list2 = CreateList([0]);
        var result = _merger.MergeTwoLists(null, list2);
        
        Assert.Equal(0, result?.val);
        Assert.Null(result?.next);
    }

    [Fact]
    public void MergeTwoLists_StandardLists_ReturnsSortedMergedList()
    {
        var list1 = CreateList([1, 2, 4]);
        var list2 = CreateList([1, 3, 4]);
        
        var result = _merger.MergeTwoLists(list1, list2);
        
        int[] expected = [1, 1, 2, 3, 4, 4];
        Assert.Equal(expected, ListToArray(result));
    }

    // Helper methods for testing
    private static ListNode? CreateList(int[] values)
    {
        if (values.Length == 0) return null;
        ListNode head = new ListNode(values[0]);
        ListNode current = head;
        for (int i = 1; i < values.Length; i++)
        {
            current.next = new ListNode(values[i]);
            current = current.next;
        }
        return head;
    }

    private static List<int> ListToArray(ListNode? head)
    {
        var result = new List<int>();
        while (head != null)
        {
            result.Add(head.val);
            head = head.next;
        }
        return result;
    }
}