Skip to content

Commit

Permalink
add account test
Browse files Browse the repository at this point in the history
  • Loading branch information
findbug2019 committed May 24, 2019
1 parent 581c5ec commit 0d29e6c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 26 deletions.
27 changes: 6 additions & 21 deletions accountmanager/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ func (a *Account) GetAllBalances() (map[uint64]*big.Int, error) {

// BinarySearch binary search
func (a *Account) binarySearch(assetID uint64) (int64, bool) {
if len(a.Balances) == 0 {
return 0, false
}

low := int64(0)
high := int64(len(a.Balances)) - 1
for low <= high {
Expand Down Expand Up @@ -328,26 +326,13 @@ func (a *Account) AddNewAssetByAssetID(assetID uint64, amount *big.Int) {
} else {
//insert
if a.Balances[p].AssetID < assetID {
//insert back
//insert after p
p = p + 1
tail := append([]*AssetBalance{}, a.Balances[p:]...)
a.Balances = append(a.Balances[:p], newAssetBalance(assetID, amount))
a.Balances = append(a.Balances, tail...)
} else {
//insert front
if len(a.Balances) > 1 {
if a.Balances[p].AssetID < assetID {
p = p + 1
}
tail := append([]*AssetBalance{}, a.Balances[p:]...)
a.Balances = append(a.Balances[:p], newAssetBalance(assetID, amount))
a.Balances = append(a.Balances, tail...)
} else {
tail := append([]*AssetBalance{}, a.Balances[p:]...)
a.Balances = append([]*AssetBalance{}, newAssetBalance(assetID, amount))
a.Balances = append(a.Balances, tail...)
}
}
tail := append([]*AssetBalance{}, a.Balances[p:]...)
a.Balances = append(a.Balances[:p], newAssetBalance(assetID, amount))
a.Balances = append(a.Balances, tail...)

}
}
}
Expand Down
96 changes: 91 additions & 5 deletions accountmanager/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/crypto"
)

func Test_newAssetBalance(t *testing.T) {
Expand Down Expand Up @@ -479,14 +480,32 @@ func TestAccount_binarySearch(t *testing.T) {
type args struct {
assetID uint64
}

pubkey, _ := GeneragePubKey()
asset1 := newAssetBalance(1, big.NewInt(20))
asset2 := newAssetBalance(2, big.NewInt(20))
asset3 := newAssetBalance(3, big.NewInt(20))

b := make([]*AssetBalance, 0)
b = append(b, asset1)
b = append(b, asset2)
b = append(b, asset3)
//
tests := []struct {
name string
fields fields
args args
want int64
want1 bool
}{
// TODO: Add test cases.
//test cases.
{"empty", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, nil, false, false}, args{21}, 0, false},
{"notfind", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, b, false, false}, args{21}, 2, false},
{"notfind2", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, b, false, false}, args{4}, 2, false},
{"notfind3", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, b, false, false}, args{0}, 0, false},
{"find", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, b, false, false}, args{2}, 1, true},
{"find2", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, b, false, false}, args{1}, 0, true},
{"find3", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, b, false, false}, args{3}, 2, true},
}
for _, tt := range tests {
a := &Account{
Expand Down Expand Up @@ -525,12 +544,69 @@ func TestAccount_AddNewAssetByAssetID(t *testing.T) {
assetID uint64
amount *big.Int
}

pubkey, _ := GeneragePubKey()
asset0 := newAssetBalance(0, big.NewInt(20))
asset1 := newAssetBalance(1, big.NewInt(20))
asset2 := newAssetBalance(2, big.NewInt(20))
asset3 := newAssetBalance(3, big.NewInt(20))
asset5 := newAssetBalance(5, big.NewInt(20))
// 1
a := make([]*AssetBalance, 0)
a = append(a, asset1)
// 1
b := make([]*AssetBalance, 0)
b = append(b, asset1)
// b = append(b, asset2)
//b = append(b, asset3)

c := make([]*AssetBalance, 0)
c = append(c, asset1)
c = append(c, asset2)
c = append(c, asset3)

d := make([]*AssetBalance, 0)
d = append(d, asset0)
d = append(d, asset1)
d = append(d, asset2)
d = append(d, asset3)
d = append(d, asset5)
// 0 1 2 3 5
e := make([]*AssetBalance, 0)
e = append(e, asset0)
e = append(e, asset1)
e = append(e, asset2)
e = append(e, asset3)
e = append(e, asset5)
//
tests := []struct {
name string
fields fields
args args
name string
fields fields
args args
id uint64
position int64
find bool
value *big.Int
}{
// TODO: Add test cases.
// [2]
{"emptyappend", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, nil, false, false}, args{2, big.NewInt(3)}, 2, 0, true, big.NewInt(3)},
// [2]
{"appendnotexist", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, nil, false, false}, args{2, big.NewInt(3)}, 0, 0, false, big.NewInt(3)},
// [0] 1
{"headinsert1", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, a, false, false}, args{0, big.NewInt(32)}, 0, 0, true, big.NewInt(32)},
// 1 [3]
{"append", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, b, false, false}, args{3, big.NewInt(10)}, 3, 1, true, big.NewInt(10)},
// 1
// {"1append", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, b, false, false}, args{2, big.NewInt(2)}, 1, true, big.NewInt(2)},
// [0] 1 2 3
{"headinsert", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, c, false, false}, args{0, big.NewInt(10)}, 0, 0, true, big.NewInt(10)},
// {"tailinsert", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, c, false, false}, args{3, big.NewInt(10)}, 2, true, big.NewInt(10)},
// 0 1 [2] 3 5
{"2insert", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, d, false, false}, args{2, big.NewInt(9)}, 5, 4, true, big.NewInt(20)},
// 0 1 2 3 [4] 5
{"4insert", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, d, false, false}, args{4, big.NewInt(1)}, 4, 4, true, big.NewInt(1)},
// 0 1 2 3 [4] 5
{"4insert", fields{common.Name("aaabbb1982"), 1, pubkey, nil, crypto.Keccak256Hash(nil), 0, e, false, false}, args{4, big.NewInt(2)}, 5, 5, true, big.NewInt(20)},
}

for _, tt := range tests {
Expand All @@ -545,6 +621,16 @@ func TestAccount_AddNewAssetByAssetID(t *testing.T) {
Destroy: tt.fields.Destroy,
}
a.AddNewAssetByAssetID(tt.args.assetID, tt.args.amount)
got, got1 := a.binarySearch(tt.id)
if got != tt.position {
t.Errorf("%q. Account.AddNewAssetByAssetID() got = %v, want %v", tt.name, got, tt.position)
}
if got1 != tt.find {
t.Errorf("%q. Account.AddNewAssetByAssetID() got1 position = %v, want %v", tt.name, got1, tt.find)
}
if a.Balances[got].Balance.Cmp(tt.value) != 0 {
t.Errorf("%q. Account.AddNewAssetByAssetID() balance = %v, want %v", tt.name, a.Balances[got].Balance, tt.value)
}
}
}

Expand Down

0 comments on commit 0d29e6c

Please sign in to comment.