github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/util/testutil/testutil.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package testutil
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/insionng/yougam/libraries/pingcap/check"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    21  )
    22  
    23  // CompareUnorderedStringSlice compare two string slices.
    24  // If a and b is exactly the same except the order, it returns true.
    25  // In otherwise return false.
    26  func CompareUnorderedStringSlice(a []string, b []string) bool {
    27  	if a == nil && b == nil {
    28  		return true
    29  	}
    30  	if a == nil || b == nil {
    31  		return false
    32  	}
    33  	if len(a) != len(b) {
    34  		return false
    35  	}
    36  	m := make(map[string]int, len(a))
    37  	for _, i := range a {
    38  		_, ok := m[i]
    39  		if !ok {
    40  			m[i] = 1
    41  		} else {
    42  			m[i]++
    43  		}
    44  	}
    45  
    46  	for _, i := range b {
    47  		_, ok := m[i]
    48  		if !ok {
    49  			return false
    50  		}
    51  		m[i]--
    52  		if m[i] == 0 {
    53  			delete(m, i)
    54  		}
    55  	}
    56  	return len(m) == 0
    57  }
    58  
    59  // DatumEquals checker.
    60  type datumEqualsChecker struct {
    61  	*check.CheckerInfo
    62  }
    63  
    64  // DatumEquals checker verifies that the obtained value is equal to
    65  // the expected value.
    66  // For example:
    67  //     c.Assert(value, DatumEquals, NewDatum(42))
    68  var DatumEquals check.Checker = &datumEqualsChecker{
    69  	&check.CheckerInfo{Name: "DatumEquals", Params: []string{"obtained", "expected"}},
    70  }
    71  
    72  func (checker *datumEqualsChecker) Check(params []interface{}, names []string) (result bool, error string) {
    73  	defer func() {
    74  		if v := recover(); v != nil {
    75  			result = false
    76  			error = fmt.Sprint(v)
    77  		}
    78  	}()
    79  	paramFirst, ok := params[0].(types.Datum)
    80  	if !ok {
    81  		panic("the first param should be datum")
    82  	}
    83  	paramSecond, ok := params[1].(types.Datum)
    84  	if !ok {
    85  		panic("the second param should be datum")
    86  	}
    87  
    88  	res, err := paramFirst.CompareDatum(paramSecond)
    89  	if err != nil {
    90  		panic(err)
    91  	}
    92  	return res == 0, ""
    93  }