Browse Source

initial commit

master
Pixdigit 3 years ago
parent
commit
37ba465b19
  1. 10
      BaseIDSpace.go
  2. 18
      IDs.go
  3. 16
      IDs_test.go

10
BaseIDSpace.go

@ -0,0 +1,10 @@
package uniqueID
var baseSpace IDSpace
func NewID() ID {
return baseSpace.NewID()
}
func IDExists(id ID) bool {
return baseSpace.IDExists(id)
}

18
IDs.go

@ -0,0 +1,18 @@
package uniqueID
type ID int
type IDSpace struct {
freeID ID
}
func (s *IDSpace) NewID() ID {
newID := s.freeID
s.freeID++
return newID
}
func (s *IDSpace) IDExists(id ID) bool {
//All IDs below freeID are taken and therefore exist
return s.freeID > id
}

16
IDs_test.go

@ -0,0 +1,16 @@
package uniqueID
import (
"testing"
tools "gitlab.com/Pixdigit/goTestTools"
)
func TestBaseIDSpace(t *testing.T) {
ID1 := NewID()
ID2 := NewID()
ID3 := NewID()
tools.Test(ID1 != ID2 && ID2 != ID3 && ID3 != ID1, "duplicate IDS", t)
tools.Test(IDExists(ID3), "did not confirm existance of ID", t)
tools.Test(!IDExists(ID(5)), "wrongly confirmed existance of ID", t)
}
Loading…
Cancel
Save