-
Notifications
You must be signed in to change notification settings - Fork 251
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
138d4f6
commit df2886c
Showing
5 changed files
with
343 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package tx | ||
|
||
import ( | ||
"math/big" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vechain/thor/thor" | ||
) | ||
|
||
func TestClauseTo(t *testing.T) { | ||
var toAddress thor.Address | ||
copy(toAddress[:], []byte{0xde, 0xad, 0xbe, 0xef}) | ||
|
||
clause := &Clause{ | ||
body: clauseBody{ | ||
To: &toAddress, | ||
}, | ||
} | ||
|
||
result := clause.To() | ||
|
||
// The result should not be nil and should match the mock address | ||
assert.NotNil(t, result) | ||
assert.Equal(t, toAddress, *result) | ||
|
||
// Test the case where 'To' is nil | ||
clause.body.To = nil | ||
result = clause.To() | ||
|
||
// The result should be nil | ||
assert.Nil(t, result) | ||
} | ||
|
||
func TestClauseValue(t *testing.T) { | ||
expectedValue := big.NewInt(100) // Mock value | ||
|
||
clause := &Clause{ | ||
body: clauseBody{ | ||
Value: expectedValue, | ||
}, | ||
} | ||
|
||
result := clause.Value() | ||
|
||
// The result should not be nil and should match the mock value | ||
assert.NotNil(t, result) | ||
assert.Equal(t, 0, expectedValue.Cmp(result)) | ||
} | ||
|
||
func TestClauseData(t *testing.T) { | ||
expectedData := []byte{0x01, 0x02, 0x03} // Mock data | ||
|
||
clause := &Clause{ | ||
body: clauseBody{ | ||
Data: expectedData, | ||
}, | ||
} | ||
|
||
result := clause.Data() | ||
|
||
// The result should not be nil and should match the mock data | ||
assert.NotNil(t, result) | ||
assert.True(t, reflect.DeepEqual(expectedData, result)) | ||
} |
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
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
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,28 @@ | ||
package tx_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vechain/thor/thor" | ||
"github.com/vechain/thor/tx" | ||
) | ||
|
||
func MockTransactions(n int) tx.Transactions { | ||
txs := make(tx.Transactions, n) | ||
for i := range txs { | ||
mockTx := GetMockTx() | ||
txs[i] = &mockTx | ||
} | ||
return txs | ||
} | ||
|
||
func TestRootHash(t *testing.T) { | ||
// Test empty transactions slice | ||
emptyTxs := tx.Transactions{} | ||
emptyRoot := emptyTxs.RootHash() | ||
assert.Equal(t, emptyTxs.RootHash(), emptyRoot) | ||
|
||
nonEmptyTxs := MockTransactions(2) | ||
assert.Equal(t, nonEmptyTxs.RootHash(), thor.Bytes32{0x30, 0x9a, 0xd5, 0x4b, 0x28, 0x76, 0x65, 0x52, 0x66, 0x89, 0x7b, 0x19, 0x22, 0x24, 0x63, 0xd8, 0x27, 0xc8, 0x2a, 0xd6, 0x20, 0x17, 0x7a, 0xcf, 0x9a, 0xfa, 0xc, 0xce, 0xff, 0x12, 0x24, 0x48}) | ||
} |
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,51 @@ | ||
package tx | ||
|
||
import ( | ||
"math" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWorkToGas(t *testing.T) { | ||
// Test cases | ||
testCases := []struct { | ||
name string | ||
work *big.Int | ||
blockNum uint32 | ||
expected uint64 | ||
}{ | ||
{ | ||
name: "Basic conversion", | ||
work: big.NewInt(10000), | ||
blockNum: 100, | ||
expected: 10, | ||
}, | ||
{ | ||
name: "Zero work", | ||
work: big.NewInt(0), | ||
blockNum: 100, | ||
expected: 0, | ||
}, | ||
{ | ||
name: "Large work value", | ||
work: big.NewInt(math.MaxInt64), | ||
blockNum: 100, | ||
expected: 0x20c49ba5e353f7, | ||
}, | ||
{ | ||
name: "Large work value and blockNum", | ||
work: big.NewInt(math.MaxInt64), | ||
blockNum: 12345679, | ||
expected: 0x52fc533083329, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result := workToGas(tc.work, tc.blockNum) | ||
assert.Equal(t, tc.expected, result, "Expected and actual gas should match") | ||
}) | ||
} | ||
} |