github.com/matrixorigin/matrixone@v1.2.0/pkg/pb/timestamp/timestamp_test.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package timestamp
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestTimestampIsEmpty(t *testing.T) {
    26  	tests := []struct {
    27  		ts      Timestamp
    28  		isEmpty bool
    29  	}{
    30  		{Timestamp{}, true},
    31  		{Timestamp{PhysicalTime: 1}, false},
    32  		{Timestamp{LogicalTime: 1}, false},
    33  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
    34  	}
    35  
    36  	for _, tt := range tests {
    37  		result := tt.ts.IsEmpty()
    38  		assert.Equal(t, tt.isEmpty, result)
    39  	}
    40  }
    41  
    42  func TestTimestampToStdTime(t *testing.T) {
    43  	now := time.Now().UTC()
    44  	ts := Timestamp{PhysicalTime: now.UnixNano()}
    45  	result := ts.ToStdTime()
    46  	assert.Equal(t, now, result)
    47  }
    48  
    49  func TestTimestampEqual(t *testing.T) {
    50  	tests := []struct {
    51  		lhs   Timestamp
    52  		rhs   Timestamp
    53  		equal bool
    54  	}{
    55  		{Timestamp{}, Timestamp{}, true},
    56  
    57  		{Timestamp{}, Timestamp{PhysicalTime: 1}, false},
    58  		{Timestamp{PhysicalTime: 1}, Timestamp{}, false},
    59  
    60  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 1}, true},
    61  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 2}, false},
    62  		{Timestamp{PhysicalTime: 2}, Timestamp{PhysicalTime: 1}, false},
    63  
    64  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 1}, true},
    65  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 2}, false},
    66  		{Timestamp{LogicalTime: 2}, Timestamp{LogicalTime: 1}, false},
    67  
    68  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 1}, false},
    69  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 1}, false},
    70  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 2}, false},
    71  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 2}, false},
    72  
    73  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, true},
    74  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 1}, false},
    75  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 2}, false},
    76  		{Timestamp{PhysicalTime: 2, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
    77  		{Timestamp{PhysicalTime: 1, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
    78  		{Timestamp{PhysicalTime: 2, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		assert.Equal(t, tt.equal, tt.lhs.Equal(tt.rhs))
    83  	}
    84  }
    85  
    86  func TestTimestampLess(t *testing.T) {
    87  	tests := []struct {
    88  		lhs  Timestamp
    89  		rhs  Timestamp
    90  		less bool
    91  	}{
    92  		{Timestamp{}, Timestamp{}, false},
    93  		{Timestamp{}, Timestamp{PhysicalTime: 1}, true},
    94  		{Timestamp{PhysicalTime: 1}, Timestamp{}, false},
    95  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 1}, false},
    96  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 2}, true},
    97  		{Timestamp{PhysicalTime: 2}, Timestamp{PhysicalTime: 1}, false},
    98  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 1}, false},
    99  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 2}, true},
   100  		{Timestamp{LogicalTime: 2}, Timestamp{LogicalTime: 1}, false},
   101  
   102  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 1}, false},
   103  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 1}, true},
   104  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 2}, false},
   105  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 2}, true},
   106  
   107  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
   108  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 1}, true},
   109  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 2}, true},
   110  		{Timestamp{PhysicalTime: 2, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
   111  		{Timestamp{PhysicalTime: 1, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
   112  		{Timestamp{PhysicalTime: 2, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
   113  	}
   114  
   115  	for _, tt := range tests {
   116  		assert.Equal(t, tt.less, tt.lhs.Less(tt.rhs))
   117  	}
   118  }
   119  
   120  func TestTimestampGreater(t *testing.T) {
   121  	tests := []struct {
   122  		lhs     Timestamp
   123  		rhs     Timestamp
   124  		greater bool
   125  	}{
   126  		{Timestamp{}, Timestamp{}, false},
   127  		{Timestamp{}, Timestamp{PhysicalTime: 1}, false},
   128  		{Timestamp{PhysicalTime: 1}, Timestamp{}, true},
   129  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 1}, false},
   130  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 2}, false},
   131  		{Timestamp{PhysicalTime: 2}, Timestamp{PhysicalTime: 1}, true},
   132  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 1}, false},
   133  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 2}, false},
   134  		{Timestamp{LogicalTime: 2}, Timestamp{LogicalTime: 1}, true},
   135  
   136  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 1}, true},
   137  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 1}, false},
   138  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 2}, true},
   139  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 2}, false},
   140  
   141  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
   142  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 1}, false},
   143  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 2}, false},
   144  		{Timestamp{PhysicalTime: 2, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, true},
   145  		{Timestamp{PhysicalTime: 1, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, true},
   146  		{Timestamp{PhysicalTime: 2, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, true},
   147  	}
   148  
   149  	for _, tt := range tests {
   150  		assert.Equal(t, tt.greater, tt.lhs.Greater(tt.rhs))
   151  	}
   152  }
   153  
   154  func TestTimestampLessEq(t *testing.T) {
   155  	tests := []struct {
   156  		lhs    Timestamp
   157  		rhs    Timestamp
   158  		lessEq bool
   159  	}{
   160  		{Timestamp{}, Timestamp{}, true},
   161  		{Timestamp{}, Timestamp{PhysicalTime: 1}, true},
   162  		{Timestamp{PhysicalTime: 1}, Timestamp{}, false},
   163  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 1}, true},
   164  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 2}, true},
   165  		{Timestamp{PhysicalTime: 2}, Timestamp{PhysicalTime: 1}, false},
   166  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 1}, true},
   167  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 2}, true},
   168  		{Timestamp{LogicalTime: 2}, Timestamp{LogicalTime: 1}, false},
   169  
   170  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 1}, false},
   171  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 1}, true},
   172  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 2}, false},
   173  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 2}, true},
   174  
   175  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, true},
   176  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 1}, true},
   177  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 2}, true},
   178  		{Timestamp{PhysicalTime: 2, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
   179  		{Timestamp{PhysicalTime: 1, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
   180  		{Timestamp{PhysicalTime: 2, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, false},
   181  	}
   182  
   183  	for _, tt := range tests {
   184  		assert.Equal(t, tt.lessEq, tt.lhs.LessEq(tt.rhs))
   185  	}
   186  }
   187  
   188  func TestTimestampGreaterEq(t *testing.T) {
   189  	tests := []struct {
   190  		lhs       Timestamp
   191  		rhs       Timestamp
   192  		greaterEq bool
   193  	}{
   194  		{Timestamp{}, Timestamp{}, true},
   195  		{Timestamp{}, Timestamp{PhysicalTime: 1}, false},
   196  		{Timestamp{PhysicalTime: 1}, Timestamp{}, true},
   197  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 1}, true},
   198  		{Timestamp{PhysicalTime: 1}, Timestamp{PhysicalTime: 2}, false},
   199  		{Timestamp{PhysicalTime: 2}, Timestamp{PhysicalTime: 1}, true},
   200  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 1}, true},
   201  		{Timestamp{LogicalTime: 1}, Timestamp{LogicalTime: 2}, false},
   202  		{Timestamp{LogicalTime: 2}, Timestamp{LogicalTime: 1}, true},
   203  
   204  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 1}, true},
   205  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 1}, false},
   206  		{Timestamp{PhysicalTime: 1}, Timestamp{LogicalTime: 2}, true},
   207  		{Timestamp{LogicalTime: 1}, Timestamp{PhysicalTime: 2}, false},
   208  
   209  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, true},
   210  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 1}, false},
   211  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 2, LogicalTime: 2}, false},
   212  		{Timestamp{PhysicalTime: 2, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, true},
   213  		{Timestamp{PhysicalTime: 1, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, true},
   214  		{Timestamp{PhysicalTime: 2, LogicalTime: 2}, Timestamp{PhysicalTime: 1, LogicalTime: 1}, true},
   215  	}
   216  
   217  	for _, tt := range tests {
   218  		assert.Equal(t, tt.greaterEq, tt.lhs.GreaterEq(tt.rhs))
   219  	}
   220  }
   221  
   222  func TestTimestampNext(t *testing.T) {
   223  	tests := []struct {
   224  		ts     Timestamp
   225  		result Timestamp
   226  	}{
   227  		{Timestamp{}, Timestamp{LogicalTime: 1}},
   228  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 2}},
   229  		{Timestamp{PhysicalTime: 1234, LogicalTime: 234}, Timestamp{PhysicalTime: 1234, LogicalTime: 235}},
   230  		{Timestamp{PhysicalTime: 123, LogicalTime: math.MaxUint32}, Timestamp{PhysicalTime: 124}},
   231  	}
   232  
   233  	for _, tt := range tests {
   234  		assert.Equal(t, tt.result, tt.ts.Next())
   235  	}
   236  }
   237  
   238  func TestTimestampPrev(t *testing.T) {
   239  	tests := []struct {
   240  		ts     Timestamp
   241  		result Timestamp
   242  	}{
   243  		{Timestamp{PhysicalTime: 1, LogicalTime: 1}, Timestamp{PhysicalTime: 1, LogicalTime: 0}},
   244  		{Timestamp{PhysicalTime: 1234, LogicalTime: 234}, Timestamp{PhysicalTime: 1234, LogicalTime: 233}},
   245  		{Timestamp{PhysicalTime: 123, LogicalTime: 0}, Timestamp{PhysicalTime: 122, LogicalTime: math.MaxUint32}},
   246  	}
   247  
   248  	for _, tt := range tests {
   249  		assert.Equal(t, tt.result, tt.ts.Prev())
   250  	}
   251  }
   252  
   253  func TestParseTimestamp(t *testing.T) {
   254  	tests := []struct {
   255  		value  string
   256  		result Timestamp
   257  		err    bool
   258  	}{
   259  		{"", Timestamp{}, true},
   260  		{"1", Timestamp{}, true},
   261  		{"a", Timestamp{}, true},
   262  		{"1-", Timestamp{}, true},
   263  		{"1-a", Timestamp{}, true},
   264  		{"1-2", Timestamp{PhysicalTime: 1, LogicalTime: 2}, false},
   265  	}
   266  
   267  	for _, tt := range tests {
   268  		ts, err := ParseTimestamp(tt.value)
   269  		if !tt.err {
   270  			assert.NoError(t, err)
   271  			assert.Equal(t, tt.result, ts)
   272  			continue
   273  		}
   274  		assert.Error(t, err)
   275  	}
   276  }