1//
2//  FastQueueTest.m
3//  ANTLR
4//
5//  Created by Ian Michell on 13/05/2010.
6//  Copyright 2010 Ian Michell and Alan Condit. All rights reserved.
7//
8
9#import "FastQueueTest.h"
10#import "FastQueue.h"
11#import "ANTLRError.h"
12#import "RuntimeException.h"
13
14@implementation FastQueueTest
15
16-(void) testInit
17{
18	FastQueue *queue = [[FastQueue newFastQueue] retain];
19	STAssertNotNil(queue, @"Queue was not created and was nil");
20	[queue release];
21}
22
23-(void) testAddAndGet
24{
25	FastQueue *queue = [[FastQueue newFastQueue] retain];
26	STAssertNotNil(queue, @"Queue was not created and was nil");
27	[queue addObject:@"My String"];
28	STAssertTrue([[queue objectAtIndex:0] isKindOfClass:[NSString class]], @"First object is not a NSString");
29	STAssertEquals([queue objectAtIndex:0], @"My String", @"Object at index zero is invalid");
30	STAssertTrue([queue size] == 1, @"Queue is the wrong size: %d", [queue size]);
31	[queue release];
32}
33
34-(void) testInvalidElementIndex
35{
36    //RuntimeException *NoSuchElementException = [NoSuchElementException newException:@"No such element exception"];
37    id retVal;
38	FastQueue *queue = [[FastQueue newFastQueue] retain];
39	STAssertNotNil(queue, @"Queue was not created and was nil");
40	@try
41	{
42		retVal = [queue objectAtIndex:100];
43	}
44	@catch (NoSuchElementException *e)
45	{
46		STAssertTrue([[e name] isEqualTo:@"NoSuchElementException"], @"Exception was not type: NoSuchElementException -- %@", [e name]);
47		return;
48	}
49	STFail(@"Exception NoSuchElementException was not thrown -- %@", [retVal name]);
50    [queue release];
51}
52
53-(void) testHead
54{
55	FastQueue *queue = [[FastQueue newFastQueue] retain];
56	STAssertNotNil(queue, @"Queue was not created and was nil");
57	[queue addObject:@"Item 1"];
58	[queue addObject:@"Item 2"];
59	[queue addObject:@"Item 3"];
60	id head = [queue head];
61	STAssertNotNil(head, @"Object returned from head is nil");
62	STAssertEquals(head, @"Item 1", @"Object returned was not first item in");
63	[queue release];
64}
65
66-(void) testClear
67{
68	FastQueue *queue = [[FastQueue newFastQueue] retain];
69	STAssertNotNil(queue, @"Queue was not created and was nil");
70	[queue addObject:@"Item 1"];
71	[queue addObject:@"Item 2"];
72	[queue addObject:@"Item 3"];
73	STAssertTrue([queue size] == 3, @"Queue was too small, was: %d expected 3", [queue size]);
74	[queue reset];
75	STAssertTrue([queue size] == 0, @"Queue is not empty, it's still %d", [queue size]);
76	[queue release];
77}
78
79-(void) testDescription
80{
81	FastQueue *queue = [[FastQueue newFastQueue] retain];
82	STAssertNotNil(queue, @"Queue was not created and was nil");
83	[queue addObject:@"My"];
84	[queue addObject:@"String"];
85	STAssertTrue([[queue description] isEqualToString:@"My String"], @"Queue description was not right, got: \"%@\" expected: \"My String\"", [queue description]);
86	[queue release];
87}
88
89-(void) testRemove
90{
91	FastQueue *queue = [[FastQueue newFastQueue] retain];
92	STAssertNotNil(queue, @"Queue was not created and was nil");
93	[queue addObject:@"My"];
94	[queue addObject:@"String"];
95	STAssertTrue([queue size] == 2, @"Queue not the correct size, was: %d expected 2", [queue size]);
96	[queue remove];
97	STAssertTrue([queue size] == 1, @"Queue not the correct size, was %d expected 1", [queue size]);
98	[queue remove]; // test that the queue is reset when we remove the last object...
99	STAssertTrue([queue size] == 0, @"Queue was not reset, when we hit the buffer, was still %d", [queue size]);
100	[queue release];
101}
102
103@end
104