github.com/schollz/clusters@v0.0.0-20221201012527-c6c68863636f/common_test.go (about)

     1  package clusters
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestQueueEmptyWhenCreated(t *testing.T) {
     8  	queue := newPriorityQueue(0)
     9  	if queue.NotEmpty() {
    10  		t.Error("Newly created queue is not empty")
    11  	}
    12  }
    13  
    14  func TestQueueNowEmptyAfterAdd(t *testing.T) {
    15  	queue := newPriorityQueue(1)
    16  
    17  	queue.Push(&pItem{
    18  		v: 0,
    19  		p: 0.5,
    20  	})
    21  
    22  	if !queue.NotEmpty() {
    23  		t.Error("Queue is empty after a single add")
    24  	}
    25  }
    26  
    27  func TestQueueReturnsInPriorityOrder(t *testing.T) {
    28  	queue := newPriorityQueue(2)
    29  
    30  	var (
    31  		itemOne = &pItem{
    32  			v: 0,
    33  			p: 0.5,
    34  		}
    35  		itemTwo = &pItem{
    36  			v: 1,
    37  			p: 0.6,
    38  		}
    39  	)
    40  
    41  	queue.Push(itemTwo)
    42  	queue.Push(itemOne)
    43  
    44  	if queue.Pop().(*pItem) != itemOne {
    45  		t.Error("Queue is should return itemOne first")
    46  	}
    47  
    48  	if queue.Pop().(*pItem) != itemTwo {
    49  		t.Error("Queue is should return itemTwo next")
    50  	}
    51  
    52  	if queue.NotEmpty() {
    53  		t.Error("Queue is not empty")
    54  	}
    55  }
    56  
    57  func TestQueueReturnsInPriorityOrderAfterUpdate(t *testing.T) {
    58  	queue := newPriorityQueue(2)
    59  
    60  	var (
    61  		itemOne = &pItem{
    62  			v: 0,
    63  			p: 0.5,
    64  		}
    65  		itemTwo = &pItem{
    66  			v: 1,
    67  			p: 0.6,
    68  		}
    69  	)
    70  
    71  	queue.Push(itemTwo)
    72  	queue.Push(itemOne)
    73  
    74  	queue.Update(itemTwo, 1, 0.4)
    75  
    76  	if queue.Pop().(*pItem) != itemTwo {
    77  		t.Error("Queue is should return itemTwo first")
    78  	}
    79  
    80  	if queue.Pop().(*pItem) != itemOne {
    81  		t.Error("Queue is should return itemOne next")
    82  	}
    83  
    84  	if queue.NotEmpty() {
    85  		t.Error("Queue is not empty")
    86  	}
    87  }
    88  
    89  func TestBounds(t *testing.T) {
    90  	var (
    91  		f = "data/test.csv"
    92  		i = CsvImporter()
    93  		l = 3
    94  	)
    95  
    96  	d, e := i.Import(f, 0, 2)
    97  	if e != nil {
    98  		t.Errorf("Error importing data: %s\n", e.Error())
    99  	}
   100  
   101  	bounds := bounds(d)
   102  
   103  	if len(bounds) != 3 {
   104  		t.Errorf("Mismatched bounds array length: %d vs %d\n", len(bounds), l)
   105  	}
   106  
   107  	if bounds[0][0] != 0.1 || bounds[0][1] != 0.7 {
   108  		t.Error("Invalid bounds for feature #0")
   109  	}
   110  
   111  	if bounds[1][0] != 0.2 || bounds[1][1] != 0.8 {
   112  		t.Error("Invalid bounds for feature #1")
   113  	}
   114  
   115  	if bounds[2][0] != 0.3 || bounds[2][1] != 0.9 {
   116  		t.Error("Invalid bounds for feature #2")
   117  	}
   118  }
   119  
   120  func TestUniform(t *testing.T) {
   121  	var (
   122  		l = 100
   123  		d = &[2]float64{
   124  			0,
   125  			10,
   126  		}
   127  	)
   128  
   129  	for i := 0; i < l; i++ {
   130  		u := uniform(d)
   131  		if u < 0 || u > 10 {
   132  			t.Error("Unformly distributed variable out of bounds")
   133  		}
   134  	}
   135  }