Skip to content

Commit

Permalink
all id test ok
Browse files Browse the repository at this point in the history
  • Loading branch information
rokath committed Oct 13, 2024
1 parent 7476019 commit d4753f5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/id/cachedInsert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func Test_31_11111_insert_on_valid_cCache_valid_iCache_inserted_edited_file(t *t

// Legacy tests

func InsertOnInsertedFileWithValidCCache(t *testing.T) {
func TestInsertOnInsertedFileWithValidCCache(t *testing.T) {
defer Setup(t)() // This executes Setup(t) and puts the returned function into the defer list.
assertFileCreate(t, FSys, SFName, `trice(iD(999), "msg:value=%d\n", -1);`) // create src file
sFNameMTime := mTime(t, FSys, SFName) // keep mtime
Expand Down
4 changes: 2 additions & 2 deletions internal/id/cleanIDs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestClean(t *testing.T) {
assert.Nil(t, FSys.WriteFile(sFn, []byte(src), 0777))

// action
assert.Nil(t, args.Handler(W, FSys, []string{"trice", "clean", "-til", FnJSON, "-li", LIFnJSON}))
assert.Nil(t, args.Handler(W, FSys, []string{"trice", "clean", "-src", sFn, "-til", FnJSON, "-li", LIFnJSON}))

// check modified src file
expSrc := `break; case __LINE__: trice("msg:value=%d\n", -1 );`
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestCleanWithLIExtension(t *testing.T) {
assert.Nil(t, FSys.WriteFile(sFn, []byte(src), 0777))

// action
assert.Nil(t, args.Handler(W, FSys, []string{"trice", "clean", "-til", FnJSON, "-li", LIFnJSON}))
assert.Nil(t, args.Handler(W, FSys, []string{"trice", "clean", "-src", sFn, "-til", FnJSON, "-li", LIFnJSON}))

// check modified src file
expSrc := `break; case __LINE__: trice("msg:value=%d\n", -1 );`
Expand Down
7 changes: 6 additions & 1 deletion internal/id/insertIDs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ func TestInsertIDsForNewTrice2WithoutLI(t *testing.T) {
assert.Nil(t, e)
assert.Equal(t, expTil0, string(actTil0))

// 2nd action
IDData.IDSpace = IDData.IDSpace[:0] // reset ID space
assert.Nil(t, args.Handler(W, FSys, []string{"TRICE", "insert", "-src", fn0, "-src", fn1, "-v", "-IDMin=10", "-IDMax=19", "-IDMethod=downward", "-til", FnJSON, "-li", LIFnJSON}))

// Do noit apply "-src=." here! It would set Srcs to be {".", "."} what results in a double parsing of the root folder causing the test to fail.
Expand Down Expand Up @@ -684,6 +686,8 @@ func TestInsertIDsForNewTrice2WithoutLIAndTickInComment(t *testing.T) {
assert.Nil(t, e)
assert.Equal(t, expTil0, string(actTil0))

// 2nd action
IDData.IDSpace = IDData.IDSpace[:0] // reset ID space
assert.Nil(t, args.Handler(W, FSys, []string{"TRICE", "insert", "-src", fn0, "-src", fn1, "-v", "-IDMin=10", "-IDMax=19", "-IDMethod=downward", "-til", FnJSON, "-li", LIFnJSON}))

// Do noit apply "-src=." here! It would set Srcs to be {".", "."} what results in a double parsing of the root folder causing the test to fail.
Expand Down Expand Up @@ -862,7 +866,8 @@ func TestInsertIDsForNewTrice2(t *testing.T) {
}
assert.True(t, result)

// action 1
// action 1 (2nd action)
IDData.IDSpace = IDData.IDSpace[:0] // reset ID space
e = args.Handler(os.Stdout, FSys, []string{"trice", "insert", "-src", fn0, "-src", fn1, "-IDMin=1200", "-IDMax=1299", "-IDMethod=upward", "-til", FnJSON, "-li", LIFnJSON})
assert.Nil(t, e)

Expand Down
4 changes: 4 additions & 0 deletions internal/id/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package id

import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -37,6 +38,7 @@ func init() {
func Setup(t *testing.T) func() {
// Setup code here ///////////////////
g.SetGlobalVars(t)
IDData.IDSpace = IDData.IDSpace[:0]

// set folders
UserHomeDir = Home // overwrite global variable for the id package tests
Expand All @@ -54,9 +56,11 @@ func Setup(t *testing.T) func() {
CCache = filepath.Join(Cache, "/cleaned", SFName)
ICache = filepath.Join(Cache, "/inserted", SFName)

fmt.Println(t.Name(), "...")
// tear down later //////////////////
return func() {
// tear-down code here
fmt.Println(t.Name(), "...done.")
b.Reset() // Clear output generated during the test.

// Remove without error check, because file or folder could not exist after a test.
Expand Down
29 changes: 16 additions & 13 deletions internal/id/switchIDs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type triceTagIDSpace struct {

// IDIsPartOfIDSpace returns true if ID is existent inside p.IDSpace.
func (p *idData) IDIsPartOfIDSpace(id TriceID) bool {
for _, s := range p.IDSpace {
for _, i := range s.idSpace {
for _, tag := range p.IDSpace {
for _, i := range tag.idSpace {
if i == id {
return true
}
Expand All @@ -54,13 +54,14 @@ func (p *idData) IDIsPartOfIDSpace(id TriceID) bool {
// Example: When -IDMin=10, -IDMax=20 and id=99 found in source.
func (p *idData) removeIDFromIDSpace(id TriceID) {
for _, tag := range p.IDSpace {
for index, i := range tag.idSpace {
if i == id {
for idx, iD := range tag.idSpace {
if iD == id {
if SearchMethod == "random" { // do not care about order inside tag.idSpace, so do it fast
tag.idSpace[index] = tag.idSpace[len(tag.idSpace)-1] // overwrite with last
tag.idSpace = tag.idSpace[:len(tag.idSpace)-1] // remove last
fmt.Println("tag.idSpace=", tag.idSpace, "idx=", idx, "iD=", iD)
tag.idSpace[idx] = tag.idSpace[len(tag.idSpace)-1] // overwrite with last
tag.idSpace = tag.idSpace[:len(tag.idSpace)-1] // remove last
} else { // keep order inside tag.idSpace, so do it costly
tag.idSpace = append(tag.idSpace[:index], tag.idSpace[index+1:]...)
tag.idSpace = append(tag.idSpace[:idx], tag.idSpace[idx+1:]...)
}
}
}
Expand Down Expand Up @@ -103,7 +104,9 @@ common:
}
return
}
fmt.Println(tag, " not handled")
if Verbose {
fmt.Println("newID: Trice tag", tag, " not handled. Using common ID range for it.")
}
tag = ""
goto common
}
Expand Down Expand Up @@ -131,14 +134,14 @@ func (p *idData) PreProcessing(w io.Writer, fSys *afero.Afero) {

p.IDSpace = append(p.IDSpace, common)

for i, s := range p.IDSpace {
for i, tag := range p.IDSpace {
// create IDSpace
//s.idSpace = make([]TriceID, 0, s.Max-s.Min+1)
for id := s.Min; id <= s.Max; id++ {
//tag.idSpace = make([]TriceID, 0, s.Max-s.Min+1)
for id := tag.Min; id <= tag.Max; id++ {
_, usedFmt := p.idToTrice[id]
_, usedLoc := p.idToLocRef[id]
if !usedFmt && !usedLoc {
//s.idSpace = append(s.idSpace, id) <- does not work here!
//tag.idSpace = append(tag.idSpace, id) <- does not work here!
p.IDSpace[i].idSpace = append(p.IDSpace[i].idSpace, id)
} else if Verbose {
if usedFmt && !usedLoc {
Expand All @@ -153,7 +156,7 @@ func (p *idData) PreProcessing(w io.Writer, fSys *afero.Afero) {
}
}
if Verbose {
fmt.Fprintln(w, "trice tag", s.tag, "has", s.Max-s.Min+1, "IDs total space,", len(s.idSpace), "IDs usable")
fmt.Fprintln(w, "trice tag", tag.tag, "has", tag.Max-tag.Min+1, "IDs total space,", len(tag.idSpace), "IDs usable")
}
}
}
Expand Down

0 comments on commit d4753f5

Please sign in to comment.