github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/utils/compare_test.go (about)

     1  // Copyright (c) 2020 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package utils
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type ExampleStruct struct {
    15  	X int
    16  	Y string
    17  }
    18  
    19  func TestCompareStruct(t *testing.T) {
    20  	assert := assert.New(t)
    21  
    22  	var testStruct1, testStruct2 ExampleStruct
    23  
    24  	testStruct1 = ExampleStruct{1, "test"}
    25  	testStruct2 = ExampleStruct{1, "test"}
    26  	result := DeepCompare(testStruct1, testStruct2)
    27  	assert.True(result)
    28  
    29  	testStruct2 = ExampleStruct{2, "test"}
    30  	result = DeepCompare(testStruct1, testStruct2)
    31  	assert.False(result)
    32  }
    33  
    34  func TestCompareArray(t *testing.T) {
    35  	assert := assert.New(t)
    36  
    37  	a1 := [2]string{"test", "array"}
    38  	a2 := [2]string{"test", "array"}
    39  	result := DeepCompare(a1, a2)
    40  	assert.True(result)
    41  
    42  	a2 = [2]string{"test", "compare"}
    43  	result = DeepCompare(a1, a2)
    44  	assert.False(result)
    45  
    46  	a3 := [3]string{"test", "array", "compare"}
    47  	result = DeepCompare(a1, a3)
    48  	assert.False(result)
    49  }
    50  
    51  func TestCompareSlice(t *testing.T) {
    52  	assert := assert.New(t)
    53  
    54  	s1 := []int{1, 2, 3}
    55  	s2 := []int{1, 2, 3}
    56  	result := DeepCompare(s1, s2)
    57  	assert.True(result)
    58  
    59  	s2 = []int{1, 2, 4}
    60  	result = DeepCompare(s1, s2)
    61  	assert.False(result)
    62  
    63  	s2 = []int{1, 2, 3, 4}
    64  	result = DeepCompare(s1, s2)
    65  	assert.False(result)
    66  }
    67  
    68  func TestCompareMap(t *testing.T) {
    69  	assert := assert.New(t)
    70  
    71  	m1 := make(map[string]int)
    72  	m1["a"] = 1
    73  	m2 := make(map[string]int)
    74  	m2["a"] = 1
    75  	result := DeepCompare(m1, m2)
    76  	assert.True(result)
    77  
    78  	m1["b"] = 2
    79  	result = DeepCompare(m1, m2)
    80  	assert.False(result)
    81  
    82  	m2["b"] = 3
    83  	result = DeepCompare(m1, m2)
    84  	assert.False(result)
    85  }
    86  
    87  func TestDeepCompareValueFailure(t *testing.T) {
    88  	assert := assert.New(t)
    89  
    90  	a := [2]string{"test", "array"}
    91  	s := []string{"test", "array"}
    92  	result := DeepCompare(a, s)
    93  	assert.False(result)
    94  }