[sheepdog] [PATCH v2 6/6] unit/mock: use rb_tree for mock method management

MORITA Kazutaka morita.kazutaka at gmail.com
Tue Sep 24 11:08:15 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 tests/unit/mock/mock.c | 16 +++++-----------
 tests/unit/mock/mock.h | 16 ++++++++++++----
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/tests/unit/mock/mock.c b/tests/unit/mock/mock.c
index cd321c2..373c02a 100644
--- a/tests/unit/mock/mock.c
+++ b/tests/unit/mock/mock.c
@@ -15,21 +15,15 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "list.h"
 #include "mock.h"
 
-LIST_HEAD(mock_methods);
+struct rb_root mock_methods = RB_ROOT;
 
 static struct mock_method *find_method(const char *name)
 {
-	struct mock_method *method;
-	int len;
-	list_for_each_entry(method, &mock_methods, list) {
-		len = strlen(method->name);
-		if (strncmp(method->name, name, len) == 0)
-			return method;
-	}
-	return NULL;
+	struct mock_method key = { .name = name };
+
+	return rb_search(&mock_methods, &key, rb, mock_cmp);
 }
 
 int __method_nr_call(const char *name)
@@ -46,6 +40,6 @@ int __method_nr_call(const char *name)
 void __method_reset_all(void)
 {
 	struct mock_method *method;
-	list_for_each_entry(method, &mock_methods, list)
+	rb_for_each_entry(method, &mock_methods, rb)
 		method->nr_call = 0;
 }
diff --git a/tests/unit/mock/mock.h b/tests/unit/mock/mock.h
index 9425c76..c2d6f5b 100644
--- a/tests/unit/mock/mock.h
+++ b/tests/unit/mock/mock.h
@@ -14,19 +14,27 @@
 #ifndef __MOCK_H__
 #define __MOCK_H__
 
-#include "list.h"
+#include <string.h>
+
+#include "rbtree.h"
 
 struct mock_method {
 	const char *name;
 	int nr_call;
-	struct list_node list;
+	struct rb_node rb;
 };
 
-extern struct list_head mock_methods;
+static inline int mock_cmp(const struct mock_method *m1,
+			   const struct mock_method *m2)
+{
+	return strcmp(m1->name, m2->name);
+}
+
+extern struct rb_root mock_methods;
 #define method_register(m)						\
 	static void __attribute__((constructor)) regist_##m(void)	\
 	{								\
-		list_add(&m.list, &mock_methods);			\
+		rb_insert(&mock_methods, &m, rb, mock_cmp);		\
 	}
 
 #define MOCK_VOID_METHOD(m, ...)			\
-- 
1.8.1.2




More information about the sheepdog mailing list