github.com/chain5j/chain5j-pkg@v1.0.7/collection/maps/hashmap/hashmap_rlp_test.go (about)

     1  // Package hashmap
     2  //
     3  // @author: xwc1125
     4  package hashmap
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/chain5j/chain5j-pkg/codec"
    11  	"github.com/chain5j/chain5j-pkg/math"
    12  	"github.com/chain5j/chain5j-pkg/util/convutil"
    13  	"github.com/chain5j/chain5j-pkg/util/hexutil"
    14  	"github.com/mitchellh/mapstructure"
    15  )
    16  
    17  func TestConvert(t *testing.T) {
    18  	// int8: -128 ~ 127
    19  	// int16: -32768 ~ 32767
    20  	// int32: -2147483648 ~ 2147483647
    21  	// int64: -9223372036854775808 ~ 9223372036854775807
    22  	// uint8: 0 ~ 255
    23  	// uint16: 0 ~ 65535
    24  	// uint32: 0 ~ 4294967295
    25  	// uint64: 0 ~ 18446744073709551615
    26  	hashMap := NewHashMap(true)
    27  	hashMap.Put("int_0", 0)
    28  	hashMap.Put("int_1", 1)
    29  	hashMap.Put("int8_MinInt8", math.MinInt8)
    30  	hashMap.Put("int8_MaxInt8", math.MaxInt8)
    31  	hashMap.Put("int16_MinInt16", math.MinInt16)
    32  	hashMap.Put("int16_MaxInt16", math.MaxInt16)
    33  	hashMap.Put("int32_MinInt32", math.MinInt32)
    34  	hashMap.Put("int32_MaxInt32", math.MaxInt32)
    35  	hashMap.Put("int64_MinInt64", math.MinInt64)
    36  	hashMap.Put("int64_MaxInt64", math.MaxInt64)
    37  	// uint
    38  	hashMap.Put("uint_0", 0)
    39  	hashMap.Put("uint8_MaxUint8", math.MaxUint8)
    40  	hashMap.Put("uint16_MaxUint16", math.MaxUint16)
    41  	hashMap.Put("uint32_MaxUint32", math.MaxUint32)
    42  	// hashMap.Put("uint64_1", math.MaxUint64)
    43  
    44  	hashMap.Put("show", true)
    45  
    46  	if bytes, err := codec.Codecor().Encode(hashMap); err != nil {
    47  		t.Fatal(err)
    48  	} else {
    49  		newHashMap := NewHashMap(true)
    50  		if err := codec.Codecor().Decode(bytes, newHashMap); err != nil {
    51  			t.Fatal(err)
    52  		}
    53  		for k, v := range newHashMap.data {
    54  			t.Log("---------------" + k + "-------------")
    55  			{
    56  				t.Log("===========BytesToBool============")
    57  				b, err := convutil.BytesToBool(v.([]byte))
    58  				if err != nil {
    59  					t.Error(k, err)
    60  				} else {
    61  					t.Log(k, b)
    62  				}
    63  			}
    64  			{
    65  				t.Log("===========BytesToInt64============")
    66  				b, err := convutil.BytesToInt64(v.([]byte))
    67  				if err != nil {
    68  					t.Error(k, err)
    69  				} else {
    70  					t.Log(k, b)
    71  				}
    72  			}
    73  			{
    74  				t.Log("===========BytesToInt64U============")
    75  				b, err := convutil.BytesToUint64(v.([]byte))
    76  				if err != nil {
    77  					t.Error(k, err)
    78  				} else {
    79  					t.Log(k, b)
    80  				}
    81  			}
    82  			{
    83  				t.Log("===========BytesToString============")
    84  				b, err := convutil.BytesToString(v.([]byte))
    85  				if err != nil {
    86  					t.Error(k, err)
    87  				} else {
    88  					t.Log(k, b)
    89  				}
    90  			}
    91  		}
    92  	}
    93  }
    94  
    95  func TestRlp(t *testing.T) {
    96  	hashMap := NewHashMap(true)
    97  	hashMap.Put("epoch", 30000)
    98  	hashMap.Put("period", 5)
    99  	hashMap.Put("policy", 0)
   100  	hashMap.Put("timeout", 10000)
   101  	hashMap.Put("show", true)
   102  
   103  	hashMap.Put("manager", "0x9254e62fbca63769dfd4cc8e23f630f0785610ce0x9254e62fbca63769dfd4cc8e23f630f0785610ce")
   104  	hashMap.Put("managers", []string{
   105  		"0x9254e62fbca63769dfd4cc8e23f630f0785610ce0x9254e62fbca63769dfd4cc8e23f630f0785610ce",
   106  		"0x92c8cae42a94045670cbb0bfcf8f790d9f8097e70x9254e62fbca63769dfd4cc8e23f630f0785610ce",
   107  	})
   108  	hashMap.Put("validator", []string{
   109  		"QmVy5JASWLUns3Wwe91arP93rPPumkEQ7fQZ4GfpevxKbd",
   110  		"QmafFDAXGtW1M2zhsiYPxUtpkYjAgBUpPDPQZRkhgonGrT",
   111  	})
   112  	if bytes, err := codec.Codecor().Encode(hashMap); err != nil {
   113  		t.Fatal(err)
   114  	} else {
   115  		newHashMap := NewHashMap(true)
   116  		if err := codec.Codecor().Decode(bytes, newHashMap); err != nil {
   117  			t.Fatal(err)
   118  		}
   119  		t.Logf("%v", newHashMap)
   120  		{
   121  			obj := newHashMap.GetObj("managers")
   122  			switch p := obj.(type) {
   123  			case []interface{}:
   124  				for _, p1 := range p {
   125  					v := p1.([]byte)
   126  					t.Log(string(v))
   127  				}
   128  			}
   129  		}
   130  		{
   131  			obj := newHashMap.GetObj("epoch")
   132  			switch p := obj.(type) {
   133  			case []byte:
   134  				toInt, _ := convutil.BytesToInt64(p)
   135  				fmt.Println(toInt)
   136  				fmt.Println(hexutil.Encode(p))
   137  			}
   138  		}
   139  		for k, v := range newHashMap.data {
   140  			switch k {
   141  			case "show":
   142  				b, err := convutil.BytesToBool(v.([]byte))
   143  				if err != nil {
   144  					t.Error(k, err)
   145  				} else {
   146  					t.Log(k, b)
   147  				}
   148  			case "manager":
   149  				b, err := convutil.BytesToString(v.([]byte))
   150  				if err != nil {
   151  					t.Error(k, err)
   152  				} else {
   153  					t.Log(k, b)
   154  				}
   155  			case "epoch", "epoch1":
   156  				b, err := convutil.BytesToUint64(v.([]byte))
   157  				if err != nil {
   158  					t.Error(k, err)
   159  				} else {
   160  					t.Log(k, b)
   161  				}
   162  			default:
   163  				switch v.(type) {
   164  				case []byte:
   165  					b, err := convutil.BytesToInt64(v.([]byte))
   166  					if err != nil {
   167  						t.Error(k, err)
   168  					} else {
   169  						t.Log(k, b)
   170  					}
   171  				}
   172  			}
   173  		}
   174  
   175  		var config testConfig
   176  		if err := mapstructure.WeakDecode(newHashMap.data, &config); err != nil {
   177  			t.Error(err)
   178  		} else {
   179  			t.Log(config)
   180  		}
   181  	}
   182  }
   183  
   184  // testConfig 配置
   185  type testConfig struct {
   186  	Epoch     uint64   `json:"epoch" mapstructure:"epoch"`         // 期数
   187  	Timeout   uint64   `json:"timeout" mapstructure:"timeout"`     // round 超时时间, 在BlockPeriod不为0时有效
   188  	Period    uint64   `json:"period" mapstructure:"period"`       // 区块产生间隔
   189  	Managers  []string `json:"managers" mapstructure:"managers"`   // 管理员
   190  	Validator []string `json:"validator" mapstructure:"validator"` // 验证者
   191  	Show      bool     `json:"show" mapstructure:"show"`
   192  }