Module: wine Branch: master Commit: e95938c2d612e90c18e0772354add1a6949995fe URL: http://source.winehq.org/git/wine.git/?a=commit;h=e95938c2d612e90c18e0772354...
Author: Piotr Caban piotr@codeweavers.com Date: Wed May 21 11:57:24 2014 +0200
msvcrt: Add qsort_s tests.
---
dlls/msvcrt/tests/misc.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+)
diff --git a/dlls/msvcrt/tests/misc.c b/dlls/msvcrt/tests/misc.c index 523d277..7b940f1 100644 --- a/dlls/msvcrt/tests/misc.c +++ b/dlls/msvcrt/tests/misc.c @@ -32,6 +32,8 @@ static int (__cdecl *p_set_doserrno)(int); static int (__cdecl *p_set_errno)(int); static void (__cdecl *p__invalid_parameter)(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t); +static void (__cdecl *p_qsort_s)(void*, MSVCRT_size_t, MSVCRT_size_t, + int (__cdecl*)(void*, const void*, const void*), void*);
static void init(void) { @@ -45,6 +47,7 @@ static void init(void) p_set_doserrno = (void *)GetProcAddress(hmod, "_set_doserrno"); p_set_errno = (void *)GetProcAddress(hmod, "_set_errno"); p__invalid_parameter = (void *)GetProcAddress(hmod, "_invalid_parameter"); + p_qsort_s = (void *)GetProcAddress(hmod, "qsort_s"); }
static void test_rand_s(void) @@ -354,6 +357,126 @@ static void test__invalid_parameter(void) p__invalid_parameter(NULL, NULL, NULL, 0, 0); }
+struct qsort_test +{ + int pos; + int *base; + + struct { + int l; + int r; + } cmp[64]; +}; + +int __cdecl qsort_comp(void *ctx, const void *l, const void *r) +{ + struct qsort_test *qt = ctx; + + if(qt) { + ok(qt->pos < 64, "qt->pos = %d\n", qt->pos); + ok(qt->cmp[qt->pos].l == (int*)l-qt->base, + "%d) l on %ld position\n", qt->pos, (long)((int*)l - qt->base)); + ok(qt->cmp[qt->pos].r == (int*)r-qt->base, + "%d) r on %ld position\n", qt->pos, (long)((int*)r - qt->base)); + qt->pos++; + } + + return *(int*)l%1000 - *(int*)r%1000; +} + +static void test_qsort_s(void) +{ + static const int nonstable_test[] = {9000, 8001, 7002, 6003, 1003, 5004, 4005, 3006, 2007}; + int tab[100], i; + + struct qsort_test small_sort = { + 0, tab, { + {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 5}, {7, 6}, + {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 5}, + {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, + {1, 0}, {2, 1}, {3, 2}, {4, 3}, + {1, 0}, {2, 1}, {3, 2}, + {1, 0}, {2, 1}, + {1, 0} + } + }, small_sort2 = { + 0, tab, { + {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, + {1, 0}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, + {1, 0}, {2, 1}, {3, 2}, {4, 2}, {5, 2}, + {1, 0}, {2, 1}, {3, 2}, {4, 3}, + {1, 0}, {2, 1}, {3, 2}, + {1, 0}, {2, 1}, + {1, 0} + } + }, quick_sort = { + 0, tab, { + {0, 4}, {0, 8}, {4, 8}, + {1, 4}, {2, 4}, {3, 4}, {5, 4}, {6, 4}, {7, 4}, {7, 4}, {6, 4}, + {6, 4}, + {8, 7}, + {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 4}, + {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 3}, + {1, 0}, {2, 1}, {3, 2}, {4, 2}, + {1, 0}, {2, 1}, {3, 2}, + {1, 0}, {2, 1}, + {1, 0} + } + }; + + if(!p_qsort_s) { + win_skip("qsort_s not available\n"); + return; + } + + for(i=0; i<8; i++) tab[i] = i; + p_qsort_s(tab, 8, sizeof(int), qsort_comp, &small_sort); + ok(small_sort.pos == 28, "small_sort.pos = %d\n", small_sort.pos); + for(i=0; i<8; i++) + ok(tab[i] == i, "tab[%d] = %d\n", i, tab[i]); + + for(i=0; i<8; i++) tab[i] = 7-i; + p_qsort_s(tab, 8, sizeof(int), qsort_comp, &small_sort2); + ok(small_sort2.pos == 28, "small_sort2.pos = %d\n", small_sort2.pos); + for(i=0; i<8; i++) + ok(tab[i] == i, "tab[%d] = %d\n", i, tab[i]); + + for(i=0; i<9; i++) tab[i] = i; + tab[5] = 1; + tab[6] = 2; + p_qsort_s(tab, 9, sizeof(int), qsort_comp, &quick_sort); + ok(quick_sort.pos == 34, "quick_sort.pos = %d\n", quick_sort.pos); + + /* show that qsort is not stable */ + for(i=0; i<9; i++) tab[i] = 8-i + 1000*(i+1); + tab[0] = 1003; + p_qsort_s(tab, 9, sizeof(int), qsort_comp, NULL); + for(i=0; i<9; i++) + ok(tab[i] == nonstable_test[i], "tab[%d] = %d, expected %d\n", i, tab[i], nonstable_test[i]); + + /* check if random data is sorted */ + srand(0); + for(i=0; i<100; i++) tab[i] = rand()%1000; + p_qsort_s(tab, 100, sizeof(int), qsort_comp, NULL); + for(i=1; i<100; i++) + ok(tab[i-1] <= tab[i], "data sorted incorrectly on position %d: %d <= %d\n", i, tab[i-1], tab[i]); + + /* test if random permutation is sorted correctly */ + for(i=0; i<100; i++) tab[i] = i; + for(i=0; i<100; i++) { + int b = rand()%100; + int e = rand()%100; + + if(b == e) continue; + tab[b] ^= tab[e]; + tab[e] ^= tab[b]; + tab[b] ^= tab[e]; + } + p_qsort_s(tab, 100, sizeof(int), qsort_comp, NULL); + for(i=0; i<100; i++) + ok(tab[i] == i, "data sorted incorrectly on position %d: %d\n", i, tab[i]); +} + START_TEST(misc) { int arg_c; @@ -380,4 +503,5 @@ START_TEST(misc) test__set_errno(); test__popen(arg_v[0]); test__invalid_parameter(); + test_qsort_s(); }