-
Notifications
You must be signed in to change notification settings - Fork 146
chore(lib/trie): lib/trie/recorder sub-package
#2082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
EclesioMeloJunior
merged 4 commits into
qdm12-trie-node-subpkg-base
from
qdm12-trie-recorder-subpkg
Nov 30, 2021
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,7 @@ | ||
| package record | ||
|
|
||
| // Node represents a record of a visited node | ||
| type Node struct { | ||
| RawData []byte | ||
| Hash []byte | ||
| } |
This file contains hidden or 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,38 @@ | ||
| // Copyright 2021 ChainSafe Systems (ON) | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| package record | ||
|
|
||
| // Recorder records the list of nodes found by Lookup.Find | ||
| type Recorder struct { | ||
| nodes []Node | ||
| } | ||
|
|
||
| // NewRecorder creates a new recorder. | ||
| func NewRecorder() *Recorder { | ||
| return &Recorder{} | ||
| } | ||
|
|
||
| // Record appends a node to the list of visited nodes. | ||
| func (r *Recorder) Record(hash, rawData []byte) { | ||
| r.nodes = append(r.nodes, Node{RawData: rawData, Hash: hash}) | ||
| } | ||
|
|
||
| // Next returns the first node in the recorded list | ||
| // and removes it (shift operation). | ||
| func (r *Recorder) Next() (node *Node) { | ||
| if len(r.nodes) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| node = &r.nodes[0] | ||
| r.nodes = r.nodes[1:] | ||
|
|
||
| return node | ||
| } | ||
|
|
||
| // IsEmpty returns true if no node is in in the current | ||
| // recorded list of nodes. | ||
| func (r *Recorder) IsEmpty() (empty bool) { | ||
| return len(r.nodes) == 0 | ||
| } | ||
This file contains hidden or 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,157 @@ | ||
| package record | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func Test_NewRecorder(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| expected := &Recorder{} | ||
|
|
||
| recorder := NewRecorder() | ||
|
|
||
| assert.Equal(t, expected, recorder) | ||
| } | ||
|
|
||
| func Test_Recorder_Record(t *testing.T) { | ||
| testCases := map[string]struct { | ||
| recorder *Recorder | ||
| hash []byte | ||
| rawData []byte | ||
| expectedRecorder *Recorder | ||
| }{ | ||
| "nil data": { | ||
| recorder: &Recorder{}, | ||
| expectedRecorder: &Recorder{ | ||
| nodes: []Node{ | ||
| {}, | ||
| }, | ||
| }, | ||
| }, | ||
| "insert in empty recorder": { | ||
| recorder: &Recorder{}, | ||
| hash: []byte{1, 2}, | ||
| rawData: []byte{3, 4}, | ||
| expectedRecorder: &Recorder{ | ||
| nodes: []Node{ | ||
| {Hash: []byte{1, 2}, RawData: []byte{3, 4}}, | ||
| }, | ||
| }, | ||
| }, | ||
| "insert in non-empty recorder": { | ||
| recorder: &Recorder{ | ||
| nodes: []Node{ | ||
| {Hash: []byte{5, 6}, RawData: []byte{7, 8}}, | ||
| }, | ||
| }, | ||
| hash: []byte{1, 2}, | ||
| rawData: []byte{3, 4}, | ||
| expectedRecorder: &Recorder{ | ||
| nodes: []Node{ | ||
| {Hash: []byte{5, 6}, RawData: []byte{7, 8}}, | ||
| {Hash: []byte{1, 2}, RawData: []byte{3, 4}}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for name, testCase := range testCases { | ||
| testCase := testCase | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testCase.recorder.Record(testCase.hash, testCase.rawData) | ||
|
|
||
| assert.Equal(t, testCase.expectedRecorder, testCase.recorder) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_Recorder_Next(t *testing.T) { | ||
| testCases := map[string]struct { | ||
| recorder *Recorder | ||
| node *Node | ||
| expectedRecorder *Recorder | ||
| }{ | ||
| "no node": { | ||
| recorder: &Recorder{}, | ||
| expectedRecorder: &Recorder{}, | ||
| }, | ||
| "get single node from recorder": { | ||
| recorder: &Recorder{ | ||
| nodes: []Node{ | ||
| {Hash: []byte{1, 2}, RawData: []byte{3, 4}}, | ||
| }, | ||
| }, | ||
| node: &Node{Hash: []byte{1, 2}, RawData: []byte{3, 4}}, | ||
| expectedRecorder: &Recorder{ | ||
| nodes: []Node{}, | ||
| }, | ||
| }, | ||
| "get node from multiple nodes in recorder": { | ||
| recorder: &Recorder{ | ||
| nodes: []Node{ | ||
| {Hash: []byte{1, 2}, RawData: []byte{3, 4}}, | ||
| {Hash: []byte{5, 6}, RawData: []byte{7, 8}}, | ||
| {Hash: []byte{9, 6}, RawData: []byte{7, 8}}, | ||
| }, | ||
| }, | ||
| node: &Node{Hash: []byte{1, 2}, RawData: []byte{3, 4}}, | ||
| expectedRecorder: &Recorder{ | ||
| nodes: []Node{ | ||
| {Hash: []byte{5, 6}, RawData: []byte{7, 8}}, | ||
| {Hash: []byte{9, 6}, RawData: []byte{7, 8}}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for name, testCase := range testCases { | ||
| testCase := testCase | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| node := testCase.recorder.Next() | ||
|
|
||
| assert.Equal(t, testCase.node, node) | ||
| assert.Equal(t, testCase.expectedRecorder, testCase.recorder) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_Recorder_IsEmpty(t *testing.T) { | ||
| testCases := map[string]struct { | ||
| recorder *Recorder | ||
| empty bool | ||
| }{ | ||
| "nil nodes": { | ||
| recorder: &Recorder{}, | ||
| empty: true, | ||
| }, | ||
| "empty nodes": { | ||
| recorder: &Recorder{ | ||
| nodes: []Node{}, | ||
| }, | ||
| empty: true, | ||
| }, | ||
| "non-empty nodes": { | ||
| recorder: &Recorder{ | ||
| nodes: []Node{{}}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for name, testCase := range testCases { | ||
| testCase := testCase | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| empty := testCase.recorder.IsEmpty() | ||
|
|
||
| assert.Equal(t, testCase.empty, empty) | ||
| }) | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.