Skip to contentSkip to conversation

hwconfig: Add some unit tests

PATCH New Anton Vorontsov 2 participants 2 messages Jun 18, 2010 – Jun 29, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 hwconfig: Add some unit tests

I use this for testing, and I think this might be useful in the future.

---
 common/hwconfig.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)
common/hwconfig.c +55 -0
index 85a69da..1f9f4a0 100644
@@ -11,12 +11,20 @@
  * the License, or (at your option) any later version.
  */
 
+#ifndef HWCONFIG_TEST
 #include <config.h>
 #include <common.h>
 #include <exports.h>
 #include <hwconfig.h>
 #include <linux/types.h>
 #include <linux/string.h>
+#else
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#define min(a, b) (((a) < (b)) ? (a) : (b))
+#endif /* HWCONFIG_TEST */
 
 static const char *hwconfig_parse(const char *opts, size_t maxlen,
 				  const char *opt, char *stopchs, char eqch,
@@ -209,3 +217,50 @@ int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg)
 
 	return !strncmp(argstr, subarg, arglen);
 }
+
+#ifdef HWCONFIG_TEST
+int main()
+{
+	const char *ret;
+	size_t len;
+
+	setenv("hwconfig", "key1:subkey1=value1,subkey2=value2;key2:value3;;;;"
+			   "key3;:,:=;key4", 1);
+
+	ret = hwconfig_arg("key1", &len);
+	printf("%zd %.*s\n", len, (int)len, ret);
+	assert(len == 29);
+	assert(hwconfig_arg_cmp("key1", "subkey1=value1,subkey2=value2"));
+	assert(!strncmp(ret, "subkey1=value1,subkey2=value2", len));
+
+	ret = hwconfig_subarg("key1", "subkey1", &len);
+	printf("%zd %.*s\n", len, (int)len, ret);
+	assert(len == 6);
+	assert(hwconfig_subarg_cmp("key1", "subkey1", "value1"));
+	assert(!strncmp(ret, "value1", len));
+
+	ret = hwconfig_subarg("key1", "subkey2", &len);
+	printf("%zd %.*s\n", len, (int)len, ret);
+	assert(len == 6);
+	assert(hwconfig_subarg_cmp("key1", "subkey2", "value2"));
+	assert(!strncmp(ret, "value2", len));
+
+	ret = hwconfig_arg("key2", &len);
+	printf("%zd %.*s\n", len, (int)len, ret);
+	assert(len == 6);
+	assert(hwconfig_arg_cmp("key2", "value3"));
+	assert(!strncmp(ret, "value3", len));
+
+	assert(hwconfig("key3"));
+	assert(hwconfig_arg("key4", &len) == NULL);
+	assert(hwconfig_arg("bogus", &len) == NULL);
+
+	unsetenv("hwconfig");
+
+	assert(hwconfig(NULL) == 0);
+	assert(hwconfig("") == 0);
+	assert(hwconfig("key3") == 0);
+
+	return 0;
+}
+#endif /* HWCONFIG_TEST */
Signature · 1 line
1.7.0.5
#1 lore raw

PATCH 2/2 hwconfig: Add some unit tests

Dear Anton Vorontsov,

Quoted 7 lines · In message <[email protected]> you wrote:

I use this for testing, and I think this might be useful in the future.

---
 common/hwconfig.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)

Applied to "next", thanks.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Overflow on /dev/null, please empty the bit bucket.