summaryrefslogtreecommitdiff
path: root/vendor/github.com/gotk3/gotk3/glib/list_test.go
blob: 66e36934f49167ebae12636d745e83fd329bb61d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package glib

import (
	"fmt"
	"testing"
	"unsafe"
)

func TestList_Basics(t *testing.T) {
	list := (&List{}).Append(0).Append(1).Append(2)
	if list.Length() != 3 {
		t.Errorf("Length of list with 3 appended elements must be 3. (Got %v).", list.Length())
	}

	list = (&List{}).Prepend(0).Prepend(1).Prepend(2)
	if list.Length() != 3 {
		t.Errorf("Length of list with 3 prepended elements must be 3. (Got %v).", list.Length())
	}

	list = (&List{}).Insert(0, 0).Insert(1, 0).Insert(2, 0)
	if list.Length() != 3 {
		t.Errorf("Length of list with 3 inserted elements must be 3. (Got %v).", list.Length())
	}
}

func TestList_DataWrapper(t *testing.T) {
	list := (&List{}).Append(0).Append(1).Append(2)
	list.DataWrapper(func(ptr unsafe.Pointer) interface{} {
		return fmt.Sprintf("Value %v", uintptr(ptr))
	})

	i := 0
	for l := list; l != nil; l = l.Next() {
		expect := fmt.Sprintf("Value %v", i)
		i++
		actual, ok := l.Data().(string)
		if !ok {
			t.Error("DataWrapper must have returned a string!")
		}
		if actual != expect {
			t.Errorf("DataWrapper returned unexpected result. Expected '%v', got '%v'.", expect, actual)
		}
	}
}

func TestList_Foreach(t *testing.T) {
	list := (&List{}).Append(0).Append(1).Append(2)
	list.DataWrapper(func(ptr unsafe.Pointer) interface{} {
		return int(uintptr(ptr) + 1)
	})

	sum := 0
	list.Foreach(func(item interface{}) {
		sum += item.(int)
	})

	if sum != 6 {
		t.Errorf("Foreach resulted into wrong sum. Got %v, expected %v.", sum, 6)
	}
}

func TestList_Nth(t *testing.T) {
	list := (&List{}).Append(0).Append(1).Append(2)
	list.DataWrapper(func(ptr unsafe.Pointer) interface{} {
		return int(uintptr(ptr) + 1)
	})

	for i := uint(0); i < 3; i++ {
		nth := list.Nth(i).Data().(int)
		nthData := list.NthData(i).(int)

		if nth != nthData {
			t.Errorf("%v's element didn't match. Nth->Data returned %v; NthData returned %v.", i, nth, nthData)
		}
	}
}