Debian Codegraph @

tests/code/testcases/testcastleutils.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestCastleUtils" -*-
2 {
3 Copyright 2004-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Test CastleUtils unit. }
18 unit TestCastleUtils;
19
20 { Needed for UNIX symbol in Delphi. }
21 {$I ../../../src/common_includes/castleconf.inc}
22
23 { $define CASTLEUTILS_SPEED_TESTS}
24
25 interface
26
27 uses
28 Classes, SysUtils, CastleTester;
29
30 type
31 TTestCastleUtils = class(TCastleTestCase)
32 published
33 procedure TestMilisecTime;
34 procedure TestIndexMinMax_RestOf3DCoords;
35 procedure TestCheckIsMemCharFilled;
36 procedure TestSmallest2Exp;
37 procedure TestPathDelim;
38 procedure TestOSError;
39 procedure TestStrings;
40 procedure TestOthers;
41 procedure TestIntSqrt;
42 procedure TestDivMod;
43 procedure TestClamp;
44 procedure TestSimpleMath;
45 procedure TestMinMax;
46 procedure TestFileExt;
47 procedure TestFloatModulo;
48 procedure TestRandomIntRange;
49 procedure TestRandomIntRangeInclusive;
50 procedure TestStrToFloatDot;
51 procedure TestIsPathAbsolute;
52 procedure TestIsPathAbsoluteOnDrive;
53 procedure TestRestOf3DCoords;
54 procedure TestRestOf3DCoordsCycle;
55 procedure TestDecimalSeparator;
56 procedure TestFloatToStrDisplay;
57 procedure TestDeg;
58 end;
59
60 implementation
61
62 uses
63 {$ifdef MSWINDOWS} Windows, {$endif}
64 {$if defined(UNIX) and defined(FPC)} Unix, BaseUnix, {$endif}
65 Math, CastleUtils, CastleTimeUtils, CastleVectors, CastleLog,
66 CastleTestUtils;
67
68 {$warnings off} // knowingly using deprecated, to check they are working
69
70 procedure TTestCastleUtils.TestMilisecTime;
71 const t1: TMilisecTime = High(TMilisecTime) - 10;
72 t2: TMilisecTime = 11;
73 begin
74 {Writeln(MilisecTimesSubtract(t2, t1));}
75 { 22 - bo jak dodam 10 do t1 to bede na High, +1 bede na 0, +11 bede na 11;
76 Razem 11+1+10 = 22; }
77 AssertTrue(MilisecTimesSubtract(t2, t1) = 22);
78 end;
79
80 {$warnings on}
81
82 procedure TTestCastleUtils.TestIndexMinMax_RestOf3DCoords;
83 var
84 a: array[0..2]of Double;
85 i: Integer;
86 C1, C2, CM: T3DAxis;
87 begin
88 for i := 1 to 100 do
89 begin
90 a[0] := Random;
91 a[1] := Random;
92 a[2] := Random;
93
94 cm := IndexMin(a[0], a[1], a[2]);
95 RestOf3DCoords(cm, c1, c2);
96 AssertTrue( (a[cm] <= a[c1]) and (a[cm] <= a[c2]) );
97
98 cm := IndexMax(a[0], a[1], a[2]);
99 RestOf3DCoords(cm, c1, c2);
100 AssertTrue( (a[cm] >= a[c1]) and (a[cm] >= a[c2]) );
101 end;
102 end;
103
104 procedure WritelnMem(const Data; Size: Integer);
105 var p: PChar;
106 begin
107 p := @Data;
108 while Size > 0 do
109 begin
110 if Ord(p^) < Ord(' ') then Write('(',Ord(p^), ')') else Write(p^);
111 Inc(p);
112 Dec(Size);
113 end;
114 Writeln;
115 end;
116
117 function NaiveIsMemCharFilled(const Data; Size: Integer; AChar: AnsiChar): boolean;
118 begin
119 result := CheckIsMemCharFilled(Data, Size, AChar) = -1;
120 end;
121
122 procedure TTestCastleUtils.TestCheckIsMemCharFilled;
123
124 procedure TimeTestIsMemCharFilled(SizeOfA: Integer);
125 var
126 SpeedTest_i: Cardinal;
127 SpeedTest_Time0, SpeedTest_Time1, SpeedTest_Time2: Double;
128 StartTime: TProcessTimerResult;
129 SpeedTest_Cycles: Integer;
130 SpeedTest_SlowerName, SpeedTest_FasterName: String;
131 pa: pointer;
132 begin
133 { testowanie na danych ktore nie sa CharFilled jest problematyczne.
134 Czas wtedy bedzie zalezal liniowo od pozycji na ktorej jest przeklamanie.
135 Zreszta dla danych losowych mamy prawie pewnosc ze przeklamanie bedzie
136 juz na 1 pozycji, wiec jaki sens tu testowac szybkosc ?
137 sChcemy sprawdzic szybkosc dla przypadku pesymistycznego. }
138 pa := GetMem(SizeOfA);
139 try
140 FillChar(pa^, SizeOfA, 'x');
141
142 SpeedTest_Cycles := 100000;
143 SpeedTest_SlowerName := 'NaiveIsMemCharFilled';
144 SpeedTest_FasterName := 'IsMemCharFilled';
145 WritelnLog('SPEED TEST IsMemCharFilled on SizeOfA = '+ IntToStr(SizeOfA));
146
147 StartTime := ProcessTimer;
148 for SpeedTest_i := 1 to SpeedTest_Cycles do ;
149 SpeedTest_Time0 := ProcessTimerSeconds(ProcessTimer, StartTime);
150 WritelnLog(Format('Empty loop = %f',[SpeedTest_Time0]));
151
152 StartTime := ProcessTimer;
153 for SpeedTest_i := 1 to SpeedTest_Cycles do
154 IsMemCharFilled(pa^, SizeOfA, 'x');
155 SpeedTest_Time1 := ProcessTimerSeconds(ProcessTimer, StartTime);
156 WritelnLog(SpeedTest_FasterName, Format(' = %f',[SpeedTest_Time1]));
157
158 StartTime := ProcessTimer;
159 // measure slower function
160 for SpeedTest_i := 1 to SpeedTest_Cycles do
161 NaiveIsMemCharFilled(pa^, SizeOfA, 'x');
162 SpeedTest_Time2 := ProcessTimerSeconds(ProcessTimer, StartTime);
163 WritelnLog(SpeedTest_SlowerName, Format(' = %f',[SpeedTest_Time2]));
164
165 WritelnLog(
166 SpeedTest_FasterName + ' is faster than ' +
167 SpeedTest_SlowerName + ' by ' +
168 Format('%f', [(SpeedTest_Time2-SpeedTest_Time0)/
169 (SpeedTest_Time1-SpeedTest_Time0)]));
170
171 finally FreeMem(pa) end;
172 end;
173
174 var
175 a: array[0..100]of AnsiChar;
176 SizeOfA: Integer;
177 i, YPos: Integer;
178 begin
179 for i := 1 to 1000 do
180 begin
181 { losuje SizeOfA bo chce zeby sprawdzil czy funkcje
182 [Check]IsMemCharFilled dzialaja dla roznych Size.
183 Zawsze niech SizeOfA >= 2, przypadki SizeOfA < 2 sprawdzimy pozniej osobno. }
184 if Random(2) = 0 then SizeOfA := Random(4) else SizeOfA := Random(100);
185 SizeOfA := SizeOfA + 2;
186
187 FillChar(a, SizeOfA, 'x');
188 if Random(2) = 0 then
189 begin
190 AssertTrue(CheckIsMemCharFilled(a, SizeOfA, 'x') = -1);
191 AssertTrue(IsMemCharFilled(a, SizeOfA, 'x'));
192 if not (CheckIsMemCharFilled(a, SizeOfA, 'y') = 0) then
193 begin
194 WritelnMem(a, SizeOfA);
195 raise Exception.Create('failed');
196 end;
197 AssertTrue(not IsMemCharFilled(a, SizeOfA, 'y'));
198 end else
199 begin
200 YPos := Random(SizeOfA);
201 a[YPos] := 'y';
202 AssertTrue(CheckIsMemCharFilled(a, SizeOfA, 'x') = YPos);
203 AssertTrue(not IsMemCharFilled(a, SizeOfA, 'x'));
204 if YPos = 0 then
205 AssertTrue(CheckIsMemCharFilled(a, SizeOfA, 'y') = 1) else
206 AssertTrue(CheckIsMemCharFilled(a, SizeOfA, 'y') = 0);
207 AssertTrue(not IsMemCharFilled(a, SizeOfA, 'y'));
208 end;
209 end;
210
211 { sprawdz dla SizeOfA = 1 }
212 a[0] := 'k';
213 AssertTrue(CheckIsMemCharFilled(a, 1, 'k') = -1);
214 AssertTrue(IsMemCharFilled(a, 1, 'k'));
215 AssertTrue(CheckIsMemCharFilled(a, 1, 'g') = 0);
216 AssertTrue(not IsMemCharFilled(a, 1, 'g'));
217
218 { sprawdz dla SizeOfA = 0 (zawsze odpowiedz bedzie brzmiala -1 / true,
219 tak jak kwantyfikator ForAll jest zawsze true dla pustego zbioru...) }
220 AssertTrue(CheckIsMemCharFilled(a, 0, 'd') = -1);
221 AssertTrue(IsMemCharFilled(a, 0, 'd'));
222
223 {$ifdef CASTLEUTILS_SPEED_TESTS}
224 TimeTestIsMemCharFilled(100);
225 TimeTestIsMemCharFilled(1000);
226 TimeTestIsMemCharFilled(10000);
227 {$endif}
228 end;
229
230 procedure TTestCastleUtils.TestSmallest2Exp;
231 var i: Cardinal;
232 begin
233 AssertTrue(Smallest2Exponent(0) = -1);
234 AssertTrue(Smallest2Power(0) = 0);
235 AssertTrue(Smallest2Exponent(1) = 0);
236 AssertTrue(Smallest2Power(1) = 1);
237 AssertTrue(Smallest2Exponent(2) = 1);
238 AssertTrue(Smallest2Power(2) = 2);
239 for i := 3 to 4 do
240 begin
241 AssertTrue(Smallest2Exponent(i) = 2);
242 AssertTrue(Smallest2Power(i) = 4);
243 end;
244 for i := 5 to 8 do
245 begin
246 AssertTrue(Smallest2Exponent(i) = 3);
247 AssertTrue(Smallest2Power(i) = 8);
248 end;
249 end;
250
251 procedure TTestCastleUtils.TestPathDelim;
252 {$ifdef UNIX}
253 begin
254 AssertTrue(InclPathDelim('/c/blah/') = '/c/blah/');
255 AssertTrue(InclPathDelim('/c/blah' ) = '/c/blah/');
256 AssertTrue(ExclPathDelim('/c/blah/') = '/c/blah' );
257 AssertTrue(ExclPathDelim('/c/blah' ) = '/c/blah' );
258 {$endif}
259 {$ifdef MSWINDOWS}
260 begin
261 AssertTrue(InclPathDelim('c:\blah\') = 'c:\blah\');
262 AssertTrue(InclPathDelim('c:\blah' ) = 'c:\blah\');
263 AssertTrue(ExclPathDelim('c:\blah\') = 'c:\blah' );
264 AssertTrue(ExclPathDelim('c:\blah' ) = 'c:\blah' );
265
266 AssertTrue(InclPathDelim('c:\blah/') = 'c:\blah/');
267 AssertTrue(ExclPathDelim('c:\blah/') = 'c:\blah' );
268 {$endif}
269 end;
270
271 procedure TTestCastleUtils.TestOSError;
272 begin
273 // TODO: add some test for Delphi + Linux
274 {$if defined(MSWINDOWS) or defined(FPC)}
275 try
276 OSCheck(
277 {$ifdef MSWINDOWS} Windows.MoveFile('some_not_existing_file_name', 'foo') {$endif}
278 {$if defined(UNIX) and defined(FPC)} FpRename('some_not_existing_file_name', 'foo') <> -1 {$endif}
279 );
280 raise Exception.Create('uups ! OSCheck failed !');
281 except
282 on EOSError do ;
283 end;
284 {$endif}
285 end;
286
287 procedure TTestCastleUtils.TestStrings;
288 begin
289 // Uppercase Polish chars. We use UTF-8 now and it should work? Doesn't work yet.
290 // AssertTrue(AnsiSameText('b�cwa�', 'B�CWA�'));
291 // AssertTrue(not AnsiSameStr('b�cwa�', 'B�CWA�'));
292
293 AssertTrue(SameText('becwal', 'BECWAL'));
294 AssertTrue(not SameText('becwal', 'becwal '));
295 end;
296
297 procedure TTestCastleUtils.TestOthers;
298 begin
299 AssertTrue(StringOfChar('x', 0) = '');
300 AssertTrue(StringOfChar('x', 1) = 'x');
301 AssertTrue(StringOfChar('s', 3) = 'sss');
302 { We require Math.Power to work even with base <= 0 (but integer).
303 We even used to have our own GeneralPower implemented for this purpose,
304 but it's not needed since FPC 1.9.5 (bug 3005 is fixed). }
305 AssertTrue(Power( 2.0, 2.0) = 4.0);
306 AssertTrue(Power(-2.0, 2.0) = 4.0);
307 end;
308
309 procedure TTestCastleUtils.TestIntSqrt;
310
311 procedure Test(const Value: Cardinal);
312 var
313 S: Cardinal;
314 begin
315 S := IntSqrt(Value);
316 AssertTrue(Sqr(S) <= Value);
317 AssertTrue(Sqr(S+1) > Value);
318 end;
319
320 var
321 I: Cardinal;
322 begin
323 for I := 0 to 100 do Test(I);
324 for I := 1000 to 1100 do Test(I);
325 for I := 10000 to 10100 do Test(I);
326 for I := 100000 to 100100 do Test(I);
327 for I := 1000000 to 1000100 do Test(I);
328 end;
329
330 procedure TTestCastleUtils.TestDivMod;
331
332 procedure OneTest(const Divident: Integer; const Divisor: Word;
333 const CorrectDivResult, CorrectModResult: SmallInt);
334 var
335 DivResult: SmallInt;
336 ModResult: SmallInt;
337 begin
338 CastleDivMod(Divident, Divisor, DivResult, ModResult);
339 AssertTrue(DivResult = CorrectDivResult);
340 AssertTrue(ModResult = CorrectModResult);
341
342 DivResult := Divident div Divisor;
343 ModResult := Divident mod Divisor;
344 AssertTrue(DivResult = CorrectDivResult);
345 AssertTrue(ModResult = CorrectModResult);
346 end;
347
348 begin
349 OneTest(-39, 20, -1, -19);
350 OneTest(-9, 5, -1, -4);
351 OneTest(10, 3, 3, 1);
352 end;
353
354 procedure TTestCastleUtils.TestClamp;
355 var
356 A: Integer;
357 begin
358 A := 123;
359 ClampVar(A, 100, 200);
360 AssertTrue(A = 123);
361 ClampVar(A, 50, 100);
362 AssertTrue(A = 100);
363 ClampVar(A, 200, 300);
364 AssertTrue(A = 200);
365
366 AssertTrue(Clamped(123, 100, 200) = 123);
367 AssertTrue(Clamped(123, 50, 100) = 100);
368 AssertTrue(Clamped(123, 200, 300) = 200);
369 end;
370
371 procedure TTestCastleUtils.TestSimpleMath;
372 var
373 A, B: Integer;
374 begin
375 A := 123;
376 B := 456;
377 SwapValues(A, B);
378 AssertTrue(A = 456);
379 AssertTrue(B = 123);
380
381 OrderUp(A, B);
382 AssertTrue(A = 123);
383 AssertTrue(B = 456);
384
385 OrderUp(789, 1024, A, B);
386 AssertTrue(A = 789);
387 AssertTrue(B = 1024);
388
389 AssertTrue(Between(3, -100, 100));
390 AssertTrue(not Between(-300, -100, 100));
391
392 AssertSameValue( 0.5, MapRange(2 , 1 , 3 , 0 , 1 ), 0.01);
393 AssertSameValue( 0.5, MapRange(2.0, 1.0, 3.0, 0.0, 1.0), 0.01);
394
395 AssertSameValue(0.75, MapRange(1.5, 3 , 1 , 0 , 1 ), 0.01);
396 AssertSameValue(0.75, MapRange(1.5, 3.0, 1.0, 0.0, 1.0), 0.01);
397
398 AssertSameValue(0.25, MapRange(1.5, 1 , 3 , 0 , 1 ), 0.01);
399 AssertSameValue(0.25, MapRange(1.5, 1.0, 3.0, 0.0, 1.0), 0.01);
400
401 AssertTrue(DivRoundUp(40, 4) = 10);
402 AssertTrue(DivRoundUp(42, 4) = 11);
403 end;
404
405 procedure TTestCastleUtils.TestMinMax;
406 begin
407 AssertTrue(Min(345, 789) = 345);
408 AssertTrue(Max(345, 789) = 789);
409 AssertTrue(MinIntValue([345, 123, 789]) = 123);
410 AssertTrue(MaxIntValue([345, 123, 789]) = 789);
411 end;
412
413 procedure TTestCastleUtils.TestFileExt;
414 begin
415 AssertEquals('.wrl', ExtractFileExt('blah.blah.blah' + PathDelim + 'foo.wrl'));
416 AssertEquals('.wrl', ExtractFileExt('blah.blah.blah' + PathDelim + 'foo.bar.wrl'));
417 AssertEquals('.wrl', ExtractFileDoubleExt('blah.blah.blah' + PathDelim + 'foo.wrl'));
418 AssertEquals('.bar.wrl', ExtractFileDoubleExt('blah.blah.blah' + PathDelim + 'foo.bar.wrl'));
419 AssertEquals('blah.blah.blah' + PathDelim + 'foo', DeleteFileExt('blah.blah.blah' + PathDelim + 'foo.wrl'));
420 AssertEquals('blah.blah.blah' + PathDelim + 'foo.bar', DeleteFileExt('blah.blah.blah' + PathDelim + 'foo.bar.wrl'));
421 end;
422
423 procedure TTestCastleUtils.TestFloatModulo;
424 begin
425 AssertSameValue(0.5, FloatModulo( 0.5, 2));
426 AssertSameValue(0.5, FloatModulo( 2.5, 2));
427 AssertSameValue(1.5, FloatModulo( 3.5, 2));
428 AssertSameValue(1.5, FloatModulo(-0.5, 2));
429 AssertSameValue(1.5, FloatModulo(-2.5, 2));
430 AssertSameValue(0.5, FloatModulo(-3.5, 2));
431 end;
432
433 procedure TTestCastleUtils.TestRandomIntRange;
434
435 procedure TestRange(const R1, R2: Integer);
436 var
437 I: Integer;
438 MinRes, MaxRes, V: Integer;
439 begin
440 AssertTrue(R1 < R2);
441 MinRes := High(Integer);
442 MaxRes := Low(Integer);
443 for I := 1 to 1000 do
444 begin
445 V := RandomIntRange(R1, R2);
446 MinVar(MinRes, V);
447 MaxVar(MaxRes, V);
448 end;
449 AssertTrue(MinRes <= MaxRes);
450 AssertTrue(MinRes >= R1);
451 AssertTrue(MaxRes < R2);
452 // Writeln('For range [', R1, ',', R2 - 1, '], the random numbers were within [', MinRes, ',', MaxRes, ']. Should be usually equal, for large test count');
453 end;
454
455 begin
456 TestRange(10, 100);
457 TestRange(-100, -13);
458 TestRange(-5, 5);
459 end;
460
461 procedure TTestCastleUtils.TestRandomIntRangeInclusive;
462
463 procedure TestRange(const R1, R2: Integer);
464 var
465 I: Integer;
466 MinRes, MaxRes, V: Integer;
467 begin
468 AssertTrue(R1 < R2);
469 MinRes := High(Integer);
470 MaxRes := Low(Integer);
471 for I := 1 to 1000 do
472 begin
473 V := RandomIntRangeInclusive(R1, R2);
474 MinVar(MinRes, V);
475 MaxVar(MaxRes, V);
476 end;
477 AssertTrue(MinRes <= MaxRes);
478 AssertTrue(MinRes >= R1);
479 AssertTrue(MaxRes <= R2);
480 // Writeln('For range [', R1, ',', R2, '], the random numbers were within [', MinRes, ',', MaxRes, ']. Should be usually equal, for large test count');
481 end;
482
483 begin
484 TestRange(10, 100);
485 TestRange(-100, -13);
486 TestRange(-5, 5);
487 end;
488
489 procedure TTestCastleUtils.TestStrToFloatDot;
490 var
491 S: Single;
492 D: Double;
493 E: Extended;
494 SavedLocale: TSavedLocale;
495 begin
496 SavedLocale := FakeLocaleDecimalSeparatorComma;
497
498 AssertSameValue(0.2, StrToFloatDot('0.2'));
499
500 AssertSameValue(0.2, StrToFloatDefDot('0.2', 0.3));
501 AssertSameValue(0.3, StrToFloatDefDot('0,2', 0.3));
502
503 AssertTrue(TryStrToFloatDot('0.2', S));
504 AssertSameValue(0.2, S, 0.001);
505
506 AssertTrue(TryStrToFloatDot('0.2', D));
507 AssertSameValue(0.2, D, 0.001);
508
509 AssertTrue(TryStrToFloatDot('0.2', E));
510 AssertSameValue(0.2, E, 0.001);
511
512 AssertFalse(TryStrToFloatDot('0,2', S));
513 AssertFalse(TryStrToFloatDot('0,2', D));
514 AssertFalse(TryStrToFloatDot('0,2', E));
515
516 AssertEquals('0.10', FormatDot('%f', [0.1]));
517 AssertEquals('0.1', FloatToStrDot(0.1));
518
519 RestoreLocaleDecimalSeparatorComma(SavedLocale);
520 end;
521
522 procedure TTestCastleUtils.TestIsPathAbsolute;
523 begin
524 {$ifdef UNIX}
525 AssertTrue(IsPathAbsolute('/bla'));
526 AssertTrue(IsPathAbsolute('/bla/asdasd'));
527 AssertTrue(IsPathAbsolute('/bla/'));
528 AssertTrue(IsPathAbsolute('/bla/asdasd/'));
529
530 AssertFalse(IsPathAbsolute('\bla'));
531 AssertFalse(IsPathAbsolute('\bla\asdasd'));
532 AssertFalse(IsPathAbsolute('\bla\'));
533 AssertFalse(IsPathAbsolute('\bla\asdasd\'));
534
535 AssertFalse(IsPathAbsolute('c:/bla'));
536 AssertFalse(IsPathAbsolute('c:/bla/asdasd'));
537 AssertFalse(IsPathAbsolute('c:/bla/'));
538 AssertFalse(IsPathAbsolute('c:/bla/asdasd/'));
539
540 AssertFalse(IsPathAbsolute('c:\bla'));
541 AssertFalse(IsPathAbsolute('c:\bla\asdasd'));
542 AssertFalse(IsPathAbsolute('c:\bla\'));
543 AssertFalse(IsPathAbsolute('c:\bla\asdasd\'));
544
545 AssertFalse(IsPathAbsolute('bla'));
546 AssertFalse(IsPathAbsolute('bla/asdasd'));
547 AssertFalse(IsPathAbsolute('bla/'));
548 AssertFalse(IsPathAbsolute('bla/asdasd/'));
549
550 AssertFalse(IsPathAbsolute('bla'));
551 AssertFalse(IsPathAbsolute('bla\asdasd'));
552 AssertFalse(IsPathAbsolute('bla\'));
553 AssertFalse(IsPathAbsolute('bla\asdasd\'));
554 {$endif}
555
556 {$ifdef MSWINDOWS}
557 AssertFalse(IsPathAbsolute('/bla'));
558 AssertFalse(IsPathAbsolute('/bla/asdasd'));
559 AssertFalse(IsPathAbsolute('/bla/'));
560 AssertFalse(IsPathAbsolute('/bla/asdasd/'));
561
562 AssertFalse(IsPathAbsolute('\bla'));
563 AssertFalse(IsPathAbsolute('\bla\asdasd'));
564 AssertFalse(IsPathAbsolute('\bla\'));
565 AssertFalse(IsPathAbsolute('\bla\asdasd\'));
566
567 AssertTrue(IsPathAbsolute('c:/bla'));
568 AssertTrue(IsPathAbsolute('c:/bla/asdasd'));
569 AssertTrue(IsPathAbsolute('c:/bla/'));
570 AssertTrue(IsPathAbsolute('c:/bla/asdasd/'));
571
572 AssertTrue(IsPathAbsolute('c:\bla'));
573 AssertTrue(IsPathAbsolute('c:\bla\asdasd'));
574 AssertTrue(IsPathAbsolute('c:\bla\'));
575 AssertTrue(IsPathAbsolute('c:\bla\asdasd\'));
576
577 AssertFalse(IsPathAbsolute('bla'));
578 AssertFalse(IsPathAbsolute('bla/asdasd'));
579 AssertFalse(IsPathAbsolute('bla/'));
580 AssertFalse(IsPathAbsolute('bla/asdasd/'));
581
582 AssertFalse(IsPathAbsolute('bla'));
583 AssertFalse(IsPathAbsolute('bla\asdasd'));
584 AssertFalse(IsPathAbsolute('bla\'));
585 AssertFalse(IsPathAbsolute('bla\asdasd\'));
586 {$endif}
587 end;
588
589 procedure TTestCastleUtils.TestIsPathAbsoluteOnDrive;
590 begin
591 {$ifdef UNIX}
592 AssertTrue(IsPathAbsoluteOnDrive('/bla'));
593 AssertTrue(IsPathAbsoluteOnDrive('/bla/asdasd'));
594 AssertTrue(IsPathAbsoluteOnDrive('/bla/'));
595 AssertTrue(IsPathAbsoluteOnDrive('/bla/asdasd/'));
596
597 AssertFalse(IsPathAbsoluteOnDrive('\bla'));
598 AssertFalse(IsPathAbsoluteOnDrive('\bla\asdasd'));
599 AssertFalse(IsPathAbsoluteOnDrive('\bla\'));
600 AssertFalse(IsPathAbsoluteOnDrive('\bla\asdasd\'));
601
602 AssertFalse(IsPathAbsoluteOnDrive('c:/bla'));
603 AssertFalse(IsPathAbsoluteOnDrive('c:/bla/asdasd'));
604 AssertFalse(IsPathAbsoluteOnDrive('c:/bla/'));
605 AssertFalse(IsPathAbsoluteOnDrive('c:/bla/asdasd/'));
606
607 AssertFalse(IsPathAbsoluteOnDrive('c:\bla'));
608 AssertFalse(IsPathAbsoluteOnDrive('c:\bla\asdasd'));
609 AssertFalse(IsPathAbsoluteOnDrive('c:\bla\'));
610 AssertFalse(IsPathAbsoluteOnDrive('c:\bla\asdasd\'));
611
612 AssertFalse(IsPathAbsoluteOnDrive('bla'));
613 AssertFalse(IsPathAbsoluteOnDrive('bla/asdasd'));
614 AssertFalse(IsPathAbsoluteOnDrive('bla/'));
615 AssertFalse(IsPathAbsoluteOnDrive('bla/asdasd/'));
616
617 AssertFalse(IsPathAbsoluteOnDrive('bla'));
618 AssertFalse(IsPathAbsoluteOnDrive('bla\asdasd'));
619 AssertFalse(IsPathAbsoluteOnDrive('bla\'));
620 AssertFalse(IsPathAbsoluteOnDrive('bla\asdasd\'));
621 {$endif}
622
623 {$ifdef MSWINDOWS}
624 AssertTrue(IsPathAbsoluteOnDrive('/bla'));
625 AssertTrue(IsPathAbsoluteOnDrive('/bla/asdasd'));
626 AssertTrue(IsPathAbsoluteOnDrive('/bla/'));
627 AssertTrue(IsPathAbsoluteOnDrive('/bla/asdasd/'));
628
629 AssertTrue(IsPathAbsoluteOnDrive('\bla'));
630 AssertTrue(IsPathAbsoluteOnDrive('\bla\asdasd'));
631 AssertTrue(IsPathAbsoluteOnDrive('\bla\'));
632 AssertTrue(IsPathAbsoluteOnDrive('\bla\asdasd\'));
633
634 AssertTrue(IsPathAbsoluteOnDrive('c:/bla'));
635 AssertTrue(IsPathAbsoluteOnDrive('c:/bla/asdasd'));
636 AssertTrue(IsPathAbsoluteOnDrive('c:/bla/'));
637 AssertTrue(IsPathAbsoluteOnDrive('c:/bla/asdasd/'));
638
639 AssertTrue(IsPathAbsoluteOnDrive('c:\bla'));
640 AssertTrue(IsPathAbsoluteOnDrive('c:\bla\asdasd'));
641 AssertTrue(IsPathAbsoluteOnDrive('c:\bla\'));
642 AssertTrue(IsPathAbsoluteOnDrive('c:\bla\asdasd\'));
643
644 AssertFalse(IsPathAbsoluteOnDrive('bla'));
645 AssertFalse(IsPathAbsoluteOnDrive('bla/asdasd'));
646 AssertFalse(IsPathAbsoluteOnDrive('bla/'));
647 AssertFalse(IsPathAbsoluteOnDrive('bla/asdasd/'));
648
649 AssertFalse(IsPathAbsoluteOnDrive('bla'));
650 AssertFalse(IsPathAbsoluteOnDrive('bla\asdasd'));
651 AssertFalse(IsPathAbsoluteOnDrive('bla\'));
652 AssertFalse(IsPathAbsoluteOnDrive('bla\asdasd\'));
653 {$endif}
654 end;
655
656 procedure TTestCastleUtils.TestRestOf3DCoords;
657 var
658 A, B: T3DAxis;
659 begin
660 RestOf3DCoords(0, A, B);
661 AssertEquals(1, A);
662 AssertEquals(2, B);
663
664 RestOf3DCoords(1, A, B);
665 AssertEquals(0, A);
666 AssertEquals(2, B);
667
668 RestOf3DCoords(2, A, B);
669 AssertEquals(0, A);
670 AssertEquals(1, B);
671 end;
672
673 procedure TTestCastleUtils.TestRestOf3DCoordsCycle;
674 var
675 A, B: T3DAxis;
676 begin
677 RestOf3DCoordsCycle(0, A, B);
678 AssertEquals(1, A);
679 AssertEquals(2, B);
680
681 RestOf3DCoordsCycle(1, A, B);
682 AssertEquals(2, A);
683 AssertEquals(0, B);
684
685 RestOf3DCoordsCycle(2, A, B);
686 AssertEquals(0, A);
687 AssertEquals(1, B);
688 end;
689
690 procedure TTestCastleUtils.TestDecimalSeparator;
691 begin
692 {$ifdef CASTLE_TEST_DECIMAL_SEPARATOR_COMMA}
693 AssertEquals('123,45', Format('%f', [123.45]));
694 AssertEquals('123,45', FloatToStr(123.45));
695 AssertSameValue(123.45, StrToFloat('123,45'));
696 {$else}
697 AssertEquals('123' + {$ifdef FPC}DefaultFormatSettings{$else}FormatSettings{$endif}.DecimalSeparator + '45', Format('%f', [123.45]));
698 AssertEquals('123' + {$ifdef FPC}DefaultFormatSettings{$else}FormatSettings{$endif}.DecimalSeparator + '45', FloatToStr(123.45));
699 AssertSameValue(123.45, StrToFloat('123' + {$ifdef FPC}DefaultFormatSettings{$else}FormatSettings{$endif}.DecimalSeparator + '45'));
700 {$endif}
701 end;
702
703 procedure TTestCastleUtils.TestFloatToStrDisplay;
704 begin
705 AssertEquals('123', FloatToStrDisplay(123.000));
706 AssertEquals('123.4', FloatToStrDisplay(123.400));
707 AssertEquals('123.45', FloatToStrDisplay(123.450));
708 AssertEquals('123.46', FloatToStrDisplay(123.456));
709
710 AssertEquals('0', FloatToStrDisplay(0.000));
711 AssertEquals('0.4', FloatToStrDisplay(0.400));
712 AssertEquals('0.45', FloatToStrDisplay(0.450));
713 AssertEquals('0.46', FloatToStrDisplay(0.456));
714
715 AssertEquals('99.99', FloatToStrDisplay(99.99));
716 AssertEquals('100', FloatToStrDisplay(99.999));
717 end;
718
719 procedure TTestCastleUtils.TestDeg;
720 begin
721 AssertSameValue(0, Deg(0));
722 AssertSameValue(pi/2, Deg(90));
723 AssertSameValue(-pi/2, Deg(-90));
724 end;
725
726 initialization
727 RegisterTest(TTestCastleUtils);
728 end.