cfad47cfa3/t3compiler/tads3/test/test_sort.cpp

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
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
#define countof(arr) (sizeof(arr)/sizeof((arr)[0]))
66
67
int main()
68
{
69
    static int ints1[] = { 1, 5, 2, 10, 2, 7, 4, 3, 9 };
70
    static int ints2[] = { 1, 5, 6, 10, 2, 7, 4, 3, 9, 8 };
71
    static int ints3[] = { 11 };
72
    static int ints4[] = { 9, 1 };
73
    static int ints5[] = { 9, 1, 3 };
74
    static int ints6[] = { 17, 17, 17, 17, 1 };
75
    static char *strs[] =
76
    {
77
        "hello",
78
        "there",
79
        "how",
80
        "are",
81
        "you",
82
        "today"
83
    };
84
    vm_globals *vmg__ = 0;
85
    CVmQSortInt int_sorter;
86
    CVmQSortStr str_sorter;
87
    int i;
88
89
    /* initialize for testing */
90
    test_init();
91
92
    int_sorter.arr_ = ints1;
93
    int_sorter.sort(vmg_ 0, countof(ints1) - 1);
94
    printf("ints1:\n");
95
    for (i = 0 ; i < countof(ints1) ; ++i)
96
        printf("   %d\n", ints1[i]);
97
98
    int_sorter.arr_ = ints2;
99
    int_sorter.sort(vmg_ 0, countof(ints2) - 1);
100
    printf("ints2:\n");
101
    for (i = 0 ; i < countof(ints2) ; ++i)
102
        printf("   %d\n", ints2[i]);
103
104
    int_sorter.arr_ = ints3;
105
    int_sorter.sort(vmg_ 0, countof(ints3) - 1);
106
    printf("ints3:\n");
107
    for (i = 0 ; i < countof(ints3) ; ++i)
108
        printf("   %d\n", ints3[i]);
109
110
    int_sorter.arr_ = ints4;
111
    int_sorter.sort(vmg_ 0, countof(ints4) - 1);
112
    printf("ints4:\n");
113
    for (i = 0 ; i < countof(ints4) ; ++i)
114
        printf("   %d\n", ints4[i]);
115
116
    int_sorter.arr_ = ints5;
117
    int_sorter.sort(vmg_ 0, countof(ints5) - 1);
118
    printf("ints5:\n");
119
    for (i = 0 ; i < countof(ints5) ; ++i)
120
        printf("   %d\n", ints5[i]);
121
122
    int_sorter.arr_ = ints6;
123
    int_sorter.sort(vmg_ 0, countof(ints6) - 1);
124
    printf("ints6:\n");
125
    for (i = 0 ; i < countof(ints6) ; ++i)
126
        printf("   %d\n", ints6[i]);
127
128
    str_sorter.arr_ = (const char **)strs;
129
    str_sorter.sort(vmg_ 0, countof(strs) - 1);
130
    printf("strs:\n");
131
    for (i = 0 ; i < countof(strs) ; ++i)
132
        printf("   %s\n", strs[i]);
133
134
    return 0;
135
}
136
137