Add specialized increment and decrement methods

This commit is contained in:
Patrick Mylund Nielsen 2013-04-18 14:24:30 -04:00
parent b5601e904d
commit d5d03c28d4
3 changed files with 1584 additions and 125 deletions

136
README
View File

@ -119,13 +119,73 @@ func (c *Cache) Increment(k string, n int64) error
Increment an item of type int, int8, int16, int32, int64, uintptr, uint, Increment an item of type int, int8, int16, int32, int64, uintptr, uint,
uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
item's value is not an integer, if it was not found, or if it is not item's value is not an integer, if it was not found, or if it is not
possible to increment it by n. possible to increment it by n. To retrieve the incremented value, use one
of the specialized methods, e.g. IncrementInt64.
func (c *Cache) IncrementFloat(k string, n float64) error func (c *Cache) IncrementFloat(k string, n float64) error
Increment an item of type float32 or float64 by n. Returns an error if the Increment an item of type float32 or float64 by n. Returns an error if the
item's value is not floating point, if it was not found, or if it is not item's value is not floating point, if it was not found, or if it is not
possible to increment it by n. Pass a negative number to decrement the possible to increment it by n.
value.
func (c *Cache) IncrementInt(k string, n int) (int, error)
Increment an item of type int by n. Returns an error if the item's value
is not an int, or if it was not found. If there is no error, the incremented
value is returned.
func (c *Cache) IncrementInt8(k string, n int8) (int8, error)
Increment an item of type int8 by n. Returns an error if the item's value
is not an int8, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementInt16(k string, n int16) (int16, error)
Increment an item of type int16 by n. Returns an error if the item's value
is not an int16, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementInt32(k string, n int32) (int32, error)
Increment an item of type int32 by n. Returns an error if the item's value
is not an int32, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementInt64(k string, n int64) (int64, error)
Increment an item of type int64 by n. Returns an error if the item's value
is not an int64, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementUint(k string, n uint) (uint, error)
Increment an item of type uint by n. Returns an error if the item's value
is not an uint, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementUint8(k string, n uint8) (uint8, error)
Increment an item of type uint8 by n. Returns an error if the item's value
is not an uint8, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementUint16(k string, n uint16) (uint16, error)
Increment an item of type uint16 by n. Returns an error if the item's value
is not an uint16, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementUint32(k string, n uint32) (uint32, error)
Increment an item of type uint32 by n. Returns an error if the item's value
is not an uint32, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementUint64(k string, n uint64) (uint64, error)
Increment an item of type uint64 by n. Returns an error if the item's value
is not an uint64, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementFloat32(k string, n float32) (float32, error)
Increment an item of type float32 by n. Returns an error if the item's value
is not an float32, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) IncrementFloat64(k string, n float64) (float64, error)
Increment an item of type float64 by n. Returns an error if the item's value
is not an float64, or if it was not found. If there is no error, the
incremented value is returned.
func (c *Cache) Decrement(k string, n int64) error func (c *Cache) Decrement(k string, n int64) error
Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, Decrement an item of type int, int8, int16, int32, int64, uintptr, uint,
@ -133,6 +193,76 @@ func (c *Cache) Decrement(k string, n int64) error
item's value is not an integer, if it was not found, or if it is not item's value is not an integer, if it was not found, or if it is not
possible to decrement it by n. possible to decrement it by n.
func (c *Cache) DecrementFloat(k string, n float64) error
Decrement an item of type float32 or float64 by n. Returns an error if the
item's value is floating point, if it was not found, or if it is not
possible to decrement it by n.
func (c *Cache) DecrementInt(k string, n int) (int, error)
Decrement an item of type int by n. Returns an error if the item's value is
not an int, or if it was not found. If there is no error, the decremented
value is returned.
func (c *Cache) DecrementInt8(k string, n int8) (int8, error)
Decrement an item of type int8 by n. Returns an error if the item's value is
not an int8, or if it was not found. If there is no error, the decremented
value is returned.
func (c *Cache) DecrementInt16(k string, n int16) (int16, error)
Decrement an item of type int16 by n. Returns an error if the item's value
is not an int16, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementInt32(k string, n int32) (int32, error)
Decrement an item of type int32 by n. Returns an error if the item's value
is not an int32, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementInt64(k string, n int64) (int64, error)
Decrement an item of type int64 by n. Returns an error if the item's value
is not an int64, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementUint(k string, n uint) (uint, error)
Decrement an item of type uint by n. Returns an error if the item's value
is not an uint, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementUintptr(k string, n uintptr) (uintptr, error)
Decrement an item of type uintptr by n. Returns an error if the item's value
is not an uintptr, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementUint8(k string, n uint8) (uint8, error)
Decrement an item of type uint8 by n. Returns an error if the item's value
is not an uint8, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementUint16(k string, n uint16) (uint16, error)
Decrement an item of type uint16 by n. Returns an error if the item's value
is not an uint16, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementUint32(k string, n uint32) (uint32, error)
Decrement an item of type uint32 by n. Returns an error if the item's value
is not an uint32, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementUint64(k string, n uint64) (uint64, error)
Decrement an item of type uint64 by n. Returns an error if the item's value
is not an uint64, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementFloat32(k string, n float32) (float32, error)
Decrement an item of type float32 by n. Returns an error if the item's value
is not an float32, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) DecrementFloat64(k string, n float64) (float64, error)
Decrement an item of type float64 by n. Returns an error if the item's value
is not an float64, or if it was not found. If there is no error, the
decremented value is returned.
func (c *Cache) Delete(k string) func (c *Cache) Delete(k string)
Delete an item from the cache. Does nothing if the key does not exist in the Delete an item from the cache. Does nothing if the key does not exist in the
cache. cache.

661
cache.go
View File

@ -18,8 +18,35 @@ type unexportedInterface interface {
Replace(string, interface{}, time.Duration) error Replace(string, interface{}, time.Duration) error
Get(string) (interface{}, bool) Get(string) (interface{}, bool)
Increment(string, int64) error Increment(string, int64) error
IncrementInt(string, int) (int, error)
IncrementInt8(string, int8) (int8, error)
IncrementInt16(string, int16) (int16, error)
IncrementInt32(string, int32) (int32, error)
IncrementInt64(string, int64) (int64, error)
IncrementUint(string, uint) (uint, error)
IncrementUintptr(string, uintptr) (uintptr, error)
IncrementUint8(string, uint8) (uint8, error)
IncrementUint16(string, uint16) (uint16, error)
IncrementUint32(string, uint32) (uint32, error)
IncrementUint64(string, uint64) (uint64, error)
IncrementFloat(string, float64) error IncrementFloat(string, float64) error
IncrementFloat32(string, float32) (float32, error)
IncrementFloat64(string, float64) (float64, error)
Decrement(string, int64) error Decrement(string, int64) error
DecrementInt(string, int) (int, error)
DecrementInt8(string, int8) (int8, error)
DecrementInt16(string, int16) (int16, error)
DecrementInt32(string, int32) (int32, error)
DecrementInt64(string, int64) (int64, error)
DecrementUint(string, uint) (uint, error)
DecrementUintptr(string, uintptr) (uintptr, error)
DecrementUint8(string, uint8) (uint8, error)
DecrementUint16(string, uint16) (uint16, error)
DecrementUint32(string, uint32) (uint32, error)
DecrementUint64(string, uint64) (uint64, error)
DecrementFloat(string, float64) error
DecrementFloat32(string, float32) (float32, error)
DecrementFloat64(string, float64) (float64, error)
Delete(string) Delete(string)
DeleteExpired() DeleteExpired()
Flush() Flush()
@ -87,7 +114,7 @@ func (c *cache) Add(k string, x interface{}, d time.Duration) error {
_, found := c.get(k) _, found := c.get(k)
if found { if found {
c.Unlock() c.Unlock()
return fmt.Errorf("item %s already exists", k) return fmt.Errorf("Item %s already exists", k)
} }
c.set(k, x, d) c.set(k, x, d)
c.Unlock() c.Unlock()
@ -101,7 +128,7 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
_, found := c.get(k) _, found := c.get(k)
if !found { if !found {
c.Unlock() c.Unlock()
return fmt.Errorf("item %s doesn't exist", k) return fmt.Errorf("Item %s doesn't exist", k)
} }
c.set(k, x, d) c.set(k, x, d)
c.Unlock() c.Unlock()
@ -129,40 +156,17 @@ func (c *cache) get(k string) (interface{}, bool) {
return item.Object, true return item.Object, true
} }
// Increment an item of type float32 or float64 by n. Returns an error if the
// item's value is not floating point, if it was not found, or if it is not
// possible to increment it by n. Pass a negative number to decrement the
// value.
func (c *cache) IncrementFloat(k string, n float64) error {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return fmt.Errorf("item not found")
}
switch v.Object.(type) {
case float32:
v.Object = v.Object.(float32) + float32(n)
case float64:
v.Object = v.Object.(float64) + n
default:
c.Unlock()
return fmt.Errorf("The value for %s does not have type float32 or float64", k)
}
c.Unlock()
return nil
}
// Increment an item of type int, int8, int16, int32, int64, uintptr, uint, // Increment an item of type int, int8, int16, int32, int64, uintptr, uint,
// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the // uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
// item's value is not an integer, if it was not found, or if it is not // item's value is not an integer, if it was not found, or if it is not
// possible to increment it by n. // possible to increment it by n. To retrieve the incremented value, use one
// of the specialized methods, e.g. IncrementInt64.
func (c *cache) Increment(k string, n int64) error { func (c *cache) Increment(k string, n int64) error {
c.Lock() c.Lock()
v, found := c.items[k] v, found := c.items[k]
if !found || v.Expired() { if !found || v.Expired() {
c.Unlock() c.Unlock()
return fmt.Errorf("item not found") return fmt.Errorf("Item %s not found", k)
} }
switch v.Object.(type) { switch v.Object.(type) {
case int: case int:
@ -199,10 +203,309 @@ func (c *cache) Increment(k string, n int64) error {
return nil return nil
} }
// Increment an item of type float32 or float64 by n. Returns an error if the
// item's value is not floating point, if it was not found, or if it is not
// possible to increment it by n. Pass a negative number to decrement the
// value. To retrieve the incremented value, use one of the specialized methods,
// e.g. IncrementFloat64.
func (c *cache) IncrementFloat(k string, n float64) error {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return fmt.Errorf("Item %s not found", k)
}
switch v.Object.(type) {
case float32:
v.Object = v.Object.(float32) + float32(n)
case float64:
v.Object = v.Object.(float64) + n
default:
c.Unlock()
return fmt.Errorf("The value for %s does not have type float32 or float64", k)
}
c.Unlock()
return nil
}
// Increment an item of type int by n. Returns an error if the item's value is
// not an int, or if it was not found. If there is no error, the incremented
// value is returned.
func (c *cache) IncrementInt(k string, n int) (int, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type int8 by n. Returns an error if the item's value is
// not an int8, or if it was not found. If there is no error, the incremented
// value is returned.
func (c *cache) IncrementInt8(k string, n int8) (int8, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int8)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int8", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type int16 by n. Returns an error if the item's value is
// not an int16, or if it was not found. If there is no error, the incremented
// value is returned.
func (c *cache) IncrementInt16(k string, n int16) (int16, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int16)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int16", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type int32 by n. Returns an error if the item's value is
// not an int32, or if it was not found. If there is no error, the incremented
// value is returned.
func (c *cache) IncrementInt32(k string, n int32) (int32, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int32)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int32", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type int64 by n. Returns an error if the item's value is
// not an int64, or if it was not found. If there is no error, the incremented
// value is returned.
func (c *cache) IncrementInt64(k string, n int64) (int64, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int64)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int64", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type uint by n. Returns an error if the item's value is
// not an uint, or if it was not found. If there is no error, the incremented
// value is returned.
func (c *cache) IncrementUint(k string, n uint) (uint, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type uintptr by n. Returns an error if the item's value
// is not an uintptr, or if it was not found. If there is no error, the
// incremented value is returned.
func (c *cache) IncrementUintptr(k string, n uintptr) (uintptr, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uintptr)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uintptr", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type uint8 by n. Returns an error if the item's value
// is not an uint8, or if it was not found. If there is no error, the
// incremented value is returned.
func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint8)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint8", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type uint16 by n. Returns an error if the item's value
// is not an uint16, or if it was not found. If there is no error, the
// incremented value is returned.
func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint16)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint16", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type uint32 by n. Returns an error if the item's value
// is not an uint32, or if it was not found. If there is no error, the
// incremented value is returned.
func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint32)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint32", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type uint64 by n. Returns an error if the item's value
// is not an uint64, or if it was not found. If there is no error, the
// incremented value is returned.
func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint64)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint64", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type float32 by n. Returns an error if the item's value
// is not an float32, or if it was not found. If there is no error, the
// incremented value is returned.
func (c *cache) IncrementFloat32(k string, n float32) (float32, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(float32)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an float32", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Increment an item of type float64 by n. Returns an error if the item's value
// is not an float64, or if it was not found. If there is no error, the
// incremented value is returned.
func (c *cache) IncrementFloat64(k string, n float64) (float64, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(float64)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an float64", k)
}
nv := rv + n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, // Decrement an item of type int, int8, int16, int32, int64, uintptr, uint,
// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the // uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
// item's value is not an integer, if it was not found, or if it is not // item's value is not an integer, if it was not found, or if it is not
// possible to decrement it by n. // possible to decrement it by n. To retrieve the decremented value, use one
// of the specialized methods, e.g. DecrementInt64.
func (c *cache) Decrement(k string, n int64) error { func (c *cache) Decrement(k string, n int64) error {
// TODO: Implement Increment and Decrement more cleanly. // TODO: Implement Increment and Decrement more cleanly.
// (Cannot do Increment(k, n*-1) for uints.) // (Cannot do Increment(k, n*-1) for uints.)
@ -210,7 +513,7 @@ func (c *cache) Decrement(k string, n int64) error {
v, found := c.items[k] v, found := c.items[k]
if !found || v.Expired() { if !found || v.Expired() {
c.Unlock() c.Unlock()
return fmt.Errorf("item not found") return fmt.Errorf("Item not found")
} }
switch v.Object.(type) { switch v.Object.(type) {
case int: case int:
@ -247,6 +550,304 @@ func (c *cache) Decrement(k string, n int64) error {
return nil return nil
} }
// Decrement an item of type float32 or float64 by n. Returns an error if the
// item's value is not floating point, if it was not found, or if it is not
// possible to decrement it by n. Pass a negative number to decrement the
// value. To retrieve the decremented value, use one of the specialized methods,
// e.g. DecrementFloat64.
func (c *cache) DecrementFloat(k string, n float64) error {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return fmt.Errorf("Item %s not found", k)
}
switch v.Object.(type) {
case float32:
v.Object = v.Object.(float32) - float32(n)
case float64:
v.Object = v.Object.(float64) - n
default:
c.Unlock()
return fmt.Errorf("The value for %s does not have type float32 or float64", k)
}
c.Unlock()
return nil
}
// Decrement an item of type int by n. Returns an error if the item's value is
// not an int, or if it was not found. If there is no error, the decremented
// value is returned.
func (c *cache) DecrementInt(k string, n int) (int, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type int8 by n. Returns an error if the item's value is
// not an int8, or if it was not found. If there is no error, the decremented
// value is returned.
func (c *cache) DecrementInt8(k string, n int8) (int8, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int8)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int8", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type int16 by n. Returns an error if the item's value is
// not an int16, or if it was not found. If there is no error, the decremented
// value is returned.
func (c *cache) DecrementInt16(k string, n int16) (int16, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int16)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int16", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type int32 by n. Returns an error if the item's value is
// not an int32, or if it was not found. If there is no error, the decremented
// value is returned.
func (c *cache) DecrementInt32(k string, n int32) (int32, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int32)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int32", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type int64 by n. Returns an error if the item's value is
// not an int64, or if it was not found. If there is no error, the decremented
// value is returned.
func (c *cache) DecrementInt64(k string, n int64) (int64, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(int64)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an int64", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type uint by n. Returns an error if the item's value is
// not an uint, or if it was not found. If there is no error, the decremented
// value is returned.
func (c *cache) DecrementUint(k string, n uint) (uint, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type uintptr by n. Returns an error if the item's value
// is not an uintptr, or if it was not found. If there is no error, the
// decremented value is returned.
func (c *cache) DecrementUintptr(k string, n uintptr) (uintptr, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uintptr)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uintptr", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type uint8 by n. Returns an error if the item's value is
// not an uint8, or if it was not found. If there is no error, the decremented
// value is returned.
func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint8)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint8", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type uint16 by n. Returns an error if the item's value
// is not an uint16, or if it was not found. If there is no error, the
// decremented value is returned.
func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint16)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint16", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type uint32 by n. Returns an error if the item's value
// is not an uint32, or if it was not found. If there is no error, the
// decremented value is returned.
func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint32)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint32", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type uint64 by n. Returns an error if the item's value
// is not an uint64, or if it was not found. If there is no error, the
// decremented value is returned.
func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(uint64)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an uint64", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type float32 by n. Returns an error if the item's value
// is not an float32, or if it was not found. If there is no error, the
// decremented value is returned.
func (c *cache) DecrementFloat32(k string, n float32) (float32, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(float32)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an float32", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Decrement an item of type float64 by n. Returns an error if the item's value
// is not an float64, or if it was not found. If there is no error, the
// decremented value is returned.
func (c *cache) DecrementFloat64(k string, n float64) (float64, error) {
c.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.Unlock()
return 0, fmt.Errorf("Item %s not found", k)
}
rv, ok := v.Object.(float64)
if !ok {
c.Unlock()
return 0, fmt.Errorf("The value for %s is not an float64", k)
}
nv := rv - n
v.Object = nv
c.Unlock()
return nv, nil
}
// Delete an item from the cache. Does nothing if the key is not in the cache. // Delete an item from the cache. Does nothing if the key is not in the cache.
func (c *cache) Delete(k string) { func (c *cache) Delete(k string) {
c.Lock() c.Lock()

View File

@ -126,14 +126,93 @@ func TestStorePointerToStruct(t *testing.T) {
} }
} }
func TestIncrementUint(t *testing.T) { func TestIncrementWithInt(t *testing.T) {
tc := New(0, 0)
tc.Set("tint", 1, 0)
err := tc.Increment("tint", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint")
if !found {
t.Error("tint was not found")
}
if x.(int) != 3 {
t.Error("tint is not 3:", x)
}
}
func TestIncrementWithInt8(t *testing.T) {
tc := New(0, 0)
tc.Set("tint8", int8(1), 0)
err := tc.Increment("tint8", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint8")
if !found {
t.Error("tint8 was not found")
}
if x.(int8) != 3 {
t.Error("tint8 is not 3:", x)
}
}
func TestIncrementWithInt16(t *testing.T) {
tc := New(0, 0)
tc.Set("tint16", int16(1), 0)
err := tc.Increment("tint16", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint16")
if !found {
t.Error("tint16 was not found")
}
if x.(int16) != 3 {
t.Error("tint16 is not 3:", x)
}
}
func TestIncrementWithInt32(t *testing.T) {
tc := New(0, 0)
tc.Set("tint32", int32(1), 0)
err := tc.Increment("tint32", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint32")
if !found {
t.Error("tint32 was not found")
}
if x.(int32) != 3 {
t.Error("tint32 is not 3:", x)
}
}
func TestIncrementWithInt64(t *testing.T) {
tc := New(0, 0)
tc.Set("tint64", int64(1), 0)
err := tc.Increment("tint64", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint64")
if !found {
t.Error("tint64 was not found")
}
if x.(int64) != 3 {
t.Error("tint64 is not 3:", x)
}
}
func TestIncrementWithUint(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
tc.Set("tuint", uint(1), 0) tc.Set("tuint", uint(1), 0)
err := tc.Increment("tuint", 2) err := tc.Increment("tuint", 2)
if err != nil { if err != nil {
t.Error("Error incrementing:", err) t.Error("Error incrementing:", err)
} }
x, found := tc.Get("tuint") x, found := tc.Get("tuint")
if !found { if !found {
t.Error("tuint was not found") t.Error("tuint was not found")
@ -143,7 +222,7 @@ func TestIncrementUint(t *testing.T) {
} }
} }
func TestIncrementUintptr(t *testing.T) { func TestIncrementWithUintptr(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
tc.Set("tuintptr", uintptr(1), 0) tc.Set("tuintptr", uintptr(1), 0)
err := tc.Increment("tuintptr", 2) err := tc.Increment("tuintptr", 2)
@ -160,14 +239,13 @@ func TestIncrementUintptr(t *testing.T) {
} }
} }
func TestIncrementUint8(t *testing.T) { func TestIncrementWithUint8(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
tc.Set("tuint8", uint8(1), 0) tc.Set("tuint8", uint8(1), 0)
err := tc.Increment("tuint8", 2) err := tc.Increment("tuint8", 2)
if err != nil { if err != nil {
t.Error("Error incrementing:", err) t.Error("Error incrementing:", err)
} }
x, found := tc.Get("tuint8") x, found := tc.Get("tuint8")
if !found { if !found {
t.Error("tuint8 was not found") t.Error("tuint8 was not found")
@ -177,7 +255,7 @@ func TestIncrementUint8(t *testing.T) {
} }
} }
func TestIncrementUint16(t *testing.T) { func TestIncrementWithUint16(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
tc.Set("tuint16", uint16(1), 0) tc.Set("tuint16", uint16(1), 0)
err := tc.Increment("tuint16", 2) err := tc.Increment("tuint16", 2)
@ -194,14 +272,13 @@ func TestIncrementUint16(t *testing.T) {
} }
} }
func TestIncrementUint32(t *testing.T) { func TestIncrementWithUint32(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
tc.Set("tuint32", uint32(1), 0) tc.Set("tuint32", uint32(1), 0)
err := tc.Increment("tuint32", 2) err := tc.Increment("tuint32", 2)
if err != nil { if err != nil {
t.Error("Error incrementing:", err) t.Error("Error incrementing:", err)
} }
x, found := tc.Get("tuint32") x, found := tc.Get("tuint32")
if !found { if !found {
t.Error("tuint32 was not found") t.Error("tuint32 was not found")
@ -211,7 +288,7 @@ func TestIncrementUint32(t *testing.T) {
} }
} }
func TestIncrementUint64(t *testing.T) { func TestIncrementWithUint64(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
tc.Set("tuint64", uint64(1), 0) tc.Set("tuint64", uint64(1), 0)
err := tc.Increment("tuint64", 2) err := tc.Increment("tuint64", 2)
@ -228,87 +305,7 @@ func TestIncrementUint64(t *testing.T) {
} }
} }
func TestIncrementInt(t *testing.T) { func TestIncrementWithFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("tint", 1, 0)
err := tc.Increment("tint", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint")
if !found {
t.Error("tint was not found")
}
if x.(int) != 3 {
t.Error("tint is not 3:", x)
}
}
func TestIncrementInt8(t *testing.T) {
tc := New(0, 0)
tc.Set("tint8", int8(1), 0)
err := tc.Increment("tint8", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint8")
if !found {
t.Error("tint8 was not found")
}
if x.(int8) != 3 {
t.Error("tint8 is not 3:", x)
}
}
func TestIncrementInt16(t *testing.T) {
tc := New(0, 0)
tc.Set("tint16", int16(1), 0)
err := tc.Increment("tint16", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint16")
if !found {
t.Error("tint16 was not found")
}
if x.(int16) != 3 {
t.Error("tint16 is not 3:", x)
}
}
func TestIncrementInt32(t *testing.T) {
tc := New(0, 0)
tc.Set("tint32", int32(1), 0)
err := tc.Increment("tint32", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint32")
if !found {
t.Error("tint32 was not found")
}
if x.(int32) != 3 {
t.Error("tint32 is not 3:", x)
}
}
func TestIncrementInt64(t *testing.T) {
tc := New(0, 0)
tc.Set("tint64", int64(1), 0)
err := tc.Increment("tint64", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
x, found := tc.Get("tint64")
if !found {
t.Error("tint64 was not found")
}
if x.(int64) != 3 {
t.Error("tint64 is not 3:", x)
}
}
func TestIncrementFloat32(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
tc.Set("float32", float32(1.5), 0) tc.Set("float32", float32(1.5), 0)
err := tc.Increment("float32", 2) err := tc.Increment("float32", 2)
@ -324,7 +321,7 @@ func TestIncrementFloat32(t *testing.T) {
} }
} }
func TestIncrementFloat64(t *testing.T) { func TestIncrementWithFloat64(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
tc.Set("float64", float64(1.5), 0) tc.Set("float64", float64(1.5), 0)
err := tc.Increment("float64", 2) err := tc.Increment("float64", 2)
@ -340,7 +337,103 @@ func TestIncrementFloat64(t *testing.T) {
} }
} }
func TestDecrementInt64(t *testing.T) { func TestIncrementFloatWithFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(1.5), 0)
err := tc.IncrementFloat("float32", 2)
if err != nil {
t.Error("Error incrementfloating:", err)
}
x, found := tc.Get("float32")
if !found {
t.Error("float32 was not found")
}
if x.(float32) != 3.5 {
t.Error("float32 is not 3.5:", x)
}
}
func TestIncrementFloatWithFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(1.5), 0)
err := tc.IncrementFloat("float64", 2)
if err != nil {
t.Error("Error incrementfloating:", err)
}
x, found := tc.Get("float64")
if !found {
t.Error("float64 was not found")
}
if x.(float64) != 3.5 {
t.Error("float64 is not 3.5:", x)
}
}
func TestDecrementWithInt(t *testing.T) {
tc := New(0, 0)
tc.Set("int", int(5), 0)
err := tc.Decrement("int", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("int")
if !found {
t.Error("int was not found")
}
if x.(int) != 3 {
t.Error("int is not 3:", x)
}
}
func TestDecrementWithInt8(t *testing.T) {
tc := New(0, 0)
tc.Set("int8", int8(5), 0)
err := tc.Decrement("int8", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("int8")
if !found {
t.Error("int8 was not found")
}
if x.(int8) != 3 {
t.Error("int8 is not 3:", x)
}
}
func TestDecrementWithInt16(t *testing.T) {
tc := New(0, 0)
tc.Set("int16", int16(5), 0)
err := tc.Decrement("int16", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("int16")
if !found {
t.Error("int16 was not found")
}
if x.(int16) != 3 {
t.Error("int16 is not 3:", x)
}
}
func TestDecrementWithInt32(t *testing.T) {
tc := New(0, 0)
tc.Set("int32", int32(5), 0)
err := tc.Decrement("int32", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("int32")
if !found {
t.Error("int32 was not found")
}
if x.(int32) != 3 {
t.Error("int32 is not 3:", x)
}
}
func TestDecrementWithInt64(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
tc.Set("int64", int64(5), 0) tc.Set("int64", int64(5), 0)
err := tc.Decrement("int64", 2) err := tc.Decrement("int64", 2)
@ -356,6 +449,641 @@ func TestDecrementInt64(t *testing.T) {
} }
} }
func TestDecrementWithUint(t *testing.T) {
tc := New(0, 0)
tc.Set("uint", uint(5), 0)
err := tc.Decrement("uint", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("uint")
if !found {
t.Error("uint was not found")
}
if x.(uint) != 3 {
t.Error("uint is not 3:", x)
}
}
func TestDecrementWithUintptr(t *testing.T) {
tc := New(0, 0)
tc.Set("uintptr", uintptr(5), 0)
err := tc.Decrement("uintptr", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("uintptr")
if !found {
t.Error("uintptr was not found")
}
if x.(uintptr) != 3 {
t.Error("uintptr is not 3:", x)
}
}
func TestDecrementWithUint8(t *testing.T) {
tc := New(0, 0)
tc.Set("uint8", uint8(5), 0)
err := tc.Decrement("uint8", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("uint8")
if !found {
t.Error("uint8 was not found")
}
if x.(uint8) != 3 {
t.Error("uint8 is not 3:", x)
}
}
func TestDecrementWithUint16(t *testing.T) {
tc := New(0, 0)
tc.Set("uint16", uint16(5), 0)
err := tc.Decrement("uint16", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("uint16")
if !found {
t.Error("uint16 was not found")
}
if x.(uint16) != 3 {
t.Error("uint16 is not 3:", x)
}
}
func TestDecrementWithUint32(t *testing.T) {
tc := New(0, 0)
tc.Set("uint32", uint32(5), 0)
err := tc.Decrement("uint32", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("uint32")
if !found {
t.Error("uint32 was not found")
}
if x.(uint32) != 3 {
t.Error("uint32 is not 3:", x)
}
}
func TestDecrementWithUint64(t *testing.T) {
tc := New(0, 0)
tc.Set("uint64", uint64(5), 0)
err := tc.Decrement("uint64", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("uint64")
if !found {
t.Error("uint64 was not found")
}
if x.(uint64) != 3 {
t.Error("uint64 is not 3:", x)
}
}
func TestDecrementWithFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(5.5), 0)
err := tc.Decrement("float32", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("float32")
if !found {
t.Error("float32 was not found")
}
if x.(float32) != 3.5 {
t.Error("float32 is not 3:", x)
}
}
func TestDecrementWithFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(5.5), 0)
err := tc.Decrement("float64", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("float64")
if !found {
t.Error("float64 was not found")
}
if x.(float64) != 3.5 {
t.Error("float64 is not 3:", x)
}
}
func TestDecrementFloatWithFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(5.5), 0)
err := tc.DecrementFloat("float32", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("float32")
if !found {
t.Error("float32 was not found")
}
if x.(float32) != 3.5 {
t.Error("float32 is not 3:", x)
}
}
func TestDecrementFloatWithFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(5.5), 0)
err := tc.DecrementFloat("float64", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
x, found := tc.Get("float64")
if !found {
t.Error("float64 was not found")
}
if x.(float64) != 3.5 {
t.Error("float64 is not 3:", x)
}
}
func TestIncrementInt(t *testing.T) {
tc := New(0, 0)
tc.Set("tint", 1, 0)
n, err := tc.IncrementInt("tint", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tint")
if !found {
t.Error("tint was not found")
}
if x.(int) != 3 {
t.Error("tint is not 3:", x)
}
}
func TestIncrementInt8(t *testing.T) {
tc := New(0, 0)
tc.Set("tint8", int8(1), 0)
n, err := tc.IncrementInt8("tint8", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tint8")
if !found {
t.Error("tint8 was not found")
}
if x.(int8) != 3 {
t.Error("tint8 is not 3:", x)
}
}
func TestIncrementInt16(t *testing.T) {
tc := New(0, 0)
tc.Set("tint16", int16(1), 0)
n, err := tc.IncrementInt16("tint16", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tint16")
if !found {
t.Error("tint16 was not found")
}
if x.(int16) != 3 {
t.Error("tint16 is not 3:", x)
}
}
func TestIncrementInt32(t *testing.T) {
tc := New(0, 0)
tc.Set("tint32", int32(1), 0)
n, err := tc.IncrementInt32("tint32", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tint32")
if !found {
t.Error("tint32 was not found")
}
if x.(int32) != 3 {
t.Error("tint32 is not 3:", x)
}
}
func TestIncrementInt64(t *testing.T) {
tc := New(0, 0)
tc.Set("tint64", int64(1), 0)
n, err := tc.IncrementInt64("tint64", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tint64")
if !found {
t.Error("tint64 was not found")
}
if x.(int64) != 3 {
t.Error("tint64 is not 3:", x)
}
}
func TestIncrementUint(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint", uint(1), 0)
n, err := tc.IncrementUint("tuint", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tuint")
if !found {
t.Error("tuint was not found")
}
if x.(uint) != 3 {
t.Error("tuint is not 3:", x)
}
}
func TestIncrementUintptr(t *testing.T) {
tc := New(0, 0)
tc.Set("tuintptr", uintptr(1), 0)
n, err := tc.IncrementUintptr("tuintptr", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tuintptr")
if !found {
t.Error("tuintptr was not found")
}
if x.(uintptr) != 3 {
t.Error("tuintptr is not 3:", x)
}
}
func TestIncrementUint8(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint8", uint8(1), 0)
n, err := tc.IncrementUint8("tuint8", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tuint8")
if !found {
t.Error("tuint8 was not found")
}
if x.(uint8) != 3 {
t.Error("tuint8 is not 3:", x)
}
}
func TestIncrementUint16(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint16", uint16(1), 0)
n, err := tc.IncrementUint16("tuint16", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tuint16")
if !found {
t.Error("tuint16 was not found")
}
if x.(uint16) != 3 {
t.Error("tuint16 is not 3:", x)
}
}
func TestIncrementUint32(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint32", uint32(1), 0)
n, err := tc.IncrementUint32("tuint32", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tuint32")
if !found {
t.Error("tuint32 was not found")
}
if x.(uint32) != 3 {
t.Error("tuint32 is not 3:", x)
}
}
func TestIncrementUint64(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint64", uint64(1), 0)
n, err := tc.IncrementUint64("tuint64", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("tuint64")
if !found {
t.Error("tuint64 was not found")
}
if x.(uint64) != 3 {
t.Error("tuint64 is not 3:", x)
}
}
func TestIncrementFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(1.5), 0)
n, err := tc.IncrementFloat32("float32", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3.5 {
t.Error("Returned number is not 3.5:", n)
}
x, found := tc.Get("float32")
if !found {
t.Error("float32 was not found")
}
if x.(float32) != 3.5 {
t.Error("float32 is not 3.5:", x)
}
}
func TestIncrementFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(1.5), 0)
n, err := tc.IncrementFloat64("float64", 2)
if err != nil {
t.Error("Error incrementing:", err)
}
if n != 3.5 {
t.Error("Returned number is not 3.5:", n)
}
x, found := tc.Get("float64")
if !found {
t.Error("float64 was not found")
}
if x.(float64) != 3.5 {
t.Error("float64 is not 3.5:", x)
}
}
func TestDecrementInt8(t *testing.T) {
tc := New(0, 0)
tc.Set("int8", int8(5), 0)
n, err := tc.DecrementInt8("int8", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("int8")
if !found {
t.Error("int8 was not found")
}
if x.(int8) != 3 {
t.Error("int8 is not 3:", x)
}
}
func TestDecrementInt16(t *testing.T) {
tc := New(0, 0)
tc.Set("int16", int16(5), 0)
n, err := tc.DecrementInt16("int16", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("int16")
if !found {
t.Error("int16 was not found")
}
if x.(int16) != 3 {
t.Error("int16 is not 3:", x)
}
}
func TestDecrementInt32(t *testing.T) {
tc := New(0, 0)
tc.Set("int32", int32(5), 0)
n, err := tc.DecrementInt32("int32", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("int32")
if !found {
t.Error("int32 was not found")
}
if x.(int32) != 3 {
t.Error("int32 is not 3:", x)
}
}
func TestDecrementInt64(t *testing.T) {
tc := New(0, 0)
tc.Set("int64", int64(5), 0)
n, err := tc.DecrementInt64("int64", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("int64")
if !found {
t.Error("int64 was not found")
}
if x.(int64) != 3 {
t.Error("int64 is not 3:", x)
}
}
func TestDecrementUint(t *testing.T) {
tc := New(0, 0)
tc.Set("uint", uint(5), 0)
n, err := tc.DecrementUint("uint", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("uint")
if !found {
t.Error("uint was not found")
}
if x.(uint) != 3 {
t.Error("uint is not 3:", x)
}
}
func TestDecrementUintptr(t *testing.T) {
tc := New(0, 0)
tc.Set("uintptr", uintptr(5), 0)
n, err := tc.DecrementUintptr("uintptr", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("uintptr")
if !found {
t.Error("uintptr was not found")
}
if x.(uintptr) != 3 {
t.Error("uintptr is not 3:", x)
}
}
func TestDecrementUint8(t *testing.T) {
tc := New(0, 0)
tc.Set("uint8", uint8(5), 0)
n, err := tc.DecrementUint8("uint8", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("uint8")
if !found {
t.Error("uint8 was not found")
}
if x.(uint8) != 3 {
t.Error("uint8 is not 3:", x)
}
}
func TestDecrementUint16(t *testing.T) {
tc := New(0, 0)
tc.Set("uint16", uint16(5), 0)
n, err := tc.DecrementUint16("uint16", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("uint16")
if !found {
t.Error("uint16 was not found")
}
if x.(uint16) != 3 {
t.Error("uint16 is not 3:", x)
}
}
func TestDecrementUint32(t *testing.T) {
tc := New(0, 0)
tc.Set("uint32", uint32(5), 0)
n, err := tc.DecrementUint32("uint32", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("uint32")
if !found {
t.Error("uint32 was not found")
}
if x.(uint32) != 3 {
t.Error("uint32 is not 3:", x)
}
}
func TestDecrementUint64(t *testing.T) {
tc := New(0, 0)
tc.Set("uint64", uint64(5), 0)
n, err := tc.DecrementUint64("uint64", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("uint64")
if !found {
t.Error("uint64 was not found")
}
if x.(uint64) != 3 {
t.Error("uint64 is not 3:", x)
}
}
func TestDecrementFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(5), 0)
n, err := tc.DecrementFloat32("float32", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("float32")
if !found {
t.Error("float32 was not found")
}
if x.(float32) != 3 {
t.Error("float32 is not 3:", x)
}
}
func TestDecrementFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(5), 0)
n, err := tc.DecrementFloat64("float64", 2)
if err != nil {
t.Error("Error decrementing:", err)
}
if n != 3 {
t.Error("Returned number is not 3:", n)
}
x, found := tc.Get("float64")
if !found {
t.Error("float64 was not found")
}
if x.(float64) != 3 {
t.Error("float64 is not 3:", x)
}
}
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
err := tc.Add("foo", "bar", 0) err := tc.Add("foo", "bar", 0)