summaryrefslogtreecommitdiff
path: root/MLKLowLevelTests.m
blob: 34ec97fcc8d1af521ab2cef26c00d187d3dc525c (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* -*- mode: objc; coding: utf-8 -*- */
/* Toilet Lisp, a Common Lisp subset for the Étoilé runtime.
 * Copyright (C) 2008  Matthias Andreas Benkard.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
                           
#include <UnitKit/UnitKit.h>
#include <Foundation/Foundation.h>

#include "MLKCons.h"
#include "MLKDoubleFloat.h"
#include "MLKDynamicContext.h"
#include "MLKEnvironment.h"
#include "MLKPackage.h"
#include "MLKRatio.h"
#include "MLKReader.h"
#include "MLKReadtable.h"
#include "MLKSingleFloat.h"
#include "MLKSymbol.h"
#import "runtime-compatibility.h"

@interface MLKLowLevelTests : NSObject <UKTest>
@end


// static void MLKNSUncaughtExceptionHandler (NSException *exception)
// {
//   NSLog (@"Caught unhandled exception.\nName:%@\nReason:%@",
//          [exception name],
//          [exception reason]);
// }


@implementation MLKLowLevelTests
-(id) initForTest
{
  self = [super init];
  [MLKDynamicContext currentContext];

  //  NSSetUncaughtExceptionHandler (MLKNSUncaughtExceptionHandler);

  return self;
}


-(id) testCons
{
  id obj1 = @"Mulk.";
  id obj2 = LAUTORELEASE ([[NSMutableDictionary alloc] init]);

  MLKCons *cons2 = [MLKCons cons:obj1 with:obj2];
  MLKCons *cons3 = [MLKCons cons:obj1 with:nil];
  MLKCons *cons4 = [MLKCons cons:nil with:nil];
  MLKCons *cons5 = [MLKCons cons:nil with:obj2];

  MLKCons *cons6 = LAUTORELEASE ([[MLKCons alloc] initWithCar:obj1 cdr:obj2]);
  MLKCons *cons7 = LAUTORELEASE ([[MLKCons alloc] initWithCar:obj1 cdr:nil]);
  MLKCons *cons8 = LAUTORELEASE ([[MLKCons alloc] initWithCar:nil cdr:nil]);
  MLKCons *cons9 = LAUTORELEASE ([[MLKCons alloc] initWithCar:nil cdr:obj2]);

  UKTrue ([cons2 car] == obj1);
  UKTrue ([cons3 car] == obj1);
  UKFalse ([cons4 car] == obj1);
  UKFalse ([cons5 car] == obj1);
  UKTrue ([cons6 car] == obj1);
  UKTrue ([cons7 car] == obj1);
  UKFalse ([cons8 car] == obj1);
  UKFalse ([cons9 car] == obj1);

  UKTrue ([cons2 cdr] == obj2);
  UKFalse ([cons3 cdr] == obj2);
  UKFalse ([cons4 cdr] == obj2);
  UKTrue ([cons5 cdr] == obj2);
  UKTrue ([cons6 cdr] == obj2);
  UKFalse ([cons7 cdr] == obj2);
  UKFalse ([cons8 cdr] == obj2);
  UKTrue ([cons9 cdr] == obj2);
  
  [cons2 setCdr:obj1];
  UKTrue ([cons2 cdr] == obj1);

  [cons2 setCar:obj2];
  UKTrue ([cons2 car] == obj2);

  return nil;
}


-(id) testInitialReadtable
{
  MLKDynamicContext *ctx = [MLKDynamicContext currentContext];
  MLKReadtable *readtable = [ctx valueForSymbol:
                                   [[MLKPackage findPackage:@"COMMON-LISP"]
                                     intern:@"*READTABLE*"]];
  UKTrue ([readtable characterHasCase:'a']);
  UKTrue ([readtable characterHasCase:'x']);
  UKTrue ([readtable characterHasCase:'F']);
  UKTrue ([readtable characterHasCase:228]);  // ä
  UKTrue ([readtable characterHasCase:196]);  // Ä
  UKFalse ([readtable characterHasCase:'=']);
  UKFalse ([readtable characterHasCase:'.']);
  UKFalse ([readtable characterHasCase:223]);  // ß

  return nil;
}


-(id) testTokens
{
  UKObjectKindOf ([MLKReader readFromString:@"a"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"MULK"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"+"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"1-"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"1+"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"0AA0A"], MLKSymbol);
  
  UKObjectKindOf ([MLKReader readFromString:@"0AA0A"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"0\\aA0A"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"\\0"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"|abc def (mulk!)|"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"0\\.3"], MLKSymbol);

  UKObjectKindOf ([MLKReader readFromString:@"134651234"], MLKInteger);
  UKObjectKindOf ([MLKReader readFromString:@"223555."], MLKInteger);
  UKObjectKindOf ([MLKReader readFromString:@"-134651234"], MLKInteger);
  UKObjectKindOf ([MLKReader readFromString:@"-223555."], MLKInteger);
  UKObjectKindOf ([MLKReader readFromString:@"+134651234"], MLKInteger);
  UKObjectKindOf ([MLKReader readFromString:@"+223555."], MLKInteger);
  UKObjectKindOf ([MLKReader readFromString:@"-1."], MLKInteger);
  UKObjectKindOf ([MLKReader readFromString:@"+2"], MLKInteger);
  UKObjectKindOf ([MLKReader readFromString:@"3."], MLKInteger);
  UKObjectKindOf ([MLKReader readFromString:@"3"], MLKInteger);
  
  UKObjectKindOf ([MLKReader readFromString:@"55/11"], MLKRatio);
  UKObjectKindOf ([MLKReader readFromString:@"-55/11"], MLKRatio);

  UKObjectKindOf ([MLKReader readFromString:@"1234.5678e99"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"-1234.5678e99"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"+1234.5678e99"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"1234.5678e-99"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"1234.5678e+99"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"-1234.5678e-99"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"1234.5678"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"-1234.5678"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@".5678"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"-.5678"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"+.5678"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@".5678e3"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"-.5678e3"], MLKSingleFloat);
  UKObjectKindOf ([MLKReader readFromString:@"+.5678e3"], MLKSingleFloat);

  UKStringsEqual ([[MLKReader readFromString:@"a"] name], @"A");
  UKStringsEqual ([[MLKReader readFromString:@"1+"] name], @"1+");
  UKStringsEqual ([[MLKReader readFromString:@"mulkmulk"] name], @"MULKMULK");
  UKStringsEqual ([[MLKReader readFromString:@"ABCDefghIJKL"] name], @"ABCDEFGHIJKL");
  UKStringsEqual ([[MLKReader readFromString:@"class-name"] name], @"CLASS-NAME");
  UKStringsEqual ([[MLKReader readFromString:@"\\class-\\name"] name], @"cLASS-nAME");
  UKStringsEqual ([[MLKReader readFromString:@"|Class Name|"] name], @"Class Name");
  UKStringsEqual ([[MLKReader readFromString:@"class\\ name"] name], @"CLASS NAME");
  UKStringsEqual ([[MLKReader readFromString:@"\\100"] name], @"100");

  UKStringsEqual ([[MLKReader readFromString:@"a b c d e"] name], @"A");

  UKObjectKindOf ([MLKReader readFromString:@"cl::if"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"cl:if"], MLKSymbol);
  UKObjectKindOf ([MLKReader readFromString:@"cl-user::mulk"], MLKSymbol);
  UKObjectsSame ([MLKReader readFromString:@"common-lisp-user::a"],
                 [MLKReader readFromString:@"cl-user::a"]);
  UKObjectsSame ([MLKReader readFromString:@"a"],
                 [MLKReader readFromString:@"cl-user::a"]);

  return nil;
}


-(id) testParenReading
{
  UKObjectKindOf ([MLKReader readFromString:@"(1 2)"], MLKCons);
  UKObjectKindOf ([MLKReader readFromString:@"(1 . 2)"], MLKCons);
  UKObjectKindOf ([MLKReader readFromString:@"(a b)"], MLKCons);

  UKNil ([MLKReader readFromString:@"()"]);

  UKObjectKindOf ([[MLKReader readFromString:@"(1 . 2)"] car], MLKInteger);
  UKObjectKindOf ([[MLKReader readFromString:@"(1 . 2)"] cdr], MLKInteger);
  
  UKObjectKindOf ([[MLKReader readFromString:@"(a b)"] car], MLKSymbol);
  UKObjectKindOf ([[MLKReader readFromString:@"(a b)"] cdr], MLKCons);
  
  UKObjectKindOf ([[MLKReader readFromString:@"((a) b)"] car], MLKCons);

  return nil;
}


-(id) testStuff
{
  // UKPass(); UKFail();
  //  UKNotNil (nil);
  //  UKTrue (1);
  //  UKStringsNotEqual (@"a", @"b");
  // UKPass();
  return nil;
}
@end