Skip to content

Commit

Permalink
Fix incrby/decrby for large integers
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-grunder committed Jul 1, 2016
1 parent 9df734a commit 3a12758
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions redis_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1382,13 +1382,13 @@ redis_atomic_increment(INTERNAL_FUNCTION_PARAMETERS, int type,
if (val == 1) {
*cmd_len = redis_cmd_format_static(cmd,"INCR","s",key,key_len);
} else {
*cmd_len = redis_cmd_format_static(cmd,"INCRBY","sd",key,key_len,val);
*cmd_len = redis_cmd_format_static(cmd,"INCRBY","sl",key,key_len,val);
}
} else {
if (val == 1) {
*cmd_len = redis_cmd_format_static(cmd,"DECR","s",key,key_len);
} else {
*cmd_len = redis_cmd_format_static(cmd,"DECRBY","sd",key,key_len,val);
*cmd_len = redis_cmd_format_static(cmd,"DECRBY","sl",key,key_len,val);
}
}

Expand Down
35 changes: 19 additions & 16 deletions tests/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,32 +480,35 @@ public function testIncr()
$this->redis->set('key', 0);

$this->redis->incr('key');
$this->assertEquals(1, (int)$this->redis->get('key'));
$this->assertEquals(1, (int)$this->redis->get('key'));

$this->redis->incr('key');
$this->assertEquals(2, (int)$this->redis->get('key'));
$this->assertEquals(2, (int)$this->redis->get('key'));

$this->redis->incrBy('key', 3);
$this->assertEquals(5, (int)$this->redis->get('key'));
$this->redis->incrBy('key', 3);
$this->assertEquals(5, (int)$this->redis->get('key'));

$this->redis->incrBy('key', 1);
$this->assertEquals(6, (int)$this->redis->get('key'));
$this->redis->incrBy('key', 1);
$this->assertEquals(6, (int)$this->redis->get('key'));

$this->redis->incrBy('key', -1);
$this->assertEquals(5, (int)$this->redis->get('key'));
$this->redis->incrBy('key', -1);
$this->assertEquals(5, (int)$this->redis->get('key'));

$this->redis->incr('key', 5);
$this->assertEquals(10, (int)$this->redis->get('key'));
$this->redis->incr('key', 5);
$this->assertEquals(10, (int)$this->redis->get('key'));

$this->redis->del('key');
$this->redis->del('key');

$this->redis->set('key', 'abc');

$this->redis->set('key', 'abc');
$this->redis->incr('key');
$this->assertTrue("abc" === $this->redis->get('key'));

$this->redis->incr('key');
$this->assertTrue("abc" === $this->redis->get('key'));
$this->redis->incr('key');
$this->assertTrue("abc" === $this->redis->get('key'));

$this->redis->incr('key');
$this->assertTrue("abc" === $this->redis->get('key'));
$this->redis->set('key', 0);
$this->assertEquals(2147483648, $this->redis->incrby('key', 2147483648));
}

public function testIncrByFloat()
Expand Down

0 comments on commit 3a12758

Please sign in to comment.