Files
lux/examples/test_lists.lux
2026-02-17 06:52:44 -05:00

55 lines
1.8 KiB
Plaintext

fn test_list_length(): Unit with {Test} = {
Test.assertEqual(0, List.length([]))
Test.assertEqual(1, List.length([1]))
Test.assertEqual(3, List.length([1, 2, 3]))
}
fn test_list_head(): Unit with {Test} = {
Test.assertEqual(Some(1), List.head([1, 2, 3]))
Test.assertEqual(None, List.head([]))
}
fn test_list_tail(): Unit with {Test} = {
Test.assertEqual(Some([2, 3]), List.tail([1, 2, 3]))
Test.assertEqual(None, List.tail([]))
}
fn test_list_get(): Unit with {Test} = {
let xs = [10, 20, 30]
Test.assertEqual(Some(10), List.get(xs, 0))
Test.assertEqual(Some(20), List.get(xs, 1))
Test.assertEqual(Some(30), List.get(xs, 2))
Test.assertEqual(None, List.get(xs, 3))
}
fn test_list_map(): Unit with {Test} = {
let double = fn(x: Int): Int => x * 2
Test.assertEqual([2, 4, 6], List.map([1, 2, 3], double))
Test.assertEqual([], List.map([], double))
}
fn test_list_filter(): Unit with {Test} = {
let isEven = fn(x: Int): Bool => x % 2 == 0
Test.assertEqual([2, 4], List.filter([1, 2, 3, 4, 5], isEven))
Test.assertEqual([], List.filter([1, 3, 5], isEven))
}
fn test_list_fold(): Unit with {Test} = {
let sum = fn(acc: Int, x: Int): Int => acc + x
Test.assertEqual(6, List.fold([1, 2, 3], 0, sum))
Test.assertEqual(10, List.fold([1, 2, 3], 4, sum))
Test.assertEqual(0, List.fold([], 0, sum))
}
fn test_list_reverse(): Unit with {Test} = {
Test.assertEqual([3, 2, 1], List.reverse([1, 2, 3]))
Test.assertEqual([], List.reverse([]))
Test.assertEqual([1], List.reverse([1]))
}
fn test_list_concat(): Unit with {Test} = {
Test.assertEqual([1, 2, 3, 4], List.concat([1, 2], [3, 4]))
Test.assertEqual([1, 2], List.concat([1, 2], []))
Test.assertEqual([3, 4], List.concat([], [3, 4]))
}