Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 4 Jan 2016 03:26:36 +0000 (UTC)
From:      Garrett Cooper <ngie@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r293134 - head/lib/libnv/tests
Message-ID:  <201601040326.u043Qa46007717@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ngie
Date: Mon Jan  4 03:26:36 2016
New Revision: 293134
URL: https://svnweb.freebsd.org/changeset/base/293134

Log:
  Use `nitems(x)` macro instead of using hardcoded numbers for indices into
  the nvlists
  
  Convert some of the variables from int to unsigned int to squelch -Wsign-compare
  warnings when converting hardcoded values to nitems(..)
  
  Differential Revision: https://reviews.freebsd.org/D4769 (part of larger diff)
  MFC after: 5 days
  Reviewed by: oshogbo
  Sponsored by: EMC / Isilon Storage Division

Modified:
  head/lib/libnv/tests/nv_array_tests.cc

Modified: head/lib/libnv/tests/nv_array_tests.cc
==============================================================================
--- head/lib/libnv/tests/nv_array_tests.cc	Mon Jan  4 03:22:06 2016	(r293133)
+++ head/lib/libnv/tests/nv_array_tests.cc	Mon Jan  4 03:26:36 2016	(r293134)
@@ -27,8 +27,9 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <sys/nv.h>
+#include <sys/param.h>
 #include <sys/types.h>
+#include <sys/nv.h>
 #include <sys/socket.h>
 
 #include <atf-c++.hpp>
@@ -107,7 +108,7 @@ ATF_TEST_CASE_BODY(nvlist_string_array__
 	ATF_REQUIRE(nvlist_empty(nvl));
 	ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
 
-	nvlist_add_string_array(nvl, key, string_arr, 8);
+	nvlist_add_string_array(nvl, key, string_arr, nitems(string_arr));
 	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
 	ATF_REQUIRE(!nvlist_empty(nvl));
 	ATF_REQUIRE(nvlist_exists_string_array(nvl, key));
@@ -116,7 +117,7 @@ ATF_TEST_CASE_BODY(nvlist_string_array__
 	const_result = nvlist_get_string_array(nvl, key, &num_items);
 	ATF_REQUIRE(!nvlist_empty(nvl));
 	ATF_REQUIRE(const_result != NULL);
-	ATF_REQUIRE(num_items == 8);
+	ATF_REQUIRE(num_items == nitems(string_arr));
 	for (i = 0; i < num_items; i++) {
 		if (string_arr[i] != NULL) {
 			ATF_REQUIRE(strcmp(const_result[i],
@@ -128,7 +129,7 @@ ATF_TEST_CASE_BODY(nvlist_string_array__
 
 	result = nvlist_take_string_array(nvl, key, &num_items);
 	ATF_REQUIRE(result != NULL);
-	ATF_REQUIRE_EQ(num_items, 8);
+	ATF_REQUIRE_EQ(num_items, nitems(string_arr));
 	for (i = 0; i < num_items; i++) {
 		if (string_arr[i] != NULL) {
 			ATF_REQUIRE_EQ(strcmp(result[i], string_arr[i]), 0);
@@ -141,7 +142,7 @@ ATF_TEST_CASE_BODY(nvlist_string_array__
 	ATF_REQUIRE(nvlist_empty(nvl));
 	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
 
-	for (i = 0; i < 8; i++)
+	for (i = 0; i < num_items; i++)
 		free(result[i]);
 	free(result);
 	nvlist_destroy(nvl);
@@ -157,7 +158,7 @@ ATF_TEST_CASE_BODY(nvlist_descriptor_arr
 	unsigned int i;
 	const char *key;
 
-	for (i = 0; i < 32; i++) {
+	for (i = 0; i < nitems(fd); i++) {
 		fd[i] = dup(STDERR_FILENO);
 		ATF_REQUIRE(fd_is_valid(fd[i]));
 	}
@@ -168,7 +169,7 @@ ATF_TEST_CASE_BODY(nvlist_descriptor_arr
 	ATF_REQUIRE(nvlist_empty(nvl));
 	ATF_REQUIRE(!nvlist_exists_descriptor_array(nvl, key));
 
-	nvlist_add_descriptor_array(nvl, key, fd, 32);
+	nvlist_add_descriptor_array(nvl, key, fd, nitems(fd));
 	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
 	ATF_REQUIRE(!nvlist_empty(nvl));
 	ATF_REQUIRE(nvlist_exists_descriptor_array(nvl, key));
@@ -177,7 +178,7 @@ ATF_TEST_CASE_BODY(nvlist_descriptor_arr
 	const_result = nvlist_get_descriptor_array(nvl, key, &num_items);
 	ATF_REQUIRE(!nvlist_empty(nvl));
 	ATF_REQUIRE(const_result != NULL);
-	ATF_REQUIRE(num_items == 32);
+	ATF_REQUIRE(num_items == nitems(fd));
 	for (i = 0; i < num_items; i++) {
 		ATF_REQUIRE(fd_is_valid(const_result[i]));
 		if (i > 0)
@@ -186,7 +187,7 @@ ATF_TEST_CASE_BODY(nvlist_descriptor_arr
 
 	result = nvlist_take_descriptor_array(nvl, key, &num_items);
 	ATF_REQUIRE(result != NULL);
-	ATF_REQUIRE_EQ(num_items, 32);
+	ATF_REQUIRE_EQ(num_items, nitems(fd));
 	for (i = 0; i < num_items; i++) {
 		ATF_REQUIRE(fd_is_valid(result[i]));
 		if (i > 0)
@@ -223,7 +224,7 @@ ATF_TEST_CASE_BODY(nvlist_number_array__
 	ATF_REQUIRE(nvlist_empty(nvl));
 	ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
 
-	nvlist_add_number_array(nvl, key, number, 8);
+	nvlist_add_number_array(nvl, key, number, nitems(number));
 	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
 	ATF_REQUIRE(!nvlist_empty(nvl));
 	ATF_REQUIRE(nvlist_exists_number_array(nvl, key));
@@ -232,13 +233,13 @@ ATF_TEST_CASE_BODY(nvlist_number_array__
 	const_result = nvlist_get_number_array(nvl, key, &num_items);
 	ATF_REQUIRE(!nvlist_empty(nvl));
 	ATF_REQUIRE(const_result != NULL);
-	ATF_REQUIRE(num_items == 8);
+	ATF_REQUIRE(num_items == nitems(number));
 	for (i = 0; i < num_items; i++)
 		ATF_REQUIRE_EQ(const_result[i], number[i]);
 
 	result = nvlist_take_number_array(nvl, key, &num_items);
 	ATF_REQUIRE(result != NULL);
-	ATF_REQUIRE_EQ(num_items, 8);
+	ATF_REQUIRE_EQ(num_items, nitems(number));
 	for (i = 0; i < num_items; i++)
 		ATF_REQUIRE_EQ(result[i], number[i]);
 
@@ -286,7 +287,7 @@ ATF_TEST_CASE_BODY(nvlist_nvlist_array__
 	const_result = nvlist_get_nvlist_array(nvl, key, &num_items);
 	ATF_REQUIRE(!nvlist_empty(nvl));
 	ATF_REQUIRE(const_result != NULL);
-	ATF_REQUIRE(num_items == 8);
+	ATF_REQUIRE(num_items == nitems(testnvl));
 
 	for (i = 0; i < num_items; i++) {
 		ATF_REQUIRE_EQ(nvlist_error(const_result[i]), 0);
@@ -343,12 +344,12 @@ ATF_TEST_CASE_BODY(nvlist_clone_array)
 	const uint64_t number[8] = { 0, UINT_MAX, 7, 123, 90,
 	    100000, 8, 1 };
 
-	for (i = 0; i < 16; i++) {
+	for (i = 0; i < nitems(testfd); i++) {
 		testbool[i] = (i % 2 == 0);
 		testfd[i] = dup(STDERR_FILENO);
 		ATF_REQUIRE(fd_is_valid(testfd[i]));
 	}
-	for (i = 0; i < 8; i++) {
+	for (i = 0; i < nitems(testnvl); i++) {
 		testnvl[i] = nvlist_create(0);
 		ATF_REQUIRE(nvlist_error(testnvl[i]) == 0);
 		nvlist_add_string(testnvl[i], "nvl/nvl/teststr", somestr[i]);
@@ -359,28 +360,30 @@ ATF_TEST_CASE_BODY(nvlist_clone_array)
 	ATF_REQUIRE(nvlist_error(src) == 0);
 
 	ATF_REQUIRE(!nvlist_exists_bool_array(src, "nvl/bool"));
-	nvlist_add_bool_array(src, "nvl/bool", testbool, 16);
+	nvlist_add_bool_array(src, "nvl/bool", testbool, nitems(testbool));
 	ATF_REQUIRE_EQ(nvlist_error(src), 0);
 	ATF_REQUIRE(nvlist_exists_bool_array(src, "nvl/bool"));
 
 	ATF_REQUIRE(!nvlist_exists_string_array(src, "nvl/string"));
-	nvlist_add_string_array(src, "nvl/string", string_arr, 8);
+	nvlist_add_string_array(src, "nvl/string", string_arr,
+	    nitems(string_arr));
 	ATF_REQUIRE_EQ(nvlist_error(src), 0);
 	ATF_REQUIRE(nvlist_exists_string_array(src, "nvl/string"));
 
 	ATF_REQUIRE(!nvlist_exists_descriptor_array(src, "nvl/fd"));
-	nvlist_add_descriptor_array(src, "nvl/fd", testfd, 16);
+	nvlist_add_descriptor_array(src, "nvl/fd", testfd, nitems(testfd));
 	ATF_REQUIRE_EQ(nvlist_error(src), 0);
 	ATF_REQUIRE(nvlist_exists_descriptor_array(src, "nvl/fd"));
 
 	ATF_REQUIRE(!nvlist_exists_number_array(src, "nvl/number"));
-	nvlist_add_number_array(src, "nvl/number", number, 8);
+	nvlist_add_number_array(src, "nvl/number", number,
+	    nitems(number));
 	ATF_REQUIRE_EQ(nvlist_error(src), 0);
 	ATF_REQUIRE(nvlist_exists_number_array(src, "nvl/number"));
 
 	ATF_REQUIRE(!nvlist_exists_nvlist_array(src, "nvl/array"));
 	nvlist_add_nvlist_array(src, "nvl/array",
-	    (const nvlist_t * const *)testnvl, 8);
+	    (const nvlist_t * const *)testnvl, nitems(testnvl));
 	ATF_REQUIRE_EQ(nvlist_error(src), 0);
 	ATF_REQUIRE(nvlist_exists_nvlist_array(src, "nvl/array"));
 
@@ -389,7 +392,7 @@ ATF_TEST_CASE_BODY(nvlist_clone_array)
 
 	ATF_REQUIRE(nvlist_exists_bool_array(dst, "nvl/bool"));
 	(void) nvlist_get_bool_array(dst, "nvl/bool", &num_items);
-	ATF_REQUIRE_EQ(num_items, 16);
+	ATF_REQUIRE_EQ(num_items, nitems(testbool));
 	for (i = 0; i < num_items; i++) {
 		ATF_REQUIRE(
 		    nvlist_get_bool_array(dst, "nvl/bool", &num_items)[i] ==
@@ -398,7 +401,7 @@ ATF_TEST_CASE_BODY(nvlist_clone_array)
 
 	ATF_REQUIRE(nvlist_exists_string_array(dst, "nvl/string"));
 	(void) nvlist_get_string_array(dst, "nvl/string", &num_items);
-	ATF_REQUIRE_EQ(num_items, 8);
+	ATF_REQUIRE_EQ(num_items, nitems(string_arr));
 	for (i = 0; i < num_items; i++) {
 		if (nvlist_get_string_array(dst, "nvl/string",
 		    &num_items)[i] == NULL) {
@@ -414,14 +417,14 @@ ATF_TEST_CASE_BODY(nvlist_clone_array)
 
 	ATF_REQUIRE(nvlist_exists_descriptor_array(dst, "nvl/fd"));
 	(void) nvlist_get_descriptor_array(dst, "nvl/fd", &num_items);
-	ATF_REQUIRE_EQ(num_items, 16);
+	ATF_REQUIRE_EQ(num_items, nitems(testfd));
 	for (i = 0; i < num_items; i++) {
 		ATF_REQUIRE(fd_is_valid(
 		    nvlist_get_descriptor_array(dst, "nvl/fd", &num_items)[i]));
 	}
 	ATF_REQUIRE(nvlist_exists_number_array(dst, "nvl/number"));
 	(void) nvlist_get_number_array(dst, "nvl/number", &num_items);
-	ATF_REQUIRE_EQ(num_items, 8);
+	ATF_REQUIRE_EQ(num_items, nitems(number));
 
 	for (i = 0; i < num_items; i++) {
 		ATF_REQUIRE(
@@ -431,7 +434,7 @@ ATF_TEST_CASE_BODY(nvlist_clone_array)
 
 	ATF_REQUIRE(nvlist_exists_nvlist_array(dst, "nvl/array"));
 	(void) nvlist_get_nvlist_array(dst, "nvl/array", &num_items);
-	ATF_REQUIRE_EQ(num_items, 8);
+	ATF_REQUIRE_EQ(num_items, nitems(testnvl));
 	for (i = 0; i < num_items; i++) {
 		nvl = nvlist_get_nvlist_array(dst, "nvl/array", &num_items)[i];
 		ATF_REQUIRE(nvlist_exists_string(nvl, "nvl/nvl/teststr"));
@@ -439,11 +442,11 @@ ATF_TEST_CASE_BODY(nvlist_clone_array)
 		    somestr[i]) == 0);
 	}
 
-	for (i = 0; i < 16; i++) {
+	for (i = 0; i < nitems(testfd); i++) {
 		close(testfd[i]);
-		if (i < 8) {
-			nvlist_destroy(testnvl[i]);
-		}
+	}
+	for (i = 0; i < nitems(testnvl); i++) {
+		nvlist_destroy(testnvl[i]);
 	}
 	nvlist_destroy(src);
 	nvlist_destroy(dst);
@@ -765,11 +768,12 @@ ATF_TEST_CASE_BODY(nvlist_nvlist_array__
 {
 	nvlist_t *nvl, *test[5], *nasted;
 	const nvlist_t *travel;
-	void *cookie;
-	int index, i, type;
 	const char *name;
+	void *cookie;
+	int type;
+	unsigned int i, index;
 
-	for (i = 0; i < 5; i++) {
+	for (i = 0; i < nitems(test); i++) {
 		test[i] = nvlist_create(0);
 		ATF_REQUIRE(test[i] != NULL);
 		nvlist_add_number(test[i], "nvl/number", i);
@@ -777,11 +781,12 @@ ATF_TEST_CASE_BODY(nvlist_nvlist_array__
 	}
 	nvl = nvlist_create(0);
 	ATF_REQUIRE(nvl != NULL);
-	nvlist_add_nvlist_array(nvl, "nvl/nvlist_array", test, 5);
+	nvlist_add_nvlist_array(nvl, "nvl/nvlist_array", test, nitems(test));
 	ATF_REQUIRE(nvlist_error(nvl) == 0);
 	nasted = nvlist_create(0);
 	ATF_REQUIRE(nasted != NULL);
-	nvlist_add_nvlist_array(nasted, "nvl/nvl/nvlist_array", test, 5);
+	nvlist_add_nvlist_array(nasted, "nvl/nvl/nvlist_array", test,
+	    nitems(test));
 	ATF_REQUIRE(nvlist_error(nasted) == 0);
 	nvlist_move_nvlist(nvl, "nvl/nvl", nasted);
 	ATF_REQUIRE(nvlist_error(nvl) == 0);
@@ -795,15 +800,16 @@ ATF_TEST_CASE_BODY(nvlist_nvlist_array__
 		while ((name = nvlist_next(travel, &type, &cookie)) != NULL) {
 			if (index == 0) {
 				ATF_REQUIRE(type == NV_TYPE_NVLIST_ARRAY);
-			} else if (index >= 1 && index <= 5) {
+			} else if (index >= 1 && index <= nitems(test)) {
 				ATF_REQUIRE(type == NV_TYPE_NUMBER);
-			} else if (index == 6) {
+			} else if (index == nitems(test) + 1) {
 				ATF_REQUIRE(type == NV_TYPE_NVLIST);
-			} else if (index == 7) {
+			} else if (index == nitems(test) + 2) {
 				ATF_REQUIRE(type == NV_TYPE_NVLIST_ARRAY);
-			} else if (index >= 8 && index <= 12) {
+			} else if (index >= nitems(test) + 3 &&
+				   index <= 2 * nitems(test) + 2) {
 				ATF_REQUIRE(type == NV_TYPE_NUMBER);
-			} else if (index == 13) {
+			} else if (index == 2 * nitems(test) + 3) {
 				ATF_REQUIRE(type == NV_TYPE_STRING);
 			}
 
@@ -819,7 +825,7 @@ ATF_TEST_CASE_BODY(nvlist_nvlist_array__
 		}
 	} while ((travel = nvlist_get_pararr(travel, &cookie)) != NULL);
 
-	for (i = 0; i < 5; i++)
+	for (i = 0; i < nitems(test); i++)
 		nvlist_destroy(test[i]);
 
 	nvlist_destroy(nvl);
@@ -909,7 +915,7 @@ ATF_TEST_CASE_BODY(nvlist_bool_array__pa
 	const bool *const_result;
 	bool testbool[16];
 
-	for (i = 0; i < 16; i++)
+	for (i = 0; i < nitems(testbool); i++)
 		testbool[i] = (i % 2 == 0);
 
 	key = "nvl/bool";
@@ -918,7 +924,7 @@ ATF_TEST_CASE_BODY(nvlist_bool_array__pa
 	ATF_REQUIRE(nvlist_empty(nvl));
 	ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
 
-	nvlist_add_bool_array(nvl, key, testbool, 16);
+	nvlist_add_bool_array(nvl, key, testbool, nitems(testbool));
 	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
 	ATF_REQUIRE(!nvlist_empty(nvl));
 	ATF_REQUIRE(nvlist_exists_bool_array(nvl, key));
@@ -932,7 +938,7 @@ ATF_TEST_CASE_BODY(nvlist_bool_array__pa
 	ATF_REQUIRE(nvlist_exists_bool_array(unpacked, key));
 
 	const_result = nvlist_get_bool_array(unpacked, key, &count);
-	ATF_REQUIRE_EQ(count, 16);
+	ATF_REQUIRE_EQ(count, nitems(testbool));
 	for (i = 0; i < count; i++) {
 		ATF_REQUIRE_EQ(testbool[i], const_result[i]);
 	}
@@ -974,7 +980,7 @@ ATF_TEST_CASE_BODY(nvlist_number_array__
 	ATF_REQUIRE(nvlist_exists_number_array(unpacked, key));
 
 	const_result = nvlist_get_number_array(unpacked, key, &count);
-	ATF_REQUIRE_EQ(count, 8);
+	ATF_REQUIRE_EQ(count, nitems(number));
 	for (i = 0; i < count; i++) {
 		ATF_REQUIRE_EQ(number[i], const_result[i]);
 	}
@@ -1005,7 +1011,7 @@ ATF_TEST_CASE_BODY(nvlist_descriptor_arr
 		/* Child. */
 		fd = socks[0];
 		close(socks[1]);
-		for (i = 0; i < 32; i++) {
+		for (i = 0; i < nitems(desc); i++) {
 			desc[i] = dup(STDERR_FILENO);
 			ATF_REQUIRE(fd_is_valid(desc[i]));
 		}
@@ -1015,14 +1021,14 @@ ATF_TEST_CASE_BODY(nvlist_descriptor_arr
 		ATF_REQUIRE(nvlist_empty(nvl));
 		ATF_REQUIRE(!nvlist_exists_descriptor_array(nvl, key));
 
-		nvlist_add_descriptor_array(nvl, key, desc, 32);
+		nvlist_add_descriptor_array(nvl, key, desc, nitems(desc));
 		ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
 		ATF_REQUIRE(!nvlist_empty(nvl));
 		ATF_REQUIRE(nvlist_exists_descriptor_array(nvl, key));
 
 		ATF_REQUIRE(nvlist_send(fd, nvl) >= 0);
 
-		for (i = 0; i < num_items; i++)
+		for (i = 0; i < nitems(desc); i++)
 			close(desc[i]);
 	} else {
 		/* Parent */
@@ -1037,7 +1043,7 @@ ATF_TEST_CASE_BODY(nvlist_descriptor_arr
 
 		const_result = nvlist_get_descriptor_array(nvl, key, &num_items);
 		ATF_REQUIRE(const_result != NULL);
-		ATF_REQUIRE_EQ(num_items, 32);
+		ATF_REQUIRE_EQ(num_items, nitems(desc));
 		for (i = 0; i < num_items; i++)
 			ATF_REQUIRE(fd_is_valid(const_result[i]));
 
@@ -1066,7 +1072,7 @@ ATF_TEST_CASE_BODY(nvlist_string_array__
 	ATF_REQUIRE(nvlist_empty(nvl));
 	ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
 
-	nvlist_add_string_array(nvl, key, string_arr, 8);
+	nvlist_add_string_array(nvl, key, string_arr, nitems(string_arr));
 	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
 	ATF_REQUIRE(!nvlist_empty(nvl));
 	ATF_REQUIRE(nvlist_exists_string_array(nvl, key));
@@ -1080,7 +1086,7 @@ ATF_TEST_CASE_BODY(nvlist_string_array__
 	ATF_REQUIRE(nvlist_exists_string_array(unpacked, key));
 
 	const_result = nvlist_get_string_array(unpacked, key, &count);
-	ATF_REQUIRE_EQ(count, 8);
+	ATF_REQUIRE_EQ(count, nitems(string_arr));
 	for (i = 0; i < count; i++) {
 		ATF_REQUIRE_EQ(strcmp(string_arr[i], const_result[i]), 0);
 	}
@@ -1103,7 +1109,7 @@ ATF_TEST_CASE_BODY(nvlist_nvlist_array__
 	const char *somestr[8] = { "a", "b", "c", "d", "e", "f", "g", "h" };
 	const char *key;
 
-	for (i = 0; i < 8; i++) {
+	for (i = 0; i < nitems(testnvl); i++) {
 		testnvl[i] = nvlist_create(0);
 		ATF_REQUIRE(testnvl[i] != NULL);
 		ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0);
@@ -1133,7 +1139,7 @@ ATF_TEST_CASE_BODY(nvlist_nvlist_array__
 
 	const_result = nvlist_get_nvlist_array(unpacked, key, &num_items);
 	ATF_REQUIRE(const_result != NULL);
-	ATF_REQUIRE_EQ(num_items, 8);
+	ATF_REQUIRE_EQ(num_items, nitems(testnvl));
 	for (i = 0; i < num_items; i++) {
 		ATF_REQUIRE_EQ(nvlist_error(const_result[i]), 0);
 		if (i < num_items - 1) {
@@ -1151,7 +1157,7 @@ ATF_TEST_CASE_BODY(nvlist_nvlist_array__
 		    "nvl/string"), somestr[i]) == 0);
 	}
 
-	for (i = 0; i < 8; i++)
+	for (i = 0; i < nitems(testnvl); i++)
 		nvlist_destroy(testnvl[i]);
 	free(result);
 	nvlist_destroy(nvl);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201601040326.u043Qa46007717>