github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/f/convert_test.go (about)

     1  package f_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/angenalZZZ/gofunc/f"
     8  )
     9  
    10  func TestGbkToUtf8(t *testing.T) {
    11  	data, err := f.ReadFile("../test/temp/encoding-gbk.txt")
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	} else {
    15  		t.Logf("%s", data)
    16  	}
    17  	if data, err = f.GbkToUtf8(data); err != nil {
    18  		t.Fatal(err)
    19  	} else {
    20  		t.Logf("%s", data)
    21  	}
    22  }
    23  
    24  func TestToUtf8(t *testing.T) {
    25  	data, err := f.ReadFile("../test/temp/encoding-gbk.txt")
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if data, err = f.ToUtf8(data, "gbk"); err != nil {
    30  		t.Fatal(err)
    31  	} else {
    32  		t.Logf("%s", data)
    33  	}
    34  }
    35  
    36  func TestToInt(t *testing.T) {
    37  	// ToInt parse string to int64
    38  	if i, err := f.ToInt("123"); err != nil {
    39  		t.Fatal(err)
    40  	} else {
    41  		t.Logf("ToInt: %d\n", i)
    42  	}
    43  
    44  	raw := "hello world"
    45  	rawData := []byte(raw)
    46  	// Int32Sum returns the CRC-32 checksum of data using the IEEE polynomial.
    47  	int32x := f.Int32Sum(rawData)
    48  	t.Logf("Int32Sum: %d\n", int32x)
    49  	int32x = f.Int32SumString(raw)
    50  	t.Logf("Int32SumString: %d\n", int32x)
    51  	// Int64Sum xx.hash is a Go implementation of the 64-bit xxHash algorithm, XXH64.
    52  	int64x := f.Int64Sum(rawData)
    53  	t.Logf("Int64Sum: %d\n", int64x)
    54  	int64x = f.Int64SumString(raw)
    55  	t.Logf("Int64SumString: %d\n", int64x)
    56  	int64x = f.Int64MemHash(rawData)
    57  	t.Logf("Int64MemHash: %d\n", int64x)
    58  	int64x = f.Int64MemHashString(raw)
    59  	t.Logf("Int64MemHashString: %d\n", int64x)
    60  
    61  	buf1 := f.BytesUint16(65535)
    62  	i16 := f.Uint16Bytes(buf1)
    63  	if i16 != 1<<16-1 {
    64  		t.Fatal()
    65  	}
    66  
    67  	buf2 := f.BytesUint32(4294967295)
    68  	i32 := f.Uint32Bytes(buf2)
    69  	if i32 != 1<<32-1 {
    70  		t.Fatal()
    71  	}
    72  
    73  	buf3 := f.BytesUint64(18446744073709551615)
    74  	i64 := f.Uint64Bytes(buf3)
    75  	if i64 != 1<<64-1 {
    76  		t.Fatal()
    77  	}
    78  }
    79  
    80  func TestMapMerge(t *testing.T) {
    81  	for _, tuple := range []struct {
    82  		src      string
    83  		dst      string
    84  		expected string
    85  	}{
    86  		{
    87  			src:      `{}`,
    88  			dst:      `{}`,
    89  			expected: `{}`,
    90  		},
    91  		{
    92  			src:      `{"b":2}`,
    93  			dst:      `{"a":1}`,
    94  			expected: `{"a":1,"b":2}`,
    95  		},
    96  		{
    97  			src:      `{"a":0}`,
    98  			dst:      `{"a":1}`,
    99  			expected: `{"a":0}`,
   100  		},
   101  		{
   102  			src:      `{"a":{       "y":2}}`,
   103  			dst:      `{"a":{"x":1       }}`,
   104  			expected: `{"a":{"x":1, "y":2}}`,
   105  		},
   106  		{
   107  			src:      `{"a":{"x":2}}`,
   108  			dst:      `{"a":{"x":1}}`,
   109  			expected: `{"a":{"x":2}}`,
   110  		},
   111  		{
   112  			src:      `{"a":{       "y":7, "z":8}}`,
   113  			dst:      `{"a":{"x":1, "y":2       }}`,
   114  			expected: `{"a":{"x":1, "y":7, "z":8}}`,
   115  		},
   116  		{
   117  			src:      `{"1": { "b":1, "2": { "3": {         "b":3, "n":[1,2]} }        }}`,
   118  			dst:      `{"1": {        "2": { "3": {"a":"A",        "n":"xxx"} }, "a":3 }}`,
   119  			expected: `{"1": { "b":1, "2": { "3": {"a":"A", "b":3, "n":[1,2]} }, "a":3 }}`,
   120  		},
   121  	} {
   122  		var dst map[string]interface{}
   123  		if err := f.DecodeJson([]byte(tuple.dst), &dst); err != nil {
   124  			t.Error(err)
   125  			continue
   126  		}
   127  
   128  		var src map[string]interface{}
   129  		if err := f.DecodeJson([]byte(tuple.src), &src); err != nil {
   130  			t.Error(err)
   131  			continue
   132  		}
   133  
   134  		var expected map[string]interface{}
   135  		if err := f.DecodeJson([]byte(tuple.expected), &expected); err != nil {
   136  			t.Error(err)
   137  			continue
   138  		}
   139  
   140  		got := f.MapMerge(dst, src)
   141  		assertMapMerge(t, expected, got)
   142  	}
   143  }
   144  
   145  func assertMapMerge(t *testing.T, expected, got map[string]interface{}) {
   146  	expectedBuf, err := f.EncodeJson(expected)
   147  	if err != nil {
   148  		t.Error(err)
   149  		return
   150  	}
   151  	gotBuf, err := f.EncodeJson(got)
   152  	if err != nil {
   153  		t.Error(err)
   154  		return
   155  	}
   156  	if bytes.Compare(expectedBuf, gotBuf) != 0 {
   157  		t.Errorf("expected %s, got %s", string(expectedBuf), string(gotBuf))
   158  		return
   159  	}
   160  }