github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/ekv/utils_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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 ekv
    15  
    16  import (
    17  	"context"
    18  	"strconv"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  )
    22  
    23  var _ = Suite(testUtilsSuite{})
    24  
    25  type testUtilsSuite struct {
    26  }
    27  
    28  func (s testUtilsSuite) TestIncInt64(c *C) {
    29  	mb := newMemDB()
    30  	key := Key("key")
    31  	v, err := IncInt64(mb, key, 1)
    32  	c.Check(err, IsNil)
    33  	c.Check(v, Equals, int64(1))
    34  	v, err = IncInt64(mb, key, 10)
    35  	c.Check(err, IsNil)
    36  	c.Check(v, Equals, int64(11))
    37  
    38  	err = mb.Set(key, []byte("not int"))
    39  	c.Check(err, IsNil)
    40  	_, err = IncInt64(mb, key, 1)
    41  	c.Check(err, NotNil)
    42  
    43  	// test int overflow
    44  	maxUint32 := int64(^uint32(0))
    45  	err = mb.Set(key, []byte(strconv.FormatInt(maxUint32, 10)))
    46  	c.Check(err, IsNil)
    47  	v, err = IncInt64(mb, key, 1)
    48  	c.Check(err, IsNil)
    49  	c.Check(v, Equals, maxUint32+1)
    50  
    51  }
    52  
    53  func (s testUtilsSuite) TestGetInt64(c *C) {
    54  	mb := newMemDB()
    55  	key := Key("key")
    56  	v, err := GetInt64(context.TODO(), mb, key)
    57  	c.Check(v, Equals, int64(0))
    58  	c.Check(err, IsNil)
    59  
    60  	_, err = IncInt64(mb, key, 15)
    61  	c.Check(err, IsNil)
    62  	v, err = GetInt64(context.TODO(), mb, key)
    63  	c.Check(v, Equals, int64(15))
    64  	c.Check(err, IsNil)
    65  }