pax_global_header00006660000000000000000000000064134431472640014522gustar00rootroot0000000000000052 comment=88acc9dede4a1fd2ed5fe2e2fc4e2faf7a0fee72 go.incremental-1.0.0/000077500000000000000000000000001344314726400144255ustar00rootroot00000000000000go.incremental-1.0.0/.gitignore000066400000000000000000000000321344314726400164100ustar00rootroot00000000000000/gen/gen /example/example go.incremental-1.0.0/LICENSE000066400000000000000000000024301344314726400154310ustar00rootroot00000000000000Copyright (c) 2013, Geert-Johan Riemer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. go.incremental-1.0.0/README.md000066400000000000000000000022721344314726400157070ustar00rootroot00000000000000## go.incremental [![Build Status](https://drone.io/github.com/GeertJohan/go.incremental/status.png)](https://drone.io/github.com/GeertJohan/go.incremental/latest) Go package incremental provides typed incremental counters that are type-safe. ### Install `go get github.com/GeertJohan/go.incremental` ### Usage example This example is also located in the example subdirectory ```go package main import ( "fmt" "github.com/GeertJohan/go.incremental" "runtime" ) func main() { // use max cpu's runtime.GOMAXPROCS(runtime.NumCPU()) // create new incremental.Int i := &incremental.Int{} // print some numbers fmt.Println(i.Next()) // print 1 fmt.Println(i.Next()) // print 2 fmt.Println(i.Next()) // print 3 // create chan to check if goroutines are done done := make(chan int) // spawn 4 goroutines for a := 0; a < 4; a++ { // call goroutine with it's number (0-3) go func(aa int) { // print 10 incremental numbers for b := 0; b < 10; b++ { fmt.Printf("routine %d: %d\n", aa, i.Next()) } // signal done done <- aa }(a) } // wait until all goroutines are done for a := 0; a < 4; a++ { fmt.Printf("goroutine %d done\n", <-done) } fmt.Println("all done") } ```go.incremental-1.0.0/doc.go000066400000000000000000000003621344314726400155220ustar00rootroot00000000000000 // package incremental provides concurency-safe incremental numbers. // // This package was created by a simple piece of code located in the gen subdirectory. Please modify that command if you want to modify this package. package incrementalgo.incremental-1.0.0/example/000077500000000000000000000000001344314726400160605ustar00rootroot00000000000000go.incremental-1.0.0/example/example.go000066400000000000000000000014671344314726400200520ustar00rootroot00000000000000package main import ( "fmt" "github.com/GeertJohan/go.incremental" "runtime" ) func main() { // use max cpu's runtime.GOMAXPROCS(runtime.NumCPU()) // create new incremental.Int i := &incremental.Int{} // print some numbers fmt.Println(i.Next()) // print 1 fmt.Println(i.Next()) // print 2 fmt.Println(i.Next()) // print 3 // create chan to check if goroutines are done done := make(chan int) // spawn 4 goroutines for a := 0; a < 4; a++ { // call goroutine with it's number (0-3) go func(aa int) { // print 10 incremental numbers for b := 0; b < 10; b++ { fmt.Printf("routine %d: %d\n", aa, i.Next()) } // signal done done <- aa }(a) } // wait until all goroutines are done for a := 0; a < 4; a++ { fmt.Printf("goroutine %d done\n", <-done) } fmt.Println("all done") } go.incremental-1.0.0/gen/000077500000000000000000000000001344314726400151765ustar00rootroot00000000000000go.incremental-1.0.0/gen/generator.go000066400000000000000000000067571344314726400175320ustar00rootroot00000000000000package main import ( "log" "os" "strings" "text/template" ) var ( tmpl *template.Template tmplTest *template.Template ) var types = []string{ "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", } func init() { var err error tmpl, err = template.New("tmpl").Parse(`package incremental import ( "sync" ) type {{.Upper}} struct { increment {{.Lower}} lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this {{.Upper}} func (i *{{.Upper}}) Next() {{.Lower}} { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number ({{.Lower}}) that was returned by the most recent call to this instance's Next() func (i *{{.Upper}}) Last() {{.Lower}} { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *{{.Upper}}) Set(value {{.Lower}}) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } `) tmplTest, err = template.New("tmplTest").Parse(`package incremental import ( "testing" ) type some{{.Upper}}Struct struct { i {{.Upper}} } func Test{{.Upper}}Ptr(t *testing.T) { i := &{{.Upper}}{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func Test{{.Upper}}AsField(t *testing.T) { s := some{{.Upper}}Struct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSome{{.Upper}}Struct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSome{{.Upper}}Struct(s *some{{.Upper}}Struct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } `) if err != nil { log.Fatal(err) } } type data struct { Upper string Lower string } func main() { // loop over integer types for _, t := range types { // create data with upper and lower names for the type d := &data{ Upper: strings.ToUpper(t[0:1]) + t[1:], Lower: t, } // create file for type file, err := os.Create(t + ".go") if err != nil { log.Fatal(err) } defer file.Close() // execute template, write directly to file err = tmpl.Execute(file, d) if err != nil { log.Fatal(err) } // create file for test file, err = os.Create(t + "_test.go") if err != nil { log.Fatal(err) } defer file.Close() // execute template, write directly to file err = tmplTest.Execute(file, d) if err != nil { log.Fatal(err) } } // create doc.go file, err := os.Create("doc.go") if err != nil { log.Fatal(err) } defer file.Close() file.WriteString(` // package incremental provides concurency-safe incremental numbers. // // This package was created by a simple piece of code located in the gen subdirectory. Please modify that command if you want to modify this package. package incremental`) } go.incremental-1.0.0/go.mod000066400000000000000000000000651344314726400155340ustar00rootroot00000000000000module github.com/GeertJohan/go.incremental go 1.12 go.incremental-1.0.0/int.go000066400000000000000000000012151344314726400155450ustar00rootroot00000000000000package incremental import ( "sync" ) type Int struct { increment int lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Int func (i *Int) Next() int { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (int) that was returned by the most recent call to this instance's Next() func (i *Int) Last() int { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Int) Set(value int) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/int16.go000066400000000000000000000012411344314726400157130ustar00rootroot00000000000000package incremental import ( "sync" ) type Int16 struct { increment int16 lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Int16 func (i *Int16) Next() int16 { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (int16) that was returned by the most recent call to this instance's Next() func (i *Int16) Last() int16 { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Int16) Set(value int16) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/int16_test.go000066400000000000000000000023601344314726400167550ustar00rootroot00000000000000package incremental import ( "testing" ) type someInt16Struct struct { i Int16 } func TestInt16Ptr(t *testing.T) { i := &Int16{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestInt16AsField(t *testing.T) { s := someInt16Struct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeInt16Struct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeInt16Struct(s *someInt16Struct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } go.incremental-1.0.0/int32.go000066400000000000000000000012411344314726400157110ustar00rootroot00000000000000package incremental import ( "sync" ) type Int32 struct { increment int32 lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Int32 func (i *Int32) Next() int32 { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (int32) that was returned by the most recent call to this instance's Next() func (i *Int32) Last() int32 { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Int32) Set(value int32) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/int32_test.go000066400000000000000000000023601344314726400167530ustar00rootroot00000000000000package incremental import ( "testing" ) type someInt32Struct struct { i Int32 } func TestInt32Ptr(t *testing.T) { i := &Int32{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestInt32AsField(t *testing.T) { s := someInt32Struct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeInt32Struct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeInt32Struct(s *someInt32Struct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } go.incremental-1.0.0/int64.go000066400000000000000000000012411344314726400157160ustar00rootroot00000000000000package incremental import ( "sync" ) type Int64 struct { increment int64 lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Int64 func (i *Int64) Next() int64 { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (int64) that was returned by the most recent call to this instance's Next() func (i *Int64) Last() int64 { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Int64) Set(value int64) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/int64_test.go000066400000000000000000000023601344314726400167600ustar00rootroot00000000000000package incremental import ( "testing" ) type someInt64Struct struct { i Int64 } func TestInt64Ptr(t *testing.T) { i := &Int64{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestInt64AsField(t *testing.T) { s := someInt64Struct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeInt64Struct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeInt64Struct(s *someInt64Struct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } go.incremental-1.0.0/int8.go000066400000000000000000000012271344314726400156400ustar00rootroot00000000000000package incremental import ( "sync" ) type Int8 struct { increment int8 lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Int8 func (i *Int8) Next() int8 { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (int8) that was returned by the most recent call to this instance's Next() func (i *Int8) Last() int8 { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Int8) Set(value int8) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/int8_test.go000066400000000000000000000023471344314726400167030ustar00rootroot00000000000000package incremental import ( "testing" ) type someInt8Struct struct { i Int8 } func TestInt8Ptr(t *testing.T) { i := &Int8{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestInt8AsField(t *testing.T) { s := someInt8Struct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeInt8Struct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeInt8Struct(s *someInt8Struct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } go.incremental-1.0.0/int_test.go000066400000000000000000000023361344314726400166110ustar00rootroot00000000000000package incremental import ( "testing" ) type someIntStruct struct { i Int } func TestIntPtr(t *testing.T) { i := &Int{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestIntAsField(t *testing.T) { s := someIntStruct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeIntStruct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeIntStruct(s *someIntStruct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } go.incremental-1.0.0/uint.go000066400000000000000000000012271344314726400157350ustar00rootroot00000000000000package incremental import ( "sync" ) type Uint struct { increment uint lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Uint func (i *Uint) Next() uint { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (uint) that was returned by the most recent call to this instance's Next() func (i *Uint) Last() uint { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Uint) Set(value uint) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/uint16.go000066400000000000000000000012531344314726400161030ustar00rootroot00000000000000package incremental import ( "sync" ) type Uint16 struct { increment uint16 lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Uint16 func (i *Uint16) Next() uint16 { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (uint16) that was returned by the most recent call to this instance's Next() func (i *Uint16) Last() uint16 { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Uint16) Set(value uint16) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/uint16_test.go000066400000000000000000000023711344314726400171440ustar00rootroot00000000000000package incremental import ( "testing" ) type someUint16Struct struct { i Uint16 } func TestUint16Ptr(t *testing.T) { i := &Uint16{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestUint16AsField(t *testing.T) { s := someUint16Struct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeUint16Struct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeUint16Struct(s *someUint16Struct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } go.incremental-1.0.0/uint32.go000066400000000000000000000012531344314726400161010ustar00rootroot00000000000000package incremental import ( "sync" ) type Uint32 struct { increment uint32 lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Uint32 func (i *Uint32) Next() uint32 { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (uint32) that was returned by the most recent call to this instance's Next() func (i *Uint32) Last() uint32 { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Uint32) Set(value uint32) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/uint32_test.go000066400000000000000000000023711344314726400171420ustar00rootroot00000000000000package incremental import ( "testing" ) type someUint32Struct struct { i Uint32 } func TestUint32Ptr(t *testing.T) { i := &Uint32{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestUint32AsField(t *testing.T) { s := someUint32Struct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeUint32Struct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeUint32Struct(s *someUint32Struct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } go.incremental-1.0.0/uint64.go000066400000000000000000000012531344314726400161060ustar00rootroot00000000000000package incremental import ( "sync" ) type Uint64 struct { increment uint64 lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Uint64 func (i *Uint64) Next() uint64 { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (uint64) that was returned by the most recent call to this instance's Next() func (i *Uint64) Last() uint64 { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Uint64) Set(value uint64) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/uint64_test.go000066400000000000000000000023711344314726400171470ustar00rootroot00000000000000package incremental import ( "testing" ) type someUint64Struct struct { i Uint64 } func TestUint64Ptr(t *testing.T) { i := &Uint64{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestUint64AsField(t *testing.T) { s := someUint64Struct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeUint64Struct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeUint64Struct(s *someUint64Struct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } go.incremental-1.0.0/uint8.go000066400000000000000000000012411344314726400160210ustar00rootroot00000000000000package incremental import ( "sync" ) type Uint8 struct { increment uint8 lock sync.Mutex } // Next returns with an integer that is exactly one higher as the previous call to Next() for this Uint8 func (i *Uint8) Next() uint8 { i.lock.Lock() defer i.lock.Unlock() i.increment++ return i.increment } // Last returns the number (uint8) that was returned by the most recent call to this instance's Next() func (i *Uint8) Last() uint8 { return i.increment } // Set changes the increment to given value, the succeeding call to Next() will return the given value+1 func (i *Uint8) Set(value uint8) { i.lock.Lock() defer i.lock.Unlock() i.increment = value } go.incremental-1.0.0/uint8_test.go000066400000000000000000000023601344314726400170630ustar00rootroot00000000000000package incremental import ( "testing" ) type someUint8Struct struct { i Uint8 } func TestUint8Ptr(t *testing.T) { i := &Uint8{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestUint8AsField(t *testing.T) { s := someUint8Struct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeUint8Struct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeUint8Struct(s *someUint8Struct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } } go.incremental-1.0.0/uint_test.go000066400000000000000000000023471344314726400170000ustar00rootroot00000000000000package incremental import ( "testing" ) type someUintStruct struct { i Uint } func TestUintPtr(t *testing.T) { i := &Uint{} num := i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } i.Set(42) num = i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func TestUintAsField(t *testing.T) { s := someUintStruct{} num := s.i.Next() if num != 1 { t.Fatalf("expected 1, got %d", num) } num = s.i.Next() if num != 2 { t.Fatalf("expected 2, got %d", num) } num = s.i.Last() if num != 2 { t.Fatalf("expected last to be 2, got %d", num) } useSomeUintStruct(&s, t) num = s.i.Last() if num != 3 { t.Fatalf("expected last to be 3, got %d", num) } s.i.Set(42) num = s.i.Last() if num != 42 { t.Fatalf("expected last to be 42, got %d", num) } num = s.i.Next() if num != 43 { t.Fatalf("expected 43, got %d", num) } } func useSomeUintStruct(s *someUintStruct, t *testing.T) { num := s.i.Next() if num != 3 { t.Fatalf("expected 3, got %d", num) } }