forked from inkyblackness/imgui-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AllocatedGlyphRanges_test.go
123 lines (112 loc) · 4.3 KB
/
AllocatedGlyphRanges_test.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package imgui_test
import (
"fmt"
"testing"
"unsafe"
"github.com/inkyblackness/imgui-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAllocatedGlyphRangesFreeSetsToZero(t *testing.T) {
var builder imgui.GlyphRangesBuilder
ranges := builder.Build()
require.NotEqual(t, uintptr(0), uintptr(ranges.GlyphRanges), "Should have allocated")
ranges.Free()
assert.Equal(t, uintptr(0), uintptr(ranges.GlyphRanges), "Should have reset")
}
func TestGlyphRangesBuilderAdd(t *testing.T) {
type singleRange struct {
from rune
to rune
}
tt := []struct {
name string
input []singleRange
expected []uint16
}{
{name: "No input should contain terminator only", input: nil, expected: []uint16{0}},
{name: "adding of single rune range", input: []singleRange{{from: 'A', to: 'A'}}, expected: []uint16{65, 65, 0}},
{name: "adding of larger range", input: []singleRange{{from: 'A', to: 'C'}}, expected: []uint16{65, 67, 0}},
{name: "adding two ranges", input: []singleRange{{from: 'A', to: 'C'}, {from: 'E', to: 'F'}}, expected: []uint16{65, 67, 69, 70, 0}},
{name: "wrong order is ignored", input: []singleRange{{from: 'B', to: 'A'}}, expected: []uint16{0}},
}
for _, tc := range tt {
var builder imgui.GlyphRangesBuilder
for _, add := range tc.input {
builder.Add(add.from, add.to)
}
result := builder.Build()
require.NotEqual(t, uintptr(0), uintptr(result.GlyphRanges))
for index, expected := range tc.expected {
resultPtr := unsafe.Pointer(uintptr(result.GlyphRanges) + uintptr(2*index)) // nolint: gosec
resultShort := (*uint16)(resultPtr)
assert.Equal(t, expected, *resultShort, fmt.Sprintf("%s: Index %d mismatch", tc.name, index))
}
result.Free()
}
}
func TestGlyphRangesBuilderAddExisting(t *testing.T) {
aRange := func(from, to rune) imgui.AllocatedGlyphRanges {
var builder imgui.GlyphRangesBuilder
builder.Add(from, to)
return builder.Build()
}
baseRangeAA := aRange('A', 'A')
defer baseRangeAA.Free()
baseRangeEF := aRange('E', 'F')
defer baseRangeEF.Free()
tt := []struct {
name string
input []imgui.GlyphRanges
expected []uint16
}{
{name: "adding of single range", input: []imgui.GlyphRanges{baseRangeAA.GlyphRanges}, expected: []uint16{65, 65, 0}},
{name: "adding of two single ranges", input: []imgui.GlyphRanges{baseRangeAA.GlyphRanges, baseRangeEF.GlyphRanges}, expected: []uint16{65, 65, 69, 70, 0}},
}
for _, tc := range tt {
var builder imgui.GlyphRangesBuilder
for _, add := range tc.input {
builder.AddExisting(add)
}
result := builder.Build()
require.NotEqual(t, uintptr(0), uintptr(result.GlyphRanges))
for index, expected := range tc.expected {
resultPtr := unsafe.Pointer(uintptr(result.GlyphRanges) + uintptr(2*index)) // nolint: gosec
resultShort := (*uint16)(resultPtr)
assert.Equal(t, expected, *resultShort, fmt.Sprintf("%s: Index %d mismatch", tc.name, index))
}
result.Free()
}
}
func TestGlyphRangesBuilderCompactsRanges(t *testing.T) {
type singleRange struct {
from rune
to rune
}
tt := []struct {
name string
input []singleRange
expected []uint16
}{
{name: "Removing duplications", input: []singleRange{{from: 'A', to: 'B'}, {from: 'A', to: 'B'}}, expected: []uint16{65, 66}},
{name: "combining ranges A", input: []singleRange{{from: 'A', to: 'D'}, {from: 'B', to: 'E'}}, expected: []uint16{65, 69, 0}},
{name: "combining ranges B", input: []singleRange{{from: 'C', to: 'E'}, {from: 'A', to: 'D'}}, expected: []uint16{65, 69, 0}},
{name: "combining ranges C", input: []singleRange{{from: 'A', to: 'E'}, {from: 'B', to: 'C'}}, expected: []uint16{65, 69, 0}},
{name: "combining ranges of whole set", input: []singleRange{
{from: 'A', to: 'B'}, {from: 'E', to: 'F'}, {from: 'A', to: 'C'}}, expected: []uint16{65, 67, 69, 70, 0}},
}
for _, tc := range tt {
var builder imgui.GlyphRangesBuilder
for _, add := range tc.input {
builder.Add(add.from, add.to)
}
result := builder.Build()
require.NotEqual(t, uintptr(0), uintptr(result.GlyphRanges))
for index, expected := range tc.expected {
resultPtr := unsafe.Pointer(uintptr(result.GlyphRanges) + uintptr(2*index)) // nolint: gosec
resultShort := (*uint16)(resultPtr)
assert.Equal(t, expected, *resultShort, fmt.Sprintf("%s: Index %d mismatch", tc.name, index))
}
result.Free()
}
}