package main

import (
	"math"
	"reflect"
	"strconv"
	"testing"
)

func TestCount(t *testing.T) {
	if got := Count([]int{2, 1, 2}); !reflect.DeepEqual(got, map[int]int{1: 1, 2: 2}) {
		t.Fatal(got)
	}
	if got := Count([]string(nil)); len(got) != 0 {
		t.Fatal(got)
	}
	if !dynamicKeyPanics() {
		t.Fatal("slice dynamic key must panic")
	}
	nan := math.NaN()
	if len(Count([]float64{nan, nan})) != 2 {
		t.Fatal("NaN is not equal to itself")
	}
}
func TestFilter(t *testing.T) {
	xs := IDs{1, 2, 3, 4}
	var got IDs = Filter(xs, func(x UserID) bool { return x%2 == 0 })
	if !reflect.DeepEqual(got, IDs{2, 4}) {
		t.Fatal(got)
	}
	got[0] = 99
	if xs[1] != 2 {
		t.Fatal("filter must not reuse the source array in this implementation")
	}
}
func TestGenericMethodAndInference(t *testing.T) {
	b := Box[int]{Value: 42}.Map(strconv.Itoa)
	if b.Get() != "42" {
		t.Fatal(b)
	}
	var g func(int) int = Identity
	h := IntFunc(Identity)
	if g(8) != 8 || h(9) != 9 {
		t.Fatal("function inference")
	}
}
func TestSum(t *testing.T) {
	if Sum(UserID(2), UserID(3)) != UserID(5) || Sum[int]() != 0 {
		t.Fatal("sum")
	}
}
