File: probabilitySetTest.m

package info (click to toggle)
oolite 1.77.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,264 kB
  • ctags: 5,362
  • sloc: objc: 132,090; ansic: 10,457; python: 2,225; sh: 1,325; makefile: 332; perl: 259; xml: 125; php: 5
file content (133 lines) | stat: -rw-r--r-- 3,083 bytes parent folder | download
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
#import <Foundation/Foundation.h>
#import "OOProbabilitySet.h"
#import "legacy_random.h"
#undef NSLog
#undef NSLogv


static OOProbabilitySet *SetUpTestSet1(void);
static OOProbabilitySet *SetUpTestSet2(void);
static OOProbabilitySet *SetUpTestSet3(void);
static void PresentHistogram(NSCountedSet *histogram);


@interface OOProbabilitySet (SekritInternalStuff)

- (id) privObjectForWeight:(float)target;

@end


int main (int argc, const char * argv[])
{
	srandomdev();
	ranrot_srand(random());
	
	OOProbabilitySet *pset = nil;
	
#if 0
	// Test basic lookup
	pset = SetUpTestSet1();
	float f;
	
	for (f = -1; f <= [pset count] + 1; f += 0.25)
	{
		NSLog(@"%f: %@", f, [pset privObjectForWeight:f]);
	}
#endif
	
	// Test probabilities
	pset = SetUpTestSet2();
	NSLog(@"%@", pset);
	unsigned count = 1000000;
	NSCountedSet *histogram = [[NSCountedSet alloc] init];
	while (count--)
	{
		id object = [pset randomObject];
		if (object == nil)  object = [NSNull null];
		[histogram addObject:object];
	}
	
	// Test non-empty all-zero set
	pset = SetUpTestSet3();
	NSLog(@"Random item from set with all-zero weights: %@", [pset randomObject]);
	
	PresentHistogram(histogram);
	
	return 0;
}


static OOProbabilitySet *SetUpTestSet1(void)
{
	// Create a probability set with 20 ordered NSNumbers, all of weight one.
	enum { kCount = 20 };
	unsigned i;
	float weights[kCount];
	id objects[kCount];
	
	for (i = 0; i < kCount; ++i)
	{
		objects[i] = [NSNumber numberWithInt:i+1];
		weights[i] = 1.0f;
	}
	
	return [OOProbabilitySet probabilitySetWithObjects:objects weights:weights count:kCount];
}


static OOProbabilitySet *SetUpTestSet2(void)
{
#if 0
	// Also test plist-based set-up as a bonus.
	id plist = [@"{ objects = (Five, One, Three, Zero, \"Also one\" ); weights = (5, 1, 3, 0, 1); }" propertyList];
	return [OOProbabilitySet probabilitySetWithPropertyListRepresentation:plist];
#else
	OOMutableProbabilitySet *pset = [OOMutableProbabilitySet probabilitySet];
	[pset setWeight:5 forObject:@"Five"];
	[pset setWeight:5 forObject:@"Three"];	// To test replacement
	[pset setWeight:3 forObject:@"Three"];
	[pset setWeight:0 forObject:@"Zero"];
	[pset setWeight:1 forObject:@"One"];
	[pset setWeight:1 forObject:@"Also one"];
	
	return pset;
#endif
}


static OOProbabilitySet *SetUpTestSet3(void)
{
	OOMutableProbabilitySet *pset = [OOMutableProbabilitySet probabilitySet];
	[pset setWeight:0 forObject:@"A"];
	[pset setWeight:0 forObject:@"B"];
	[pset setWeight:0 forObject:@"C"];
	return [pset copy];
}


static void PresentHistogram(NSCountedSet *histogram)
{
	NSEnumerator			*objEnum = nil;
	id						obj = nil;
	
	for (objEnum = [histogram objectEnumerator]; (obj = [objEnum nextObject]); )
	{
		NSLog(@"%@: %lu", obj, [histogram countForObject:obj]);
	}
}


void OOLogWithFunctionFileAndLine(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inFormat, ...)
{
	va_list args;
	va_start(args, inFormat);
	NSLogv(inFormat, args);
	va_end(args);
}


BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
{
	return YES;
}