// Copyright 2025 Gabriel Bjørnager Jensen.

#![cfg(test)]

use core::array;
use multitype::Array;

#[test]
fn test_array_from_fn() {
	let data: [u8; 8] = array::from_fn(|i| 2u16.wrapping_pow(i as u32).wrapping_sub(1) as u8);

	#[track_caller]
	fn test<T: Array<Scalar = u8>>(data: &T) {
		assert_eq!(
			data.as_slice(),
			&[0, 1, 3, 7, 15, 31, 63, 127],
		);
	}

	test(&data);
}

#[test]
fn test_array_repeat() {
	static mut COUNTER: u8 = 0;

	struct Foo;

	impl Clone for Foo {
		fn clone(&self) -> Self {
			// SAFETY: We guarantee that only one thread ac-
			// cesses `COUNTER`.
			unsafe { COUNTER = COUNTER.checked_add(1).unwrap() };

			Self
		}
	}

	fn repeat<T: Array<Scalar = Foo>>() -> T {
		T::repeat(Foo)
	}

	repeat::<[Foo; 0001]>();
	repeat::<[Foo; 0002]>();
	repeat::<[Foo; 0004]>();
	repeat::<[Foo; 0008]>();
	repeat::<[Foo; 0016]>();
	repeat::<[Foo; 0032]>();

	assert_eq!(unsafe { COUNTER }, 63);
}
