git » nmdb » commit f8ef82e

Add a test for the increment operation.

author Alberto Bertogli
2007-08-28 06:14:02 UTC
committer Alberto Bertogli
2007-08-28 06:14:02 UTC
parent 4cb062b7c3366f05e8f442b87b2460804ac9e253

Add a test for the increment operation.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

tests/c/incr.c +67 -0
tests/c/make.sh +1 -1
tests/c/prototypes.h +3 -0

diff --git a/tests/c/incr.c b/tests/c/incr.c
new file mode 100644
index 0000000..a0604d1
--- /dev/null
+++ b/tests/c/incr.c
@@ -0,0 +1,67 @@
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <stdlib.h>
+
+#include <nmdb.h>
+#include "timer.h"
+#include "prototypes.h"
+
+
+int main(int argc, char **argv)
+{
+	int i, r, times;
+	unsigned long s_elapsed;
+	char *key = "k";
+	char *initval = "0";
+	size_t ksize;
+	long long int increment;
+	nmdb_t *db;
+
+	if (argc != 3) {
+		printf("Usage: incr-* TIMES INCREMENT\n");
+		return 1;
+	}
+
+	times = atoi(argv[1]);
+	if (times < 1) {
+		printf("Error: TIMES must be >= 1\n");
+		return 1;
+	}
+
+	increment = strtoll(argv[2], NULL, 10);
+
+	db = nmdb_init();
+	if (db == NULL) {
+		perror("nmdb_init() failed");
+		return 1;
+	}
+
+	NADDSRV(db);
+
+	ksize = strlen(key) + 1;
+
+	/* initial set */
+	NSET(db, (unsigned char *) key, ksize,
+			(unsigned char *) initval, strlen(initval) + 1);
+
+	timer_start();
+	for (i = 0; i < times; i++) {
+		r = NINCR(db, (unsigned char *) key, ksize, increment);
+		if (r != 2) {
+			printf("result: %d\n", r);
+			perror("Incr");
+			return 1;
+		}
+	}
+	s_elapsed = timer_stop();
+
+	printf("%lu\n", s_elapsed);
+
+	nmdb_free(db);
+
+	return 0;
+}
+
diff --git a/tests/c/make.sh b/tests/c/make.sh
index b65696a..3b07957 100755
--- a/tests/c/make.sh
+++ b/tests/c/make.sh
@@ -38,7 +38,7 @@ for p in TIPC TCP UDP SCTP MULT; do
 		TF="-DUSE_$p=1 -DUSE_$v=1"
 
 		echo " * $OP:"
-		for t in 1 2 3 "set" "get" "del"; do
+		for t in 1 2 3 "set" "get" "del" "incr"; do
 			echo "   * $t"
 			if [ "$CLEAN" == 1 ]; then
 				rm -f $t-$OP
diff --git a/tests/c/prototypes.h b/tests/c/prototypes.h
index 6accea8..e127493 100644
--- a/tests/c/prototypes.h
+++ b/tests/c/prototypes.h
@@ -8,16 +8,19 @@
   #define NSET(...) nmdb_set(__VA_ARGS__)
   #define NDEL(...) nmdb_del(__VA_ARGS__)
   #define NCAS(...) nmdb_cas(__VA_ARGS__)
+  #define NINCR(...) nmdb_incr(__VA_ARGS__)
 #elif USE_CACHE
   #define NGET(...) nmdb_cache_get(__VA_ARGS__)
   #define NSET(...) nmdb_cache_set(__VA_ARGS__)
   #define NDEL(...) nmdb_cache_del(__VA_ARGS__)
   #define NCAS(...) nmdb_cache_cas(__VA_ARGS__)
+  #define NINCR(...) nmdb_cache_incr(__VA_ARGS__)
 #elif USE_SYNC
   #define NGET(...) nmdb_get(__VA_ARGS__)
   #define NSET(...) nmdb_set_sync(__VA_ARGS__)
   #define NDEL(...) nmdb_del_sync(__VA_ARGS__)
   #define NCAS(...) nmdb_cas(__VA_ARGS__)
+  #define NINCR(...) nmdb_incr(__VA_ARGS__)
 #endif