Skip to content

Commit

Permalink
Merge pull request #8 from kuoss/7-one-based-no-zerofill
Browse files Browse the repository at this point in the history
7 one based no zerofill
  • Loading branch information
jmnote authored Sep 16, 2024
2 parents 99bf183 + 5d85e55 commit 87ff414
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 20 deletions.
37 changes: 36 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
BINARY_NAME=log-loop

LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.61.0


.PHONY: build
build:
go build -o bin/$(BINARY_NAME) .
Expand All @@ -8,7 +16,34 @@ build:
run: build
bin/$(BINARY_NAME)

.PHONY: test
test:
go test --failfast ./...

.PHONY: lint
lint: golangci-lint
$(GOLANGCI_LINT) run

.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))

.PHONY: docker
docker:
docker build -t log-loop .
docker run log-loop

.PHONY: checks
checks: test lint docker

define go-install-tool
@[ -f "$(1)-$(3)" ] || { \
set -e; \
package=$(2)@$(3) ;\
echo "Downloading $${package}" ;\
rm -f $(1) || true ;\
GOBIN=$(LOCALBIN) go install $${package} ;\
mv $(1) $(1)-$(3) ;\
} ;\
ln -sf $(1)-$(3) $(1)
endef
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ docker run --rm -e LOOP_COUNTS="1000,100" ghcr.io/kuoss/log-loop:latest

### Example Output
```
a=000 b=00 uuid=f6d8cd6d-ff19-4380-920e-6f13bfce1e73
a=000 b=01 uuid=2262be90-3974-4f7f-8b86-896a9eda9d7b
a=000 b=02 uuid=9b06a1cf-97a3-4f07-bc8c-637550b187fe
a=1 b=1 uuid=f6d8cd6d-ff19-4380-920e-6f13bfce1e73
a=1 b=2 uuid=2262be90-3974-4f7f-8b86-896a9eda9d7b
a=1 b=3 uuid=9b06a1cf-97a3-4f07-bc8c-637550b187fe
...
a=999 b=97 uuid=bf887df1-83cf-442a-a1ee-de4067709af3
a=999 b=98 uuid=88044fe4-1638-471d-b00e-389cc0ac3a89
a=999 b=99 uuid=b6401365-7e81-42a5-9ff0-4347efaf7dfe
a=1000 b=98 uuid=bf887df1-83cf-442a-a1ee-de4067709af3
a=1000 b=99 uuid=88044fe4-1638-471d-b00e-389cc0ac3a89
a=1000 b=100 uuid=b6401365-7e81-42a5-9ff0-4347efaf7dfe
```
13 changes: 11 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module github.com/kuoss/log-routine
module github.com/kuoss/log-loop

go 1.22.4

require github.com/google/uuid v1.6.0
require (
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 3 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func getLoopCountsFromEnv() []int {
defaultCounts := []int{1000, 1000}
envValue := os.Getenv("LOOP_COUNTS")
if envValue == "" {
fmt.Println("LOOP_COUNTS not set, using default:", defaultCounts)
return defaultCounts
}

Expand All @@ -22,7 +21,6 @@ func getLoopCountsFromEnv() []int {
for i, countStr := range countStrings {
count, err := strconv.Atoi(countStr)
if err != nil || count <= 0 {
fmt.Printf("Invalid LOOP_COUNTS value at index %d: %s, using default: %v\n", i, countStr, defaultCounts)
return defaultCounts
}
loopCounts[i] = count
Expand All @@ -32,22 +30,16 @@ func getLoopCountsFromEnv() []int {

func getDepthPrefix(depth int) string {
if depth >= 0 && depth < 26 {
return string('a' + depth)
return string(rune('a' + depth))
}
return "?"
}

func getZeroPaddedFormat(loopSize int) string {
digits := len(strconv.Itoa(loopSize - 1))
return "%0" + strconv.Itoa(digits) + "d"
}

func generateLoops(loopCounts []int, depth int, currentIndices []string) {
zeroPaddedFormat := getZeroPaddedFormat(loopCounts[depth])
prefix := getDepthPrefix(depth)

for i := 0; i < loopCounts[depth]; i++ {
formattedIndex := fmt.Sprintf("%s=%s", prefix, fmt.Sprintf(zeroPaddedFormat, i))
for i := 1; i <= loopCounts[depth]; i++ {
formattedIndex := fmt.Sprintf("%s=%d", prefix, i)
newIndices := append(currentIndices, formattedIndex)

if depth == len(loopCounts)-1 {
Expand Down
75 changes: 75 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"bytes"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetLoopCountsFromEnv(t *testing.T) {
testCases := []struct {
envValue string
expected []int
}{
{"", []int{1000, 1000}},
{"3,5,7", []int{3, 5, 7}},
{"3,invalid,7", []int{1000, 1000}},
}

for _, tc := range testCases {
t.Setenv("LOOP_COUNTS", tc.envValue)
loopCounts := getLoopCountsFromEnv()
assert.Equal(t, tc.expected, loopCounts)
}
}

// Test for getDepthPrefix
func TestGetDepthPrefix(t *testing.T) {
testCases := []struct {
depth int
expected string
}{
{0, "a"},
{1, "b"},
{25, "z"},
{26, "?"},
{-1, "?"},
}

for _, tc := range testCases {
result := getDepthPrefix(tc.depth)
assert.Equal(t, tc.expected, result)
}
}

func TestGenerateLoops(t *testing.T) {
origStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// Execute the function that prints output
loopCounts := []int{2, 2}
generateLoops(loopCounts, 0, []string{})

// Close the writer and read the output
w.Close()
var outputBuf bytes.Buffer
n, err := outputBuf.ReadFrom(r)
assert.Equal(t, n, int64(200))
assert.NoError(t, err)

// Restore the original stdout
os.Stdout = origStdout

// Get the actual output as string
output := outputBuf.String()

expectedLines := 4
actualLines := len(strings.Split(strings.TrimSpace(output), "\n"))

assert.Equal(t, expectedLines, actualLines)
assert.Contains(t, output, "uuid=")
}

0 comments on commit 87ff414

Please sign in to comment.