forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c24dd8
commit bfdb290
Showing
3 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
module gno.land/p/demo/teritori/worx | ||
|
||
require ( | ||
gno.land/p/demo/avl v0.0.0-latest | ||
gno.land/p/demo/ufmt v0.0.0-latest | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,56 @@ | ||
package worx | ||
import( | ||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
type WorxKeeper struct { | ||
worxs []*Worx | ||
worxs *avl.Tree | ||
} | ||
const dayToSeconds = 1 | ||
|
||
func NewWorxKeeper() *WorxKeeper{ | ||
return &WorxKeeper{ | ||
worxs: avl.NewTree(), | ||
} | ||
} | ||
func (keeper *WorxKeeper) Store(worx *Worx) { | ||
keeper.worxs = append(keeper.worxs, worx) | ||
key := ufmt.Sprintf("%d", worx.Timestamp) | ||
worxsInterface, exists := keeper.worxs.Get(key) | ||
|
||
if exists { | ||
worxsOnDay := worxsInterface.([]*Worx) | ||
worxsOnDay = append(worxsOnDay, worx) | ||
keeper.worxs.Set(key, worxsOnDay) | ||
return | ||
} | ||
keeper.worxs.Set(key, []*Worx{worx}) | ||
} | ||
|
||
func (keeper *WorxKeeper) Get() []*Worx { | ||
return keeper.worxs | ||
totalWorx := make([]*Worx, 0) | ||
keeper.worxs.Iterate("", "", func(key string, value interface{}) bool { | ||
worxByDay := value.([]*Worx) | ||
totalWorx = append(totalWorx, worxByDay...) | ||
return false | ||
}) | ||
|
||
return totalWorx | ||
} | ||
|
||
func (keeper *WorxKeeper) GetFromDate(date int64) []*Worx { | ||
start := ufmt.Sprintf("%d", date) | ||
totalWorx := make([]*Worx, 0) | ||
keeper.worxs.Iterate(start, "", func(key string, value interface{}) bool { | ||
// here we account for string comparation "95" > "105" because comparing first character 9 > 1 (Comparation of lenght) | ||
// and simply comparing same lenght string 25 > 12 this in order to stop tree for iterating over lower dates leaves | ||
if len(start) > len(key) || start > key { | ||
return true | ||
} | ||
worxByDay := value.([]*Worx) | ||
totalWorx = append(totalWorx, worxByDay...) | ||
return false | ||
}) | ||
|
||
return totalWorx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package worx | ||
|
||
import ( | ||
"testing" | ||
"gno.land/p/demo/rand" | ||
"gno.land/p/demo/testutils" | ||
"gno.land/p/demo/ufmt" | ||
"std" | ||
) | ||
|
||
func TestAddGet(t *testing.T) { | ||
keeper := NewWorxKeeper() | ||
user1 := testutils.TestAddress("user1") | ||
if len(keeper.Get()) != 0{ | ||
t.Fatalf("Keeper is not empty initialized") | ||
} | ||
|
||
fillRandomWorx(keeper, 10123423, user1) | ||
if len(keeper.Get()) != 1{ | ||
t.Fatalf("Keeper Worx was not added to keeper 1") | ||
} | ||
|
||
fillRandomWorx(keeper, 10123423, user1) | ||
if len(keeper.Get()) != 2{ | ||
t.Fatalf("Keeper Worx was not added to keeper 2") | ||
} | ||
} | ||
|
||
func TestGetFromDate(t *testing.T) { | ||
keeper := NewWorxKeeper() | ||
user1 := testutils.TestAddress("user1") | ||
if len(keeper.Get()) != 0{ | ||
t.Fatalf("Keeper is not empty initialized") | ||
} | ||
|
||
for i:=0; i < 100; i++ { | ||
fillRandomWorx(keeper, int64(i*10), user1) | ||
} | ||
|
||
if len(keeper.Get()) != 100{ | ||
t.Fatalf("Keeper Worx was not totally added") | ||
} | ||
|
||
if len(keeper.GetFromDate(1003)) != 0 { | ||
t.Fatalf("Get From Date Should have found 0 registers") | ||
} | ||
|
||
if len(keeper.GetFromDate(903)) != 9 { | ||
t.Fatalf("Get From Date Should have found 9 registers") | ||
} | ||
|
||
} | ||
|
||
func fillRandomWorx(keeper *WorxKeeper, timestamp int64, address std.Address){ | ||
r := rand.New() | ||
keeper.Store(NewWorx( | ||
r.Intn(25), | ||
"somekey", | ||
address, | ||
r.Intn(100), | ||
timestamp, | ||
)) | ||
} |