From 0d29e6cb200d9c217c9540084ba793651a4e6d06 Mon Sep 17 00:00:00 2001 From: findbug2019 Date: Fri, 24 May 2019 19:32:21 +0800 Subject: [PATCH] add account test --- accountmanager/account.go | 27 +++------- accountmanager/account_test.go | 96 ++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 26 deletions(-) diff --git a/accountmanager/account.go b/accountmanager/account.go index 57a58b81..c8f98fbf 100644 --- a/accountmanager/account.go +++ b/accountmanager/account.go @@ -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 { @@ -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...) + } } } diff --git a/accountmanager/account_test.go b/accountmanager/account_test.go index 66ab00c2..822c8374 100644 --- a/accountmanager/account_test.go +++ b/accountmanager/account_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/fractalplatform/fractal/common" + "github.com/fractalplatform/fractal/crypto" ) func Test_newAssetBalance(t *testing.T) { @@ -479,6 +480,17 @@ 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 @@ -486,7 +498,14 @@ func TestAccount_binarySearch(t *testing.T) { 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{ @@ -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 { @@ -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) + } } }