The gethugepagesizes testcase tests the calls to retreieve the
available page sizes against faked up sysfs contents.  This includes
testing that the gethugepagesizes() call doesn't return more entries
than the maximum it's given (which might clobber memory).

However, it does this incorrectly: specifically, validate_sizes()
checks that the n returned entries exactly match the first n expected
entries, even when the maxmum number of entries to return is smaller
than the total number of pagesizes available.  gethugepagesizes() is
not guaranteed to return the pagesizes in any particular order (based
as it is on getting a directory listing), so this test will generate
bogus failures.

In fact, for the case where the given maximum is smaller than the
total number of pagesizes, the test should accept as a pass any subset
of the expected pagesizes in the returned results.  This patch makes
the correction.

---
Signed-off-by: David Gibson <d...@au1.ibm.com>

diff --git a/tests/gethugepagesizes.c b/tests/gethugepagesizes.c
index 8a6d2d6..860b9a5 100644
--- a/tests/gethugepagesizes.c
+++ b/tests/gethugepagesizes.c
@@ -255,22 +255,29 @@ void cleanup(void)
                cleanup_fake_data();
 }
 
-void validate_sizes(int line, long actual_sizes[], int actual, int max_actual,
-                       long expected_sizes[], int expected)
+void validate_sizes(int line, long actual_sizes[], int actual,
+                   int max, int maxmax,
+                   long expected_sizes[], int expected)
 {
        int i, j;
-       if (expected != actual)
+
+       verbose_printf("Line %d: Expecting sizes:", line);
+       for (i = 0; i < expected; i++)
+               verbose_printf(" %ld", expected_sizes[i]);
+       verbose_printf("\n");
+       verbose_printf("Line %d: Actual sizes are:", line);
+       for (i = 0; i < actual; i++)
+               verbose_printf(" %ld", actual_sizes[i]);
+       verbose_printf("\n");
+
+       if (((expected <= max) && (expected != actual))
+           || ((expected > max) && (actual < max)))
                FAIL("Line %i: Wrong number of sizes returned -- expected %i "
-                       "got %i", line, expected, actual);
+                    "got %i", line, expected, actual);
+       else if (actual > max)
+               FAIL("Line %i: %i sizes returned > maximum %i",
+                    line, actual, max);
 
-       for (i = 0; i < expected; i++) {
-               for (j = 0; j < actual; j++)
-                       if (expected_sizes[i] == actual_sizes[j])
-                               break;
-               if (j >= actual)
-                       FAIL("Line %i: Expected size %li not found in actual "
-                               "results", line, expected_sizes[i]);
-       }
        for (i = 0; i < actual; i++) {
                for (j = 0; j < expected; j++)
                        if (actual_sizes[i] == expected_sizes[j])
@@ -280,7 +287,13 @@ void validate_sizes(int line, long actual_sizes[], int 
actual, int max_actual,
                                "results", line, expected_sizes[i]);
        }
 
-       for (i = expected; i < max_actual; i++)
+       for (i = 0; i < actual; i++)
+               for (j = i+1; j < actual; j++)
+                       if (actual_sizes[i] == actual_sizes[j])
+                               FAIL("Line %i: Duplicate size %li at %i/%i",
+                                    line, actual_sizes[i], i, j);
+
+       for (i = actual; i < maxmax; i++)
                if (actual_sizes[i] != 42)
                        FAIL("Line %i: Wrote past official limit at %i",
                                line, i);
@@ -291,11 +304,10 @@ void validate_sizes(int line, long actual_sizes[], int 
actual, int max_actual,
 ({                                                                     \
        long __a[MAX] = { [0 ... MAX-1] = 42 };                         \
        int __na;                                                       \
-       int __l = (count < max) ? count : max;                          \
                                                                        \
        __na = func(__a, max);                                          \
                                                                        \
-       validate_sizes(__LINE__, __a, __na, MAX, expected, __l);        \
+       validate_sizes(__LINE__, __a, __na, max, MAX, expected, count); \
                                                                        \
        __na;                                                           \
 })

-- 
David Gibson                    | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_ _other_
                                | _way_ _around_!
http://www.ozlabs.org/~dgibson

------------------------------------------------------------------------------

_______________________________________________
Libhugetlbfs-devel mailing list
Libhugetlbfs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel

Reply via email to