func TestCurrencyScan(t *testing.T) {
	var c0 Currency
	c1 := NewCurrency64(81293)
	_, err := fmt.Sscan("81293", &c0)
	if err != nil {
		t.Fatal(err)
	}
	if c0.Cmp(c1) != 0 {
		t.Error("scanned number does not equal expected value")
	}
	_, err = fmt.Sscan("z", &c0)
	if err == nil {
		t.Fatal("scan is accepting garbage input")
	}
}
TestCurrencyScan probes the Scan function of the currency type.
package types
import (
	"bytes"
	"encoding/json"
	"fmt"
	"math/big"
	"strings"
	"testing"
	"github.com/NebulousLabs/Sia/crypto"
	"github.com/NebulousLabs/Sia/encoding"
	"github.com/NebulousLabs/fastrand"
)
func hashStr(v interface{}) string {
	h := crypto.HashObject(v)
	return fmt.Sprintf("%x", h[:])
}
var heavyBlock = func() Block {
	b := Block{
		MinerPayouts: []SiacoinOutput{
			{Value: CalculateCoinbase(0)},
			{Value: CalculateCoinbase(1)},
		},
		Transactions: []Transaction{
			{
				SiacoinInputs: []SiacoinInput{{
					UnlockConditions: UnlockConditions{
						PublicKeys: []SiaPublicKey{{
							Algorithm: SignatureEd25519,
							Key:       fastrand.Bytes(32),
						}},
						SignaturesRequired: 6,
					},
				}},
				SiacoinOutputs: []SiacoinOutput{{
					Value: NewCurrency64(20),
				}},
				FileContracts: []FileContract{{
					FileSize:       12,
					Payout:         NewCurrency64(100),
					RevisionNumber: 8,
					ValidProofOutputs: []SiacoinOutput{{
						Value: NewCurrency64(2),
					}},
					MissedProofOutputs: []SiacoinOutput{{
						Value: NewCurrency64(3),
					}},
				}},
				FileContractRevisions: []FileContractRevision{{
					NewFileSize:       13,
					NewRevisionNumber: 9,
					UnlockConditions: UnlockConditions{
						PublicKeys: []SiaPublicKey{{
							Algorithm: SignatureEd25519,
							Key:       fastrand.Bytes(32),
						}},
					},
					NewValidProofOutputs: []SiacoinOutput{{
						Value: NewCurrency64(4),
					}},
					NewMissedProofOutputs: []SiacoinOutput{{
						Value: NewCurrency64(5),
					}},
				}},
				StorageProofs: []StorageProof{{
					HashSet: []crypto.Hash{{}},
				}},
				SiafundInputs: []SiafundInput{{
					UnlockConditions: UnlockConditions{
						PublicKeys: []SiaPublicKey{{
							Algorithm: SignatureEd25519,
							Key:       fastrand.Bytes(32),
						}},
					},
				}},
				SiafundOutputs: []SiafundOutput{{
					ClaimStart: NewCurrency64(99),
					Value:      NewCurrency64(25),
				}},
				MinerFees:     []Currency{NewCurrency64(215)},
				ArbitraryData: [][]byte{fastrand.Bytes(10)},
				TransactionSignatures: []TransactionSignature{{
					PublicKeyIndex: 5,
					Timelock:       80,
					CoveredFields: CoveredFields{
						WholeTransaction:      true,
						SiacoinInputs:         []uint64{0},
						SiacoinOutputs:        []uint64{1},
						FileContracts:         []uint64{2},
						FileContractRevisions: []uint64{3},
						StorageProofs:         []uint64{4},
						SiafundInputs:         []uint64{5},
						SiafundOutputs:        []uint64{6},
						MinerFees:             []uint64{7},
						ArbitraryData:         []uint64{8},
						TransactionSignatures: []uint64{9},
					},
					Signature: fastrand.Bytes(32),
				}},
			},
		},
	}
	fastrand.Read(b.Transactions[0].SiacoinInputs[0].ParentID[:])
	fastrand.Read(b.Transactions[0].SiacoinOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].FileContracts[0].FileMerkleRoot[:])
	fastrand.Read(b.Transactions[0].FileContracts[0].ValidProofOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].FileContracts[0].MissedProofOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].FileContractRevisions[0].ParentID[:])
	fastrand.Read(b.Transactions[0].FileContractRevisions[0].NewFileMerkleRoot[:])
	fastrand.Read(b.Transactions[0].FileContractRevisions[0].NewValidProofOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].FileContractRevisions[0].NewMissedProofOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].StorageProofs[0].ParentID[:])
	fastrand.Read(b.Transactions[0].StorageProofs[0].HashSet[0][:])
	fastrand.Read(b.Transactions[0].StorageProofs[0].Segment[:])
	fastrand.Read(b.Transactions[0].SiafundInputs[0].ParentID[:])
	fastrand.Read(b.Transactions[0].SiafundInputs[0].ClaimUnlockHash[:])
	fastrand.Read(b.Transactions[0].SiafundOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].TransactionSignatures[0].ParentID[:])
	return b
}()
func TestBlockEncoding(t *testing.T) {
	var decB Block
	err := encoding.Unmarshal(encoding.Marshal(heavyBlock), &decB)
	if err != nil {
		t.Fatal(err)
	}
	if hashStr(heavyBlock) != hashStr(decB) {
		t.Fatal("block changed after encode/decode:", heavyBlock, decB)
	}
}
func TestBadBlock(t *testing.T) {
	badData := "000000000000000000000000000000000000000000000000\x00\x00\x00\x00\x00\x00\x00\x00\x00"
	var block Block
	err := encoding.Unmarshal([]byte(badData), &block)
	if err == nil {
		t.Fatal("invalid block decoded successfully")
	}
}
func TestTooLargeDecoder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	enc := encoding.Marshal(Block{})
	copy(enc[len(enc)-8:], encoding.EncUint64(^uint64(0)))
	var block Block
	err := encoding.Unmarshal(enc, &block)
	if err == nil {
		t.Fatal("expected error, got nil")
	}
	var arb [][]byte
	for i := 0; i < 4; i++ {
		arb = append(arb, make([]byte, encoding.MaxSliceSize-1))
	}
	block.Transactions = []Transaction{{
		ArbitraryData: arb,
	}}
	enc = encoding.Marshal(block)
	err = encoding.Unmarshal(enc, &block)
	if err == nil {
		t.Fatal("expected error, got nil")
	}
}
func TestCurrencyMarshalJSON(t *testing.T) {
	b30 := big.NewInt(30)
	c30 := NewCurrency64(30)
	bMar30, err := b30.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	cMar30, err := c30.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(bMar30, bytes.Trim(cMar30, `"`)) {
		t.Error("Currency does not match the marshalling of its math/big equivalent")
	}
	var cUmar30 Currency
	err = cUmar30.UnmarshalJSON(cMar30)
	if err != nil {
		t.Fatal(err)
	}
	if c30.Cmp(cUmar30) != 0 {
		t.Error("Incorrect unmarshalling of currency type.")
	}
	cMar30[0] = 0
	err = cUmar30.UnmarshalJSON(cMar30)
	if err == nil {
		t.Error("JSON decoded nonsense input")
	}
}
// TestCurrencyMarshalSia probes the MarshalSia and UnmarshalSia functions of
// the currency type.
func TestCurrencyMarshalSia(t *testing.T) {
	c := NewCurrency64(1656)
	buf := new(bytes.Buffer)
	err := c.MarshalSia(buf)
	if err != nil {
		t.Fatal(err)
	}
	var cUmar Currency
	cUmar.UnmarshalSia(buf)
	if c.Cmp(cUmar) != 0 {
		t.Error("marshal and unmarshal mismatch for currency type")
	}
}
// TestCurrencyString probes the String function of the currency type.
func TestCurrencyString(t *testing.T) {
	b := big.NewInt(7135)
	c := NewCurrency64(7135)
	if b.String() != c.String() {
		t.Error("string function not behaving as expected")
	}
}