VirtualBox

source: kBuild/trunk/src/kmk/tests/scripts/features/ifcond@ 2022

Last change on this file since 2022 was 2022, checked in by bird, 16 years ago

exprval.c: fixed bug parsing unqoted variable references ($(VAR) == $(VAR)). Converted ifcond testcases to the GNU Make tests and supplemented it with tests for this bug.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1# $Id: ifcond 2022 2008-11-02 02:15:00Z bird $ -*-perl-*-
2## @file
3# if conditionals.
4#
5
6#
7# Copyright (c) 2008 knut st. osmundsen <[email protected]>
8#
9# This file is part of kBuild.
10#
11# kBuild is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 3 of the License, or
14# (at your option) any later version.
15#
16# kBuild is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with kBuild. If not, see <http://www.gnu.org/licenses/>
23#
24#
25
26$description = "Tests the if conditionals";
27
28$details = "...";
29
30if ($is_kmk) {
31
32 # TEST #0 - check that the feature is present.
33 # --------------------------------------------
34 run_make_test('
35ifneq ($(if-expr 1+1,1,0),1)
36$(error sub-test 0 failed)
37endif
38.PHONY: all
39all: ; @:
40',
41'',
42'');
43
44 # TEST #1 - A more comprehensive, yet a bit large, test.
45 # ------------------------------------------------------
46 run_make_test('
47
48#
49# Note! The testcase are ordered by ascending operator precedence
50# with the exception of equal and not-equal because these
51# are kind of useful for performing tests on non-logical ops.
52#
53
54.PHONY: all
55all: ; @:
56
57#
58# Parenthesis
59#
60$(info unary operators: ( and ))
61if (1)
62else
63$(error )
64endif
65
66if ((((1))))
67else
68$(error )
69endif
70
71
72#
73# Equal and Not Equal w/ some fundamental bits thrown in.
74#
75$(info binary operators: == and !=)
76
77if 1 == 1
78else
79$(error )
80endif
81
82if 2 == 3
83$(error )
84else
85endif
86
87if 2 != 3
88else
89$(error )
90endif
91
92if a != b
93else
94$(error )
95endif
96
97if asdf == asdf
98else
99$(error )
100endif
101
102if "asdf" == asdf
103else
104$(error )
105endif
106
107if \'asdf\' == asdf
108else
109$(error )
110endif
111
112if \'asdf\' == "asdf"
113else
114$(error )
115endif
116
117if \'asdf\' == \'asdf\'
118else
119$(error )
120endif
121
122if "asdf" == "asdf"
123else
124$(error )
125endif
126
127if 0x1 == 1
128else
129$(error )
130endif
131
132if 0xfff == 4095
133else
134$(error )
135endif
136
137if 0xfff == 4095
138else
139$(error )
140endif
141
142if 0d10 == 10
143else
144$(error )
145endif
146
147if 0d10 == 10
148else
149$(error )
150endif
151
152if 0xa == 012
153else
154$(error )
155endif
156
157if 0b1110 == 016
158else
159$(error )
160endif
161
162
163#
164# Logical OR
165#
166$(info binary operator: ||)
167if 1
168else
169$(error busted)
170endif
171
172if 1 || 1
173else
174$(error )
175endif
176
177if 0 || 0
178$(error )
179else
180endif
181
182if 1 || 0
183else
184$(error )
185endif
186
187if 0 || 1
188else
189$(error )
190endif
191
192if 0 || 0 || 0 || 0 || 0 || 0 || 0
193$(error )
194else
195endif
196
197if 0 || 0 || 0 || 1 || 0 || 0 || 0
198else
199$(error )
200endif
201
202if "asdf" || 0
203else
204$(error )
205endif
206
207if 0 || "asdf"
208else
209$(error )
210endif
211
212if \'asdf\' || 0
213else
214$(error )
215endif
216
217if "" || 0
218$(error )
219endif
220if "" || 1
221else
222$(error )
223endif
224if \'\' || 0
225$(error )
226endif
227if \'\' || 1
228else
229$(error )
230endif
231
232if "" || \'\'
233$(error )
234endif
235if "1" || \'\'
236else
237$(error )
238endif
239if "1" || \'1\'
240else
241$(error )
242endif
243if "" || \'1\'
244else
245$(error )
246endif
247
248
249#
250# Logical AND
251#
252$(info binary operator: &&)
253if 1 && 1
254else
255$(error )
256endif
257if 1 && 0
258$(error )
259endif
260if 1234 && 0
261$(error )
262endif
263if 123434 && 0 && 123435 && 1
264$(error )
265endif
266
267if "" && 1
268$(error )
269endif
270if ("asdf" && 1) != 1
271$(error )
272endif
273if "1" && \'asdf\'
274else
275$(error )
276endif
277if "1" && \'asdf\' && 0
278$(error )
279endif
280
281if 0 || 1 && 0
282$(error )
283endif
284
285
286#
287# Bitwise OR
288#
289$(info binary operator: |)
290if 1 | 0
291else
292$(error )
293endif
294if 1 | 1
295else
296$(error )
297endif
298if 11234 | 343423
299else
300$(error )
301endif
302if (1|2)!=3
303$(error )
304endif
305if 1|2 != 3
306else
307$(error )
308endif
309if (1|2|4|8)!=0xf
310$(error )
311endif
312
313
314#
315# Bitwise XOR
316#
317$(info binary operator: ^)
318if 1 ^ 1
319$(error )
320endif
321
322if (2 ^ 1) != 3
323$(error )
324endif
325
326if 7 != (2 ^ 1 ^ 4)
327$(error )
328endif
329
330if (2 ^ 1 | 2) != 3
331$(error )
332endif
333
334
335#
336# Bitwise AND
337#
338$(info binary operator: &)
339if (4097 & 1) != 1
340$(error )
341endif
342if (0xfff & 0x0f0) != 0xf0
343$(error )
344endif
345if (0x1e3 & 0x100 | 3) != 0x103
346$(error )
347endif
348
349
350#
351# Greater than
352#
353$(info binary operator: >)
354if 1 > 0
355else
356$(error )
357endif
358
359if 1024 > 1023
360else
361$(error )
362endif
363
364if 999 > 1023
365$(error )
366endif
367
368if (5 > 4 | 2) != 3
369$(error )
370endif
371
372if (1 & 8 > 4) != 1
373$(error )
374endif
375
376if (8 > 4 ^ 16) != 17
377$(error )
378endif
379
380if "b" > \'a\'
381else
382$(error )
383endif
384if "abcdef" > \'ffdas\'
385$(error )
386endif
387if abcdef > ffdas
388$(error )
389endif
390
391
392#
393# Greater or equal than
394#
395$(info binary operator: >=)
396if 20 > 0
397else
398$(error )
399endif
400
401if 20 >= 20
402else
403$(error )
404endif
405
406if 19 >= 20
407$(error )
408endif
409
410if (1 & 8 >= 4) != 1
411$(error )
412endif
413
414if "x" >= \'x\'
415else
416$(error )
417endif
418if "abdc" >= \'abcd\'
419else
420$(error )
421endif
422if "ffdaaa" >= \'ffdasd\'
423$(error )
424endif
425if asdf >= asdf
426else
427$(error )
428endif
429
430
431#
432# Less than
433#
434if 1 < 1
435$(error )
436endif
437if -123 < -134
438$(error )
439endif
440if 123 <= 7777
441else
442$(error )
443endif
444
445if "b" < \'a\'
446$(error )
447endif
448if b < a
449$(error )
450endif
451if \'foobar\' < \'a$\'
452$(error )
453endif
454if hhhh < ggggg
455$(error )
456endif
457if qwerty < qwerty0
458else
459$(error )
460endif
461
462
463#
464# Less or equal than
465#
466$(info binary operator: >>)
467if 1 <= 0
468$(error )
469endif
470if 1 <= 1
471else
472$(error )
473endif
474if 123 <= 123 != 1
475$(error )
476endif
477if 560 <= 456
478$(error )
479endif
480
481if "a" <= \'a\'
482else
483$(error )
484endif
485if "abcdef" <= \'abcdef\'
486else
487$(error )
488endif
489if q12345z6 <= q12345z
490$(error )
491endif
492if QWERTY <= ABCDE
493$(error )
494endif
495
496
497#
498# Shift right
499#
500$(info binary operator: >>)
501if 1 >> 0 != 1
502$(error )
503endif
504if 1024 >> 2 != 256
505$(error )
506endif
507if 102435 >> 4 > 1234 != 1
508$(error )
509endif
510
511
512#
513# Shift left
514#
515$(info binary operator: <<)
516if 1 << 0 != 1
517$(error )
518endif
519if 1 << 1 != 2
520$(error )
521endif
522if 1 << 4 != 16
523$(error )
524endif
525if 1 << 10 != 1024
526$(error )
527endif
528if 34 << 10 != 0x8800
529$(error )
530endif
531if 1099511627776 << 21 != 2305843009213693952
532$(error )
533endif
534if 1 << 61 != 2305843009213693952
535$(error )
536endif
537
538if 2 << 60 > 123434323 != 1
539$(error )
540endif
541
542
543#
544# Subtraction
545#
546$(info binary operator: -)
547if 1-1 != 0
548$(error )
549endif
550if 1023-511 != 512
551$(error )
552endif
553if 4 - 3 << 3 != 8
554$(error )
555endif
556
557
558#
559# Addition
560#
561$(info binary operator: +)
562if 1+1 != 2
563$(error )
564endif
565if 1234+1000 != 2234
566$(error )
567endif
568if 2 + 2 << 4 != 64
569$(error )
570endif
571
572
573#
574# Modulus
575#
576$(info binary operator: %)
577if 0%2 != 0
578$(error )
579endif
580if 10%7 != 3
581$(error )
582endif
583if 10 + 100%70 - 3 != 37
584$(error )
585endif
586
587
588#
589# Division
590#
591$(info binary operator: /)
592if 0/1 != 0
593$(error )
594endif
595if 1000/2 != 500
596$(error )
597endif
598if 1000/2 + 4 != 504
599$(error )
600endif
601if 5 + 1000/4 != 255
602$(error )
603endif
604
605
606#
607# Multiplication
608#
609$(info binary operator: *)
610if 1*1 != 1
611$(error )
612endif
613if 10*10 != 100
614$(error )
615endif
616if 1024*64 != 65536
617$(error )
618endif
619if 10*10 - 10 != 90
620$(error )
621endif
622if 1000 - 10*10 != 900
623$(error )
624endif
625
626
627#
628# Logical NOT
629#
630$(info unary operator: !)
631if !1
632$(error )
633endif
634
635if !42 == 0
636else
637$(error )
638endif
639
640if !0 == 1
641else
642$(error )
643endif
644
645if !!0 == 0
646else
647$(error )
648endif
649
650if !0 * 123 != 123
651$(error )
652endif
653if !!!0 * 512 != 512
654$(error )
655endif
656
657
658#
659# Bitwise NOT
660#
661$(info unary operator: ~)
662if ~0xfff != 0xfffffffffffff000
663$(error )
664endif
665
666
667#
668# Pluss
669#
670$(info unary operator: +)
671if +2 != 2
672$(error )
673endif
674if 1++++++++++++2134 != 2135
675$(error )
676endif
677
678
679#
680# Minus (negation)
681#
682$(info unary operator: -)
683if --2 != 2
684$(error )
685endif
686
687if 1 - -2 != 3
688$(error )
689endif
690
691
692#
693# target
694#
695trg_deps_only: foobar
696trg_with_cmds: foobar
697 echo $@
698
699$(info unary operator: target) # This flushes stuff in read.c
700
701if target trg_with_cmds
702else
703$(error target trg_with_cmds)
704endif
705if target(trg_deps_only)
706$(error target trg_deps_only)
707endif
708if target ( foobar )
709$(error target foobar)
710endif
711
712
713#
714# defined
715#
716$(info unary operator: defined)
717var_defined := 1
718var_not_defined :=
719
720if defined var_defined
721else
722$(error )
723endif
724if defined(var_defined)
725else
726$(error )
727endif
728if defined (var_defined)
729else
730$(error )
731endif
732if !defined(var_defined)
733$(error )
734endif
735if defined (var_not_defined)
736$(error )
737endif
738
739
740#
741# bool
742#
743$(info unary operator: bool)
744if bool("Asdf") != 1
745$(error )
746endif
747if bool("") != 0
748$(error )
749endif
750
751
752#
753# bool
754#
755$(info unary operator: num)
756if num("1234") != 1235 - 1
757$(error )
758endif
759if num(\'1234\') != 1233 + 1
760$(error )
761endif
762
763
764#
765# str
766#
767$(info unary operator: str)
768if str(a < b) != 1
769$(error )
770endif
771if str(a < b) != \'1\'
772$(error )
773endif
774if str( 1 ) != "1"
775$(error )
776endif
777if str( 1 ) != "1"
778$(error )
779endif
780if str( num(0x1000) ) != "4096"
781$(error )
782endif
783if str(0x1000) != 0x1000
784$(error )
785endif
786
787
788
789#
790# Quick check of $(if-expr ) and $(expr ).
791#
792$(info $$(if-expr ,,))
793ifeq ($(if-expr 0 || 2,42,500),42)
794else
795$(error )
796endif
797ifeq ($(if-expr 5+3 == 231,42,500),42)
798$(error )
799endif
800
801$(info $$(expr ))
802ifeq ($(expr 5+3),8)
803else
804$(error expr:$(expr 5+3) expected 8)
805endif
806ifeq ($(expr 25*25),625)
807else
808$(error expr:$(expr 25*25) expected 625)
809endif
810ifeq ($(expr 100/3),3)
811$(error )
812endif
813',
814'',
815'unary operators: ( and )
816binary operators: == and !=
817binary operator: ||
818binary operator: &&
819binary operator: |
820binary operator: ^
821binary operator: &
822binary operator: >
823binary operator: >=
824binary operator: >>
825binary operator: >>
826binary operator: <<
827binary operator: -
828binary operator: +
829binary operator: %
830binary operator: /
831binary operator: *
832unary operator: !
833unary operator: ~
834unary operator: +
835unary operator: -
836unary operator: target
837unary operator: defined
838unary operator: bool
839unary operator: num
840unary operator: str
841$(if-expr ,,)
842$(expr )
843');
844
845}
846
847
848 # TEST #2 - A bug.
849 # ------------------------------------------------------
850 run_make_test('
851.PHONY: all
852all: ; @:
853
854#
855# Assert sanity first on simple strings.
856#
857if abcd != "abcd"
858$(error )
859endif
860
861if \'abcd\' != abcd
862$(error )
863endif
864
865if abcd != abcd
866$(error )
867endif
868
869
870#
871# String by reference, start with a few simple cases.
872#
873STR1 = abcd
874
875if "$(STR1)" != "abcd"
876$(error )
877endif
878
879if \'$(STR1)\' == "abcd" # not expanded.
880$(error )
881endif
882
883if \'$(STR1)\' != \'$(STR1)\'
884$(error )
885endif
886
887if "$(STR1)" != "$(STR1)"
888$(error )
889endif
890
891#
892# Now for the kmk 0.1.4 bug...
893#
894if $(STR1) != "$(STR1)"
895$(error )
896endif
897
898if "$(STR1)" != $(STR1)
899$(error )
900endif
901
902if $(STR1) != $(STR1)
903$(error )
904endif
905
906#
907# And some extra for good measure.
908#
909STR2 = STR
910NUM1 = 1
911
912if $($(STR2)$(NUM1)) != "abcd"
913$(error )
914endif
915
916if "abcd" != $($(STR2)$(NUM1))
917$(error )
918endif
919
920if "abcd" != $(${STR2}$(NUM1))
921$(error )
922endif
923
924if "abcd" != ${$(STR2)$(NUM1)}
925$(error )
926endif
927
928if "abcd" != ${${STR2}${NUM1}}
929$(error )
930endif
931
932if ${${STR2}${NUM1}} != \'abcd\'
933$(error )
934endif
935
936if "${${STR2}${NUM1}}" != \'abcd\'
937$(error )
938endif
939
940
941',
942'',
943'');
944
945
946
947# Indicate that we're done.
9481;
949
950
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette