go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/kana-server/pkg/kana/helpers_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package kana_test
     9  
    10  import (
    11  	"fmt"
    12  	"math/rand"
    13  	"testing"
    14  
    15  	. "go.charczuk.com/sdk/assert"
    16  
    17  	"go.charczuk.com/projects/kana-server/pkg/kana"
    18  )
    19  
    20  func Test_SelectWeighted_uniform(t *testing.T) {
    21  	// THIS STILL FAILS RANDOMLY. WHY DO YOU DO THIS.
    22  	t.Skip()
    23  
    24  	// used fix seed for various reasons
    25  	rand.Seed(120812)
    26  
    27  	kvs := GenerateKeyValues(10)
    28  	kvws := GenerateKeyWeightsDefault(10)
    29  
    30  	var key, value string
    31  	selections := make(map[string]int)
    32  	for x := 0; x < 1024; x++ {
    33  		key, value = kana.SelectWeighted(kvs, kvws)
    34  		ItsEqual(t, value, kvs[key])
    35  		selections[key]++
    36  	}
    37  
    38  	fudge := 25
    39  	expectedMin := (1024 / 10) - fudge
    40  	expectedMax := (1024 / 10) + fudge
    41  	var total int
    42  	for _, count := range selections {
    43  		total += count
    44  		ItsEqual(t, true, count < expectedMax, fmt.Sprintf("%d should be < %v and > %v", count, expectedMax, expectedMin))
    45  		ItsEqual(t, true, count > expectedMin, fmt.Sprintf("%d should be < %v and > %v", count, expectedMax, expectedMin))
    46  	}
    47  	ItsEqual(t, total, 1024)
    48  }
    49  
    50  func Test_SelectWeighted_increasing(t *testing.T) {
    51  	kvs := GenerateKeyValues(10)
    52  	kvws := GenerateKeyWeightsIncreasing(10)
    53  
    54  	var key, value string
    55  	selections := make(map[string]int)
    56  	for x := 0; x < 1024; x++ {
    57  		key, value = kana.SelectWeighted(kvs, kvws)
    58  		ItsEqual(t, value, kvs[key])
    59  		selections[key]++
    60  	}
    61  
    62  	var previous int
    63  	for x := 0; x < 10; x++ {
    64  		key := "k" + fmt.Sprint(x)
    65  		count := selections[key]
    66  		ItsEqual(t, true, count >= previous, fmt.Sprintf("%d should be > than %d, %#v", count, previous, selections))
    67  	}
    68  }
    69  
    70  func Test_SelectWeighted_decreasing(t *testing.T) {
    71  	kvs := GenerateKeyValues(10)
    72  	kvws := GenerateKeyWeightsIncreasing(10)
    73  
    74  	var key, value string
    75  	selections := make(map[string]int)
    76  	for x := 0; x < 1024; x++ {
    77  		key, value = kana.SelectWeighted(kvs, kvws)
    78  		ItsEqual(t, value, kvs[key])
    79  		selections[key]++
    80  	}
    81  
    82  	var previous int
    83  	for x := 0; x < 10; x++ {
    84  		key := "k" + fmt.Sprint(x)
    85  		count := selections[key]
    86  		ItsEqual(t, true, count >= previous, fmt.Sprintf("%d should be > than %d, %#v", count, previous, selections))
    87  	}
    88  }