Skip to content

Commit

Permalink
Fix not initialized rand for sequential container numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclmr committed Sep 1, 2023
1 parent c2dfd8b commit acf19f6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 58 deletions.
16 changes: 6 additions & 10 deletions cmd/icm/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ icm generate --count 1000000 | icm validate`,
RunE: func(cmd *cobra.Command, args []string) error {
config.Overwrite(cmd.Flags())

builder := cont.NewUniqueGeneratorBuilder().
builder := cont.NewUniqueGeneratorBuilder(r).
Count(count).
ExcludeCheckDigit10(excludeCheckDigit10).
ExcludeTranspositionErr(excludeTranspositionErr)
Expand All @@ -111,16 +111,12 @@ icm generate --count 1000000 | icm validate`,
builder.OwnerCodes(ownerDecoder.GetAllOwnerCodes())
}

if cmd.Flags().Changed("start") || cmd.Flags().Changed("end") {
if cmd.Flags().Changed("start") {
builder.Start(startValue.value)
}
if cmd.Flags().Changed("start") {
builder.Start(startValue.value)
}

if cmd.Flags().Changed("end") {
builder.End(endValue.value)
}
} else {
builder.Rand(r)
if cmd.Flags().Changed("end") {
builder.End(endValue.value)
}

generator, err := builder.Build()
Expand Down
16 changes: 8 additions & 8 deletions cmd/icm/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ RAN U 489155 0
},
},
false,
`RAN U 000000 9
RAN U 000001 4
`RAN U 000001 4
NAR U 000002 0
RAN U 000003 5
`,
},
Expand All @@ -93,9 +93,9 @@ RAN U 000003 5
},
},
false,
`RAN U 000000 9
`NAR U 000000 0
RAN U 000001 4
RAN U 000002 0
NAR U 000002 0
`,
},
{
Expand All @@ -112,9 +112,9 @@ RAN U 000002 0
},
},
false,
`RAN U 999998 0
`NAR U 999998 1
RAN U 999999 6
RAN U 000000 9
NAR U 000000 0
`,
},
{
Expand All @@ -131,9 +131,9 @@ RAN U 000000 9
},
},
false,
`RAN U 000000 9
`NAR U 000000 0
RAN U 000001 4
RAN U 000002 0
NAR U 000002 0
`,
},
{
Expand Down
2 changes: 1 addition & 1 deletion cmd/icm/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (dummyOwnerDecoder) Decode(code string) (bool, cont.Owner) {
type dummyOwnerUpdater struct{}

func (dummyOwnerUpdater) GetAllOwnerCodes() []string {
return []string{"RAN"}
return []string{"NAR", "RAN"}
}

func (dummyOwnerUpdater) Update([]cont.Owner) error {
Expand Down
23 changes: 9 additions & 14 deletions cont/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ type GeneratorBuilder struct {
// NewUniqueGeneratorBuilder returns a new random unique container number generator.
// If possible maximum unique container numbers are exceeded, count is less than 1 or
// no owner codes are passed then nil and error is returned.
func NewUniqueGeneratorBuilder() *GeneratorBuilder {
func NewUniqueGeneratorBuilder(rand *rand.Rand) *GeneratorBuilder {
return &GeneratorBuilder{
rand: rand,
count: 1,
start: -1,
end: -1,
Expand Down Expand Up @@ -54,12 +55,6 @@ func (gb *GeneratorBuilder) End(end int) *GeneratorBuilder {
return gb
}

// Rand sets random number generator. This is needed if Start and End are not set.
func (gb *GeneratorBuilder) Rand(rand *rand.Rand) *GeneratorBuilder {
gb.rand = rand
return gb
}

// ExcludeCheckDigit10 sets the exclusion of container numbers with check digit 10.
func (gb *GeneratorBuilder) ExcludeCheckDigit10(exclude bool) *GeneratorBuilder {
gb.exclCheckDigit10 = exclude
Expand Down Expand Up @@ -94,7 +89,10 @@ func (gb *GeneratorBuilder) Build() (*UniqueGenerator, error) {
var serialNumIt serialNumIt
var count int

if gb.start > -1 && gb.end > -1 {
startIsSet := gb.start > -1
endIsSet := gb.end > -1

if startIsSet && endIsSet {
serialNumIt = newSeqSerialNumIt(gb.start)
if gb.start <= gb.end {
count = gb.end + 1 - gb.start
Expand All @@ -103,20 +101,17 @@ func (gb *GeneratorBuilder) Build() (*UniqueGenerator, error) {
}
}

if gb.start > -1 && gb.end == -1 {
if startIsSet && !endIsSet {
serialNumIt = newSeqSerialNumIt(gb.start)
count = gb.count
}

if gb.end > -1 && gb.start == -1 {
if !startIsSet && endIsSet {
serialNumIt = newSeqSerialNumIt(gb.end + 1 - gb.count)
count = gb.count
}

if gb.start == -1 && gb.end == -1 {
if gb.rand == nil {
return nil, errors.New("random number generator not set")
}
if !startIsSet && !endIsSet {
if gb.count < 1 {
return nil, fmt.Errorf("count %d is lower than minimum count 1", gb.count)
}
Expand Down
35 changes: 10 additions & 25 deletions cont/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

func TestGeneratorBuilder(t *testing.T) {
type fields struct {
rand *rand.Rand
codes []string
count int
rangeStart int
Expand All @@ -25,7 +24,6 @@ func TestGeneratorBuilder(t *testing.T) {
{
"Build unique container number generator with random serial iterator",
fields{
rand.New(rand.NewSource(1)),
[]string{"ABC"},
2,
-1,
Expand Down Expand Up @@ -91,17 +89,6 @@ func TestGeneratorBuilder(t *testing.T) {
},
false,
},
{
"Build returns error for no random number generator",
fields{
codes: []string{"ABC"},
count: 1,
rangeStart: -1,
rangeEnd: -1,
},
nil,
true,
},
{
"Build returns error for no owner codes",
fields{
Expand All @@ -125,8 +112,8 @@ func TestGeneratorBuilder(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gb := NewUniqueGeneratorBuilder().
Rand(tt.fields.rand).
r := rand.New(rand.NewSource(1))
gb := NewUniqueGeneratorBuilder(r).
OwnerCodes(tt.fields.codes).
Count(tt.fields.count).
Start(tt.fields.rangeStart).
Expand Down Expand Up @@ -155,16 +142,15 @@ func TestUniqueGenerator(t *testing.T) {
}{
{
"Generate 3 unique container numbers with random serial numbers",
NewUniqueGeneratorBuilder().
Rand(r).
NewUniqueGeneratorBuilder(r).
OwnerCodes([]string{"ABC"}).
Count(3),
false,
3,
},
{
"Generate 1 container number",
NewUniqueGeneratorBuilder().
NewUniqueGeneratorBuilder(r).
OwnerCodes([]string{"ABC"}).
Count(3).
Start(1).
Expand All @@ -174,7 +160,7 @@ func TestUniqueGenerator(t *testing.T) {
},
{
"Generate 3 unique container numbers with sequential serial numbers and start 1",
NewUniqueGeneratorBuilder().
NewUniqueGeneratorBuilder(r).
OwnerCodes([]string{"ABC"}).
Count(3).
Start(1),
Expand All @@ -183,7 +169,7 @@ func TestUniqueGenerator(t *testing.T) {
},
{
"Generate 4 unique container numbers with sequential serial numbers and end 2",
NewUniqueGeneratorBuilder().
NewUniqueGeneratorBuilder(r).
OwnerCodes([]string{"ABC"}).
Count(4).
End(2),
Expand All @@ -192,7 +178,7 @@ func TestUniqueGenerator(t *testing.T) {
},
{
"Generate 5 unique container numbers with sequential serial numbers and start 1 and end 5",
NewUniqueGeneratorBuilder().
NewUniqueGeneratorBuilder(r).
OwnerCodes([]string{"ABC"}).
Start(1).
End(5),
Expand All @@ -201,7 +187,7 @@ func TestUniqueGenerator(t *testing.T) {
},
{
"Generate 6 unique container numbers with sequential serial numbers and start 999997 and end 2",
NewUniqueGeneratorBuilder().
NewUniqueGeneratorBuilder(r).
OwnerCodes([]string{"ABC"}).
Start(999997).
End(2),
Expand All @@ -210,7 +196,7 @@ func TestUniqueGenerator(t *testing.T) {
},
{
"Generate 1 unique container numbers with sequential serial numbers and exclude possible transposition errors",
NewUniqueGeneratorBuilder().
NewUniqueGeneratorBuilder(r).
OwnerCodes([]string{"ABC"}).
Start(801743).
Count(1).
Expand All @@ -220,8 +206,7 @@ func TestUniqueGenerator(t *testing.T) {
},
{
"Generate 2000001 unique container numbers with random serial numbers",
NewUniqueGeneratorBuilder().
Rand(r).
NewUniqueGeneratorBuilder(r).
OwnerCodes([]string{"AAA", "AAB", "ABB"}).
Count(2000001),

Expand Down

0 comments on commit acf19f6

Please sign in to comment.