| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | test_sort.cpp - sorting test |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 05/14/00 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "vmglob.h" |
| 29 | #include "vmsort.h" |
| 30 | #include "t3test.h" |
| 31 | |
| 32 | class CVmQSortInt: public CVmQSortData |
| 33 | { |
| 34 | public: |
| 35 | CVmQSortInt() { arr_ = 0; } |
| 36 | CVmQSortInt(int *arr) { arr_ = arr; } |
| 37 | int compare(VMG_ size_t a, size_t b) { return arr_[a] - arr_[b]; } |
| 38 | void exchange(VMG_ size_t a, size_t b) |
| 39 | { |
| 40 | int tmp = arr_[a]; |
| 41 | arr_[a] = arr_[b]; |
| 42 | arr_[b] = tmp; |
| 43 | } |
| 44 | |
| 45 | int *arr_; |
| 46 | }; |
| 47 | |
| 48 | class CVmQSortStr: public CVmQSortData |
| 49 | { |
| 50 | public: |
| 51 | CVmQSortStr() { arr_ = 0; } |
| 52 | CVmQSortStr(const char **arr) { arr_ = arr; } |
| 53 | int compare(VMG_ size_t a, size_t b) |
| 54 | { return strcmp(arr_[a], arr_[b]); } |
| 55 | void exchange(VMG_ size_t a, size_t b) |
| 56 | { |
| 57 | const char *tmp = arr_[a]; |
| 58 | arr_[a] = arr_[b]; |
| 59 | arr_[b] = tmp; |
| 60 | } |
| 61 | |
| 62 | const char **arr_; |
| 63 | }; |
| 64 | |
| 65 | int main() |
| 66 | { |
| 67 | static int ints1[] = { 1, 5, 2, 10, 2, 7, 4, 3, 9 }; |
| 68 | static int ints2[] = { 1, 5, 6, 10, 2, 7, 4, 3, 9, 8 }; |
| 69 | static int ints3[] = { 11 }; |
| 70 | static int ints4[] = { 9, 1 }; |
| 71 | static int ints5[] = { 9, 1, 3 }; |
| 72 | static int ints6[] = { 17, 17, 17, 17, 1 }; |
| 73 | static const char *strs[] = |
| 74 | { |
| 75 | "hello", |
| 76 | "there", |
| 77 | "how", |
| 78 | "are", |
| 79 | "you", |
| 80 | "today" |
| 81 | }; |
| 82 | vm_globals *vmg__ = 0; |
| 83 | CVmQSortInt int_sorter; |
| 84 | CVmQSortStr str_sorter; |
| 85 | int i; |
| 86 | |
| 87 | /* initialize for testing */ |
| 88 | test_init(); |
| 89 | |
| 90 | int_sorter.arr_ = ints1; |
| 91 | int_sorter.sort(vmg_ 0, countof(ints1) - 1); |
| 92 | printf("ints1:\n"); |
| 93 | for (i = 0 ; i < countof(ints1) ; ++i) |
| 94 | printf(" %d\n", ints1[i]); |
| 95 | |
| 96 | int_sorter.arr_ = ints2; |
| 97 | int_sorter.sort(vmg_ 0, countof(ints2) - 1); |
| 98 | printf("ints2:\n"); |
| 99 | for (i = 0 ; i < countof(ints2) ; ++i) |
| 100 | printf(" %d\n", ints2[i]); |
| 101 | |
| 102 | int_sorter.arr_ = ints3; |
| 103 | int_sorter.sort(vmg_ 0, countof(ints3) - 1); |
| 104 | printf("ints3:\n"); |
| 105 | for (i = 0 ; i < countof(ints3) ; ++i) |
| 106 | printf(" %d\n", ints3[i]); |
| 107 | |
| 108 | int_sorter.arr_ = ints4; |
| 109 | int_sorter.sort(vmg_ 0, countof(ints4) - 1); |
| 110 | printf("ints4:\n"); |
| 111 | for (i = 0 ; i < countof(ints4) ; ++i) |
| 112 | printf(" %d\n", ints4[i]); |
| 113 | |
| 114 | int_sorter.arr_ = ints5; |
| 115 | int_sorter.sort(vmg_ 0, countof(ints5) - 1); |
| 116 | printf("ints5:\n"); |
| 117 | for (i = 0 ; i < countof(ints5) ; ++i) |
| 118 | printf(" %d\n", ints5[i]); |
| 119 | |
| 120 | int_sorter.arr_ = ints6; |
| 121 | int_sorter.sort(vmg_ 0, countof(ints6) - 1); |
| 122 | printf("ints6:\n"); |
| 123 | for (i = 0 ; i < countof(ints6) ; ++i) |
| 124 | printf(" %d\n", ints6[i]); |
| 125 | |
| 126 | str_sorter.arr_ = (const char **)strs; |
| 127 | str_sorter.sort(vmg_ 0, countof(strs) - 1); |
| 128 | printf("strs:\n"); |
| 129 | for (i = 0 ; i < countof(strs) ; ++i) |
| 130 | printf(" %s\n", strs[i]); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 |