github.com/igggame/nebulas-go@v2.1.0+incompatible/core/compatibility_test.go (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package core
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestStringCompare(t *testing.T) {
    28  	type test struct {
    29  		name, a, b string
    30  		cmp        int
    31  	}
    32  
    33  	tests := []test{
    34  		{
    35  			name: "1",
    36  			a:    "1.0.1",
    37  			b:    "1.0.2",
    38  			cmp:  -1,
    39  		},
    40  		{
    41  			name: "2",
    42  			a:    "1.0.1",
    43  			b:    "1.0.10",
    44  			cmp:  -1,
    45  		},
    46  		{
    47  			name: "3",
    48  			a:    "1.0.10",
    49  			b:    "1.0.2",
    50  			cmp:  1,
    51  		},
    52  		{
    53  			name: "4",
    54  			a:    "10.0.1",
    55  			b:    "1.0.2",
    56  			cmp:  1,
    57  		},
    58  		{
    59  			name: "5",
    60  			a:    "10.0.1",
    61  			b:    "2.0.2",
    62  			cmp:  1,
    63  		},
    64  		{
    65  			name: "6",
    66  			a:    "10.0.1",
    67  			b:    "10.90.1",
    68  			cmp:  -1,
    69  		},
    70  	}
    71  
    72  	for _, tt := range tests {
    73  		t.Run(tt.name, func(t *testing.T) {
    74  			va, _ := parseVersion(tt.a)
    75  			vb, _ := parseVersion(tt.b)
    76  			r := compareVersion(va, vb)
    77  			assert.Equal(t, tt.a, va.String())
    78  			assert.Equal(t, tt.b, vb.String())
    79  			assert.Equal(t, tt.cmp, r, "case "+tt.name+" not equal")
    80  		})
    81  	}
    82  }