File: sorttest.cc

package info (click to toggle)
wvstreams 4.0.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,420 kB
  • ctags: 6,518
  • sloc: cpp: 52,544; sh: 5,770; ansic: 810; makefile: 461; tcl: 114; perl: 18
file content (153 lines) | stat: -rw-r--r-- 3,649 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 *
 * Test program for sorted iterators on lists.
 *
 * Correct output:
 *      Frontwards: eight five four nine one seven six ten three two 
 *      Unsorted:   one two three four five six seven eight nine ten 
 *      Backwards:  two three ten six seven one nine four five eight 
 *
 */
#include <stdio.h>
#include "wvstring.h"
#include "wvlinklist.h"
#include "wvhashtable.h"
#include "wvscatterhash.h"
#include "wvsorter.h"

DeclareWvList(WvString);
DeclareWvTable(WvString);
DeclareWvScatterTable2(WvStringTable2, WvString);

int apples_to_oranges(const WvString *a, const WvString *b)
{
    return strcmp(*a, *b);
}

int oranges_to_apples(const WvString *a, const WvString *b)
{
    return -strcmp(*a, *b);
}


int main()
{
    free(malloc(1));

    {
	// sorted linked list
	printf("\nLinked list sorter test:\n");
	WvStringList l;
	l.append(new WvString("one"), true);
	l.append(new WvString("two"), true);
	l.append(new WvString("three"), true);
	l.append(new WvString("four"), true);
	l.append(new WvString("five"), true);
	l.append(new WvString("six"), true);
	l.append(new WvString("seven"), true);
	l.append(new WvString("eight"), true);
	l.append(new WvString("nine"), true);
	l.append(new WvString("ten"), true);
	
	printf("Frontwards: ");
	{
	    WvStringList::Sorter s(l, apples_to_oranges);
	    for(s.rewind(); s.next();)
		printf("%s ", (const char *) s());
	}
	
	printf("\nUnsorted:   ");
	{
	    WvStringList::Iter i(l);
	    for(i.rewind(); i.next();)
		printf("%s ", (const char *) i());
	}
	
	printf("\nBackwards:  ");
	{
	    WvStringList::Sorter s(l, oranges_to_apples);
	    for(s.rewind(); s.next();)
		printf("%s ", (const char *) s());
	}
    }

    {
	// sorted hash table
	printf("\n\nHash table sorter test:\n");
	WvStringTable t(3);
	t.add(new WvString("one"), true);
	t.add(new WvString("two"), true);
	t.add(new WvString("three"), true);
	t.add(new WvString("four"), true);
	t.add(new WvString("five"), true);
	t.add(new WvString("six"), true);
	t.add(new WvString("seven"), true);
	t.add(new WvString("eight"), true);
	t.add(new WvString("nine"), true);
	t.add(new WvString("ten"), true);
	
	printf("Frontwards: ");
	{
	    WvStringTable::Sorter s(t, apples_to_oranges);
	    for (s.rewind(); s.next(); )
		printf("%s ", (const char *) s());
	}
	
	printf("\nUnsorted:   ");
	{
	    WvStringTable::Iter i(t);
	    for (i.rewind(); i.next(); )
		printf("%s ", (const char *) i());
	}
	
	printf("\nBackwards:  ");
	{
	    WvStringTable::Sorter s(t, oranges_to_apples);
	    for (s.rewind(); s.next(); )
		printf("%s ", (const char *) s());
	}
    }
    
    {
	// sorted scatter hash table
	printf("\n\nScatterHash table sorter test:\n");
	WvStringTable2 t(3);
	t.add(new WvString("one"), true);
	t.add(new WvString("two"), true);
	t.add(new WvString("three"), true);
	t.add(new WvString("four"), true);
	t.add(new WvString("five"), true);
	t.add(new WvString("six"), true);
	t.add(new WvString("seven"), true);
	t.add(new WvString("eight"), true);
	t.add(new WvString("nine"), true);
	t.add(new WvString("ten"), true);
	
	printf("Frontwards: ");
	{
	    WvStringTable2::Sorter s(t, apples_to_oranges);
	    for (s.rewind(); s.next(); )
		printf("%s ", (const char *) s());
	}
	
	printf("\nUnsorted:   ");
	{
	    WvStringTable2::Iter i(t);
	    for (i.rewind(); i.next(); )
		printf("%s ", (const char *) i());
	}
	
	printf("\nBackwards:  ");
	{
	    WvStringTable2::Sorter s(t, oranges_to_apples);
	    for (s.rewind(); s.next(); )
		printf("%s ", (const char *) s());
	}
	
	printf("\n");
    }

    return 0;
}