• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark / 13642217050

03 Mar 2025 11:02PM CUT coverage: 99.261% (+0.4%) from 98.883%
13642217050

push

github

web-flow
Add more unit tests (#119)

* test entity pool capacity
* test relations panic
* test table match panics
* test register on locked world
* test cache panic
* test storage panics
* test exchange mask panics

3 of 3 new or added lines in 1 file covered. (100.0%)

5772 of 5815 relevant lines covered (99.26%)

34225.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.52
/ecs/archetype.go
1
package ecs
2

3
import (
4
        "math"
5
        "slices"
6
)
7

8
type archetypeID uint32
9

10
// maxArchetypeID is used as unassigned archetype ID.
11
const maxArchetypeID = math.MaxUint32
12

13
type archetype struct {
14
        id             archetypeID
15
        node           nodeID
16
        mask           Mask
17
        components     []ID                     // components IDs of the archetype in arbitrary order
18
        componentsMap  []int16                  // mapping from component IDs to column indices; -1 indicates none
19
        isRelation     []bool                   // whether columns are relations components, indexed by column index
20
        relationTables []map[entityID]*tableIDs // lookup for relation targets of tables, indexed by column index
21
        tables         []tableID                // all active tables
22
        freeTables     []tableID                // all inactive/free tables
23
        numRelations   uint8                    // number of relation components
24
}
25

26
type tableIDs struct {
27
        tables []tableID
28
}
29

30
func newArchetype(id archetypeID, node nodeID, mask *Mask, components []ID, tables []tableID, reg *componentRegistry) archetype {
585✔
31
        componentsMap := make([]int16, MaskTotalBits)
585✔
32
        for i := range MaskTotalBits {
150,345✔
33
                componentsMap[i] = -1
149,760✔
34
        }
149,760✔
35
        for i, id := range components {
2,005✔
36
                componentsMap[id.id] = int16(i)
1,420✔
37
        }
1,420✔
38

39
        numRelations := uint8(0)
585✔
40
        isRelation := make([]bool, len(components))
585✔
41
        relationTables := make([]map[entityID]*tableIDs, len(components))
585✔
42
        for i, id := range components {
2,005✔
43
                if reg.IsRelation[id.id] {
1,471✔
44
                        isRelation[i] = true
51✔
45
                        relationTables[i] = map[entityID]*tableIDs{}
51✔
46
                        numRelations++
51✔
47
                }
51✔
48
        }
49
        return archetype{
585✔
50
                id:             id,
585✔
51
                node:           node,
585✔
52
                mask:           *mask,
585✔
53
                components:     components,
585✔
54
                componentsMap:  componentsMap,
585✔
55
                isRelation:     isRelation,
585✔
56
                tables:         tables,
585✔
57
                numRelations:   numRelations,
585✔
58
                relationTables: relationTables,
585✔
59
        }
585✔
60
}
61

62
func (a *archetype) HasRelations() bool {
499,588✔
63
        return a.numRelations > 0
499,588✔
64
}
499,588✔
65

66
func (a *archetype) GetTable(storage *storage, relations []RelationID) (*table, bool) {
497,047✔
67
        if len(a.tables) == 0 {
497,439✔
68
                return nil, false
392✔
69
        }
392✔
70
        if !a.HasRelations() {
992,649✔
71
                return &storage.tables[a.tables[0]], true
495,994✔
72
        }
495,994✔
73

74
        index := a.componentsMap[relations[0].component.id]
661✔
75
        tables, ok := a.relationTables[index][relations[0].target.id]
661✔
76
        if !ok {
735✔
77
                return nil, false
74✔
78
        }
74✔
79
        for _, t := range tables.tables {
1,193✔
80
                table := &storage.tables[t]
606✔
81
                if table.MatchesExact(relations) {
1,191✔
82
                        return table, true
585✔
83
                }
585✔
84
        }
85
        return nil, false
2✔
86
}
87

88
func (a *archetype) GetTables(relations []RelationID) []tableID {
90✔
89
        if !a.HasRelations() {
90✔
90
                return a.tables
×
91
        }
×
92
        if len(relations) == 0 {
124✔
93
                return a.tables
34✔
94
        }
34✔
95
        index := a.componentsMap[relations[0].component.id]
56✔
96
        if tables, ok := a.relationTables[index][relations[0].target.id]; ok {
112✔
97
                return tables.tables
56✔
98
        }
56✔
99
        return nil
×
100
}
101

102
func (a *archetype) GetFreeTable() (tableID, bool) {
468✔
103
        if len(a.freeTables) == 0 {
934✔
104
                return 0, false
466✔
105
        }
466✔
106
        last := len(a.freeTables) - 1
2✔
107
        table := a.freeTables[last]
2✔
108

2✔
109
        a.freeTables = a.freeTables[:last]
2✔
110

2✔
111
        return table, true
2✔
112
}
113

114
func (a *archetype) FreeTable(table tableID) {
10✔
115
        // TODO: can we speed this up for large numbers of relation targets?
10✔
116
        index := slices.Index(a.tables, table)
10✔
117
        last := len(a.tables) - 1
10✔
118

10✔
119
        if index != last {
14✔
120
                a.tables[index], a.tables[last] = a.tables[last], a.tables[index]
4✔
121
        }
4✔
122
        a.tables = a.tables[:last]
10✔
123

10✔
124
        a.freeTables = append(a.freeTables, table)
10✔
125
}
126

127
func (a *archetype) AddTable(table *table) {
468✔
128
        a.tables = append(a.tables, table.id)
468✔
129
        if !a.HasRelations() {
812✔
130
                return
344✔
131
        }
344✔
132

133
        for i := range table.ids {
593✔
134
                column := &table.columns[i]
469✔
135
                if !column.isRelation {
806✔
136
                        continue
337✔
137
                }
138
                target := column.target
132✔
139
                relations := a.relationTables[i]
132✔
140

132✔
141
                if tables, ok := relations[target.id]; ok {
137✔
142
                        tables.tables = append(tables.tables, table.id)
5✔
143
                } else {
132✔
144
                        relations[target.id] = &tableIDs{tables: []tableID{table.id}}
127✔
145
                }
127✔
146
        }
147
}
148

149
func (a *archetype) RemoveTarget(entity Entity) {
13✔
150
        for i := range a.relationTables {
31✔
151
                if !a.isRelation[i] {
23✔
152
                        continue
5✔
153
                }
154
                delete(a.relationTables[i], entity.id)
13✔
155
        }
156
}
157

158
func (a *archetype) Reset(storage *storage) {
4✔
159
        if !a.HasRelations() {
7✔
160
                storage.tables[a.tables[0]].Reset()
3✔
161
                return
3✔
162
        }
3✔
163

164
        for _, tab := range a.tables {
2✔
165
                table := &storage.tables[tab]
1✔
166
                table.Reset()
1✔
167
        }
1✔
168

169
        for i := len(a.tables) - 1; i >= 0; i-- {
2✔
170
                storage.cache.removeTable(storage, &storage.tables[a.tables[i]])
1✔
171
                a.FreeTable(a.tables[i])
1✔
172
        }
1✔
173

174
        for _, m := range a.relationTables {
3✔
175
                for key := range m {
3✔
176
                        delete(m, key)
1✔
177
                }
1✔
178
        }
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc