incr.tcl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. start_server {tags {"incr"}} {
  2. test {INCR against non existing key} {
  3. set res {}
  4. append res [r incr novar]
  5. append res [r get novar]
  6. } {11}
  7. test {INCR against key created by incr itself} {
  8. r incr novar
  9. } {2}
  10. test {INCR against key originally set with SET} {
  11. r set novar 100
  12. r incr novar
  13. } {101}
  14. test {INCR over 32bit value} {
  15. r set novar 17179869184
  16. r incr novar
  17. } {17179869185}
  18. test {INCRBY over 32bit value with over 32bit increment} {
  19. r set novar 17179869184
  20. r incrby novar 17179869184
  21. } {34359738368}
  22. test {INCR fails against key with spaces (left)} {
  23. r set novar " 11"
  24. catch {r incr novar} err
  25. format $err
  26. } {ERR*}
  27. test {INCR fails against key with spaces (right)} {
  28. r set novar "11 "
  29. catch {r incr novar} err
  30. format $err
  31. } {ERR*}
  32. test {INCR fails against key with spaces (both)} {
  33. r set novar " 11 "
  34. catch {r incr novar} err
  35. format $err
  36. } {ERR*}
  37. test {INCR fails against a key holding a list} {
  38. r rpush mylist 1
  39. catch {r incr mylist} err
  40. r rpop mylist
  41. format $err
  42. } {WRONGTYPE*}
  43. test {DECRBY over 32bit value with over 32bit increment, negative res} {
  44. r set novar 17179869184
  45. r decrby novar 17179869185
  46. } {-1}
  47. test {INCR uses shared objects in the 0-9999 range} {
  48. r set foo -1
  49. r incr foo
  50. assert {[r object refcount foo] > 1}
  51. r set foo 9998
  52. r incr foo
  53. assert {[r object refcount foo] > 1}
  54. r incr foo
  55. assert {[r object refcount foo] == 1}
  56. }
  57. test {INCR can modify objects in-place} {
  58. r set foo 20000
  59. r incr foo
  60. assert {[r object refcount foo] == 1}
  61. set old [lindex [split [r debug object foo]] 1]
  62. r incr foo
  63. set new [lindex [split [r debug object foo]] 1]
  64. assert {[string range $old 0 2] eq "at:"}
  65. assert {[string range $new 0 2] eq "at:"}
  66. assert {$old eq $new}
  67. }
  68. test {INCRBYFLOAT against non existing key} {
  69. r del novar
  70. list [roundFloat [r incrbyfloat novar 1]] \
  71. [roundFloat [r get novar]] \
  72. [roundFloat [r incrbyfloat novar 0.25]] \
  73. [roundFloat [r get novar]]
  74. } {1 1 1.25 1.25}
  75. test {INCRBYFLOAT against key originally set with SET} {
  76. r set novar 1.5
  77. roundFloat [r incrbyfloat novar 1.5]
  78. } {3}
  79. test {INCRBYFLOAT over 32bit value} {
  80. r set novar 17179869184
  81. r incrbyfloat novar 1.5
  82. } {17179869185.5}
  83. test {INCRBYFLOAT over 32bit value with over 32bit increment} {
  84. r set novar 17179869184
  85. r incrbyfloat novar 17179869184
  86. } {34359738368}
  87. test {INCRBYFLOAT fails against key with spaces (left)} {
  88. set err {}
  89. r set novar " 11"
  90. catch {r incrbyfloat novar 1.0} err
  91. format $err
  92. } {ERR*valid*}
  93. test {INCRBYFLOAT fails against key with spaces (right)} {
  94. set err {}
  95. r set novar "11 "
  96. catch {r incrbyfloat novar 1.0} err
  97. format $err
  98. } {ERR*valid*}
  99. test {INCRBYFLOAT fails against key with spaces (both)} {
  100. set err {}
  101. r set novar " 11 "
  102. catch {r incrbyfloat novar 1.0} err
  103. format $err
  104. } {ERR*valid*}
  105. test {INCRBYFLOAT fails against a key holding a list} {
  106. r del mylist
  107. set err {}
  108. r rpush mylist 1
  109. catch {r incrbyfloat mylist 1.0} err
  110. r del mylist
  111. format $err
  112. } {WRONGTYPE*}
  113. test {INCRBYFLOAT does not allow NaN or Infinity} {
  114. r set foo 0
  115. set err {}
  116. catch {r incrbyfloat foo +inf} err
  117. set err
  118. # p.s. no way I can force NaN to test it from the API because
  119. # there is no way to increment / decrement by infinity nor to
  120. # perform divisions.
  121. } {ERR*would produce*}
  122. test {INCRBYFLOAT decrement} {
  123. r set foo 1
  124. roundFloat [r incrbyfloat foo -1.1]
  125. } {-0.1}
  126. test {string to double with null terminator} {
  127. r set foo 1
  128. r setrange foo 2 2
  129. catch {r incrbyfloat foo 1} err
  130. format $err
  131. } {ERR*valid*}
  132. }