From defca0fc451b59b945c9325900ea06902aefe293 Mon Sep 17 00:00:00 2001 From: Naveen Mahalingam Date: Mon, 22 Apr 2024 13:46:09 -0700 Subject: [PATCH] minor fixes --- passphrase/default_generator_test.go | 2 +- passphrase/generator.go | 2 ++ passphrase/generator_bench_test.go | 17 +++++++++++++++ passphrase/generator_test.go | 32 +++++++++------------------- password/generator_bench_test.go | 17 +++++++++++++++ password/generator_test.go | 13 ----------- 6 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 passphrase/generator_bench_test.go create mode 100644 password/generator_bench_test.go diff --git a/passphrase/default_generator_test.go b/passphrase/default_generator_test.go index ec17977..ab842ea 100644 --- a/passphrase/default_generator_test.go +++ b/passphrase/default_generator_test.go @@ -10,5 +10,5 @@ func TestGenerate(t *testing.T) { assert.NotEmpty(t, Generate()) SetSeed(1) - assert.Equal(t, "Holisms-Shark6-Elating", Generate()) + assert.Equal(t, "Holism-Sharing6-Elates", Generate()) } diff --git a/passphrase/generator.go b/passphrase/generator.go index e83d106..9aa51f8 100644 --- a/passphrase/generator.go +++ b/passphrase/generator.go @@ -80,6 +80,8 @@ func (g *generator) sanitize() (Generator, error) { slices.DeleteFunc(g.dictionary, func(word string) bool { return len(word) < g.wordLenMin || len(word) > g.wordLenMax }) + slices.Sort(g.dictionary) + slices.Compact(g.dictionary) if len(g.dictionary) < MinWordsInDictionary { return nil, ErrDictionaryTooSmall } diff --git a/passphrase/generator_bench_test.go b/passphrase/generator_bench_test.go new file mode 100644 index 0000000..2088880 --- /dev/null +++ b/passphrase/generator_bench_test.go @@ -0,0 +1,17 @@ +package passphrase + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func BenchmarkGenerator_Generate(b *testing.B) { + g, err := NewGenerator() + assert.Nil(b, err) + assert.NotEmpty(b, g.Generate()) + + for idx := 0; idx < b.N; idx++ { + _ = g.Generate() + } +} diff --git a/passphrase/generator_test.go b/passphrase/generator_test.go index d3daaaf..6ed9206 100644 --- a/passphrase/generator_test.go +++ b/passphrase/generator_test.go @@ -9,16 +9,6 @@ import ( "github.com/stretchr/testify/assert" ) -func BenchmarkGenerator_Generate(b *testing.B) { - g, err := NewGenerator() - assert.Nil(b, err) - assert.NotEmpty(b, g.Generate()) - - for idx := 0; idx < b.N; idx++ { - _ = g.Generate() - } -} - func TestGenerator_Generate(t *testing.T) { g, err := NewGenerator( WithCapitalizedWords(true), @@ -33,18 +23,16 @@ func TestGenerator_Generate(t *testing.T) { g.SetSeed(1) expectedPassphrases := []string{ - "Sans-Liber-Quale1", - "Defogs-Tael0-Hallo", - "Medium-Leader-Sesame2", - "Chelae-Tocsin8-Haling", - "Taxies1-Sordor-Banner", - "Kwanza-Molies-Lapses5", - "Scurf-Hookas-Beryl4", - "Repine-Dele-Loans3", - "Furore0-Geneva-Celts", - "Strew7-Tweed-Sannop", - "Quasi7-Vino-Optic", - "Alible8-Sherds-Fraena", + "Sannup-Libels-Quaky1", + "Defog-Tads0-Hallel", + "Medina-Leaden-Servos2", + "Chela-Tocher8-Halids", + "Taxied1-Sordid-Banned", + "Kwacha-Molest-Lapser5", + "Scups-Hookah-Berths4", + "Repin-Delays-Loaner3", + "Furor0-Genets-Celt", + "Stress7-Twee-Sank", } var actualPhrases []string for idx := 0; idx < 1000; idx++ { diff --git a/password/generator_bench_test.go b/password/generator_bench_test.go new file mode 100644 index 0000000..a884171 --- /dev/null +++ b/password/generator_bench_test.go @@ -0,0 +1,17 @@ +package password + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func BenchmarkGenerator_Generate(b *testing.B) { + g, err := NewGenerator() + assert.Nil(b, err) + assert.NotEmpty(b, g.Generate()) + + for idx := 0; idx < b.N; idx++ { + _ = g.Generate() + } +} diff --git a/password/generator_test.go b/password/generator_test.go index b21ce3e..5de1e17 100644 --- a/password/generator_test.go +++ b/password/generator_test.go @@ -10,19 +10,6 @@ import ( "github.com/stretchr/testify/assert" ) -func BenchmarkGenerator_Generate(b *testing.B) { - g, err := NewGenerator( - WithCharset(charset.AlphaNumeric.WithoutAmbiguity().WithoutDuplicates()), - WithLength(12), - ) - assert.Nil(b, err) - assert.NotEmpty(b, g.Generate()) - - for idx := 0; idx < b.N; idx++ { - _ = g.Generate() - } -} - func TestGenerator_Generate(t *testing.T) { g, err := NewGenerator( WithCharset(charset.AlphaNumeric.WithoutAmbiguity().WithoutDuplicates()),