Skip to contentSkip to conversation

testsuite: add test for hash_add_unique

PATCH Applied Caio Marcelo de Oliveira Filho 2 participants 8 messages Jan 21, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 testsuite: add test for hash_add_unique

---
 testsuite/test-hash.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
testsuite/test-hash.c +26 -0
index 3cf5ba7..ea39b80 100644
@@ -220,4 +220,30 @@ static int test_hash_free(const struct test *t)
 DEFINE_TEST(test_hash_free,
 		.description = "test hash_free calling free function for all values");
 
+
+static int test_hash_add_unique(const struct test *t)
+{
+	const char *k[] = { "k1", "k2", "k3", "k4", "k5" };
+	const char *v[] = { "v1", "v2", "v3", "v4", "v5" };
+	unsigned int i, j, N;
+
+	N = ARRAY_SIZE(k);
+	for (i = 0; i < N; i++) {
+		/* With 4 buckets, there'll be a bucket with more than one key. */
+		struct hash *h = hash_new(4, NULL);
+
+		/* Add the keys in different orders. */
+		for (j = 0; j < N; j++) {
+			unsigned int idx = (j + i) % N;
+			hash_add_unique(h, k[idx], v[idx]);
+		}
+
+		assert_return(hash_get_count(h) == N, EXIT_FAILURE);
+		hash_free(h);
+	}
+	return 0;
+}
+DEFINE_TEST(test_hash_add_unique,
+		.description = "test hash_add_unique with different key orders")
+
 TESTSUITE_MAIN();
Signature · 1 line
2.2.2
#1 lore raw

PATCH 2/4 testsuite: add test for growing then shrinking a hash

---
 testsuite/test-hash.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
testsuite/test-hash.c +33 -0
index ea39b80..2e65f86 100644
@@ -246,4 +246,37 @@ static int test_hash_add_unique(const struct test *t)
 DEFINE_TEST(test_hash_add_unique,
 		.description = "test hash_add_unique with different key orders")
 
+
+static int test_hash_massive_add_del(const struct test *t)
+{
+	char buf[1024 * 8];
+	char *k;
+	struct hash *h;
+	unsigned int i, N = 1024;
+
+	h = hash_new(8, NULL);
+
+	k = &buf[0];
+	for (i = 0; i < N; i++) {
+		snprintf(k, 8, "k%d", i);
+		hash_add(h, k, k);
+		k += 8;
+	}
+
+	assert_return(hash_get_count(h) == N, EXIT_FAILURE);
+
+	k = &buf[0];
+	for (i = 0; i < N; i++) {
+		hash_del(h, k);
+		k += 8;
+	}
+
+	assert_return(hash_get_count(h) == 0, EXIT_FAILURE);
+
+	hash_free(h);
+	return 0;
+}
+DEFINE_TEST(test_hash_massive_add_del,
+		.description = "test multiple adds followed by multiple dels")
+
 TESTSUITE_MAIN();
Signature · 1 line
2.2.2
#2 lore raw

Re: [PATCH 2/4] testsuite: add test for growing then shrinking a hash

Quoted 3 lines · Caio Marcelo de Oliveira Filho
---
 testsuite/test-hash.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

Applied.

Signature · 1 line
Lucas De Marchi
#3 lore raw

PATCH 3/4 testsuite: add more input cases to freadline_wrapped test

In reply to Caio Marcelo de Oliveira Filho
---
 testsuite/rootfs-pristine/test-util/freadline_wrapped-correct.txt | 4 ++++
 testsuite/rootfs-pristine/test-util/freadline_wrapped-input.txt   | 2 ++
 2 files changed, 6 insertions(+)
testsuite/rootfs-pristine/test-util/freadline_wrapped-correct.txt +4 -0
index 87344ab..80c671b 100644
@@ -4,3 +4,7 @@ this is a single line
 1
 three line lines in a row
 3
+this line has exactly 256 chars to make fun with the implementation, so it must have word after word after word after word after word after word after word after word after word after word after word after word after word after word until it hits 256 chars
+1
+this line is very large because it has word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word until it hits 2015 chars
+1
testsuite/rootfs-pristine/test-util/freadline_wrapped-input.txt +2 -0
index f84a852..1c81f91 100644
@@ -4,3 +4,5 @@ this is a single line
 three line \
 lines \
 in a row
+this line has exactly 256 chars to make fun with the implementation, so it must have word after word after word after word after word after word after word after word after word after word after word after word after word after word until it hits 256 chars
+this line is very large because it has word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word after word until it hits 2015 chars
Signature · 1 line
2.2.2
#4 lore raw

Re: [PATCH 3/4] testsuite: add more input cases to freadline_wrapped test

Quoted 4 lines · Caio Marcelo de Oliveira Filho
---
 testsuite/rootfs-pristine/test-util/freadline_wrapped-correct.txt | 4 ++++
 testsuite/rootfs-pristine/test-util/freadline_wrapped-input.txt   | 2 ++
 2 files changed, 6 insertions(+)

Applied.

Signature · 1 line
Lucas De Marchi
#5 lore raw

PATCH 4/4 util: remove dead code in freadline_wrapped

In reply to Caio Marcelo de Oliveira Filho

Each loop iteration starts with 'i < size'. When i is changed the invariant is kept by checking if 'i == size' and growing the buffer.

---
 shared/util.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
shared/util.c +1 -7
index 479f0fe..1a2b17c 100644
@@ -314,13 +314,7 @@ char *freadline_wrapped(FILE *fp, unsigned int *linenum)
 			n++;
 
 			{
-				char *ret;
-				if (i == size) {
-					ret = realloc(buf, size + 1);
-					if (!ret)
-						return NULL;
-				} else
-					ret = buf;
+				char *ret = buf;
 				ret[i] = '\0';
 				buf = NULL;
 				if (linenum)
Signature · 1 line
2.2.2
#6 lore raw

Re: [PATCH 4/4] util: remove dead code in freadline_wrapped

Quoted 6 lines · Caio Marcelo de Oliveira Filho

Each loop iteration starts with 'i < size'. When i is changed the invariant is kept by checking if 'i == size' and growing the buffer.

---
 shared/util.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

Applied

Signature · 4 lines
Lucas De Marchi

-- 
Lucas De Marchi
#7 lore raw
In reply to Caio Marcelo de Oliveira Filho
Quoted 13 lines · Caio Marcelo de Oliveira Filho
---
 testsuite/test-hash.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
testsuite/test-hash.c +1 -0
index 3cf5ba7..ea39b80 100644
@@ -220,4 +220,30 @@ static int test_hash_free(const struct test *t)
 DEFINE_TEST(test_hash_free,
                .description = "test hash_free calling free function for all values");
 
+

testsuite/test-hash.c static int test_hash_free(const struct test *t) · line 233

Quoted 10 lines
testsuite/test-hash.c +10 -0
+static int test_hash_add_unique(const struct test *t)
+{
+       const char *k[] = { "k1", "k2", "k3", "k4", "k5" };
+       const char *v[] = { "v1", "v2", "v3", "v4", "v5" };
+       unsigned int i, j, N;
+
+       N = ARRAY_SIZE(k);
+       for (i = 0; i < N; i++) {
+               /* With 4 buckets, there'll be a bucket with more than one key. */
+               struct hash *h = hash_new(4, NULL);

Applied with 's/4/N - 1/' here

Signature · 1 line
Lucas De Marchi