|
| 1 | +package protocol |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestContractCreation(t *testing.T) { |
| 10 | + createdContract := NewContract(accA.Address, accA.Issuer, accA.Balance, accA.Contract, accA.ContractVariables) |
| 11 | + |
| 12 | + if !reflect.DeepEqual(createdContract.Address, accA.Address) { |
| 13 | + t.Errorf("Address does not match the given one: %x vs. %x", createdContract.Address, accA.Address) |
| 14 | + } |
| 15 | + |
| 16 | + if !reflect.DeepEqual(createdContract.Issuer, accA.Issuer) { |
| 17 | + t.Errorf("Issuer does not match the given one: %x vs. %x", createdContract.Issuer, accA.Issuer) |
| 18 | + } |
| 19 | + |
| 20 | + if !reflect.DeepEqual(createdContract.Balance, accA.Balance) { |
| 21 | + t.Errorf("Balance does not match the given one: %v vs. %v", createdContract.Balance, accA.Balance) |
| 22 | + } |
| 23 | + |
| 24 | + if !reflect.DeepEqual(createdContract.Contract, accA.Contract) { |
| 25 | + t.Errorf("Contract does not match the given one: %x vs. %x", createdContract.Contract, accA.Contract) |
| 26 | + } |
| 27 | + |
| 28 | + if !reflect.DeepEqual(createdContract.ContractVariables, accA.ContractVariables) { |
| 29 | + t.Errorf("ContractVariables does not match the given one: %x vs. %x", createdContract.ContractVariables, accA.ContractVariables) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestContractHash(t *testing.T) { |
| 34 | + var address [64]byte |
| 35 | + rand.Read(address[:]) |
| 36 | + |
| 37 | + hash1 := accA.Hash() |
| 38 | + |
| 39 | + if !reflect.DeepEqual(hash1, accA.Hash()) { |
| 40 | + t.Errorf("Contract hashing failed!") |
| 41 | + } |
| 42 | + |
| 43 | + accA.Address = address |
| 44 | + hash2 :=accA.Hash() |
| 45 | + |
| 46 | + if reflect.DeepEqual(hash1, hash2) { |
| 47 | + t.Errorf("Contract hashing failed!") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestContractSerialization(t *testing.T) { |
| 52 | + addTestingAccounts() |
| 53 | + |
| 54 | + accA.Balance = 1000 |
| 55 | + accA.IsStaking = true |
| 56 | + accA.TxCnt = 5 |
| 57 | + |
| 58 | + var compareContract *Contract |
| 59 | + encodedContract := accA.Encode() |
| 60 | + compareContract = compareContract.Decode(encodedContract) |
| 61 | + |
| 62 | + if !reflect.DeepEqual(accA, compareContract) { |
| 63 | + t.Error("Contract encoding/decoding failed!") |
| 64 | + } |
| 65 | +} |
0 commit comments