Permalink
Browse files

Added ACOUNT to count array elements

  • Loading branch information...
txus committed Sep 16, 2012
1 parent ae322e9 commit c2523a84ee60ea07ea1fca0343c4b3ddf023e930
Showing with 19 additions and 2 deletions.
  1. +1 −0 README.md
  2. +9 −0 src/shitdb/db.c
  3. +1 −0 src/shitdb/db.h
  4. +8 −2 tests/db_tests.c
View
@@ -37,6 +37,7 @@ Example session:
* APUSH [key] [value]: Pushes value to the array located at key.
* APOP [key]: Pops the array located at key and returns the popped value.
+* ACOUNT [key]: Returns the number of elements in the array.
## Contributing
View
@@ -44,3 +44,12 @@ DB_apop(DB *db, bstring key)
Object *array = (Object*)Hashmap_get(db->map, key);
return (Object*)DArray_pop(array->value.as_array);
}
+
+Object*
+DB_acount(DB *db, bstring key)
+{
+ Object *array = (Object*)Hashmap_get(db->map, key);
+ int count = DArray_count(array->value.as_array);
+ return Object_create_integer(count);
+}
+
View
@@ -17,5 +17,6 @@ Object* DB_get(DB *db, bstring key);
// Array
void DB_apush(DB *db, bstring key, Object *value);
Object* DB_apop(DB *db, bstring key);
+Object* DB_acount(DB *db, bstring key);
#endif
View
@@ -57,9 +57,14 @@ char *test_apop()
bstring integerstr = Object_to_string(integer);
mu_assert(bstrcmp(integerstr, bfromcstr("999")) == 0, "Wrong popped value.");
+ return NULL;
+}
- Object *array = DB_get(db, bfromcstr("array"));
- mu_assert(DArray_count(array->value.as_array) == 2, "Value was not really popped.");
+char *test_acount()
+{
+ Object *count = DB_acount(db, bfromcstr("array"));
+ bstring countstr = Object_to_string(count);
+ mu_assert(bstrcmp(countstr, bfromcstr("2")) == 0, "Wrong array count.");
return NULL;
}
@@ -78,6 +83,7 @@ char *all_tests() {
mu_run_test(test_get);
mu_run_test(test_apush);
mu_run_test(test_apop);
+ mu_run_test(test_acount);
mu_run_test(test_destroy);
return NULL;

0 comments on commit c2523a8

Please sign in to comment.