PageRenderTime 53ms CodeModel.GetById 2ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Tests/test_conditional.py

#
Python | 1154 lines | 1111 code | 15 blank | 28 comment | 76 complexity | 4bab0f1fc7b7cd9c05128cdd011b6b57 MD5 | raw file
   1#####################################################################################
   2#
   3#  Copyright (c) Microsoft Corporation. All rights reserved.
   4#
   5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
   6# copy of the license can be found in the License.html file at the root of this distribution. If
   7# you cannot locate the  Apache License, Version 2.0, please send an email to
   8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
   9# by the terms of the Apache License, Version 2.0.
  10#
  11# You must not remove this notice, or any other, from this software.
  12#
  13#
  14#####################################################################################
  15
  16from iptest.assert_util import *
  17import sys
  18
  19def test_simple():
  20    # Simple conditional true case
  21    Assert(100 if 1 else 200 == 100)
  22
  23    # Simple conditional false case
  24    Assert(0 if 0 else 200 == 200)
  25
  26def test_multiple_assignment():
  27    # Conditional multiple assignment
  28    x, y, z, w, u  = 1 if 0 else 2, 2 if 1 else 3, 3 if 10 else 4, 1 & 0 if 0 and 3 or 4 else 100, 1 and 0 if 0 and 3 or 4 & 0 else 100
  29    AreEqual((x,y,z,w,u), (2,2,3,0,100))
  30
  31def test_in_expressions():
  32    # combination of operators and conditional
  33    Assert(100 + 1 & 3 if 1 else 2 == 1)
  34    Assert(100 + (1 & 3 if 1 else 2) == 101)
  35
  36def test_if_else():
  37    # conditional in if-else
  38    x, y, z = 0,1,2
  39    if x if y else z:
  40        p = 100
  41    else:
  42        p = 200
  43    Assert(p == 200)
  44
  45def test_nested_conditionals():
  46    # nested conditionals
  47    if 0 if (0 if 100 else 1 ) else 10:
  48        x = 300
  49    else:
  50        x = 400
  51    Assert(x == 300)
  52
  53def test_conditionals_with_test_list_1():
  54    # conditionals with test-list #test1
  55    x,y,z = 1,2,3
  56    if 20 if (x,y,z == 1,2,3) else 0:
  57        x = 300
  58    else:
  59        x = 400
  60    Assert(x == 300)
  61
  62
  63def test_conditionals_with_test_list_2():
  64    # conditionals with test-list #test2
  65    list = [[1 if 1 else 0,0 if 0 else 2,3],[4,5 if 1 and 1 else 0,8 if 0 and 1 else 6 & 7]]
  66    if 20 if (list == [[1,2,3],[4,5,7]]) else 0 if 1 else 200 :
  67        x = 300
  68    else:
  69        x = 400
  70    Assert(x == 400)
  71
  72def test_generator_expressions():
  73    #test for gen_for
  74    Assert(sum(x*x for x in range(10) if not x%2 if not x%3) == sum([x*x for x in range(10) if not x%6]))
  75    
  76    #test for gen_for gen_if combined
  77    Assert(sum(x*x for x in range(10) for x in range(5) if not x %2) == 200)
  78
  79def test_list_for():
  80    #test for list_for
  81    list = [10,20,30,40,50,60,70,80,90,100,110,120,130]
  82    mysum = 0
  83    for i in (0,1,2,3,4,5,6,7,8,9,10,11,12):
  84        mysum += list[i]
  85    Assert(mysum == 910)
  86
  87    #test for list_for list_if combined
  88    list = [10,20,30,40,50,60,70,80,90,100,110,120,130]
  89    Assert(sum(list[i] if not i%2 else 0 for i in (0,1,2,3,4,5,6,7,8,9,10,11,12) if not i %3 if not 0) == 210)
  90
  91def test_errors():
  92    #test for null list
  93    AssertError(SyntaxError, compile, "mysum = 0;for i in 10:pass", "Error", "exec")
  94    # test for lambda function
  95    AssertError(SyntaxError, compile, "[f for f in 1, lambda x: x if x >= 0 else -1]", "", "exec")
  96
  97def test_conditional_in_lambda():
  98    try:
  99        list = [f for f in (1, lambda x: x if x >= 0 else -1)]
 100        list = [f for f in 1, lambda x: (x if x >= 0 else -1)]
 101        list = [f for f in 1, (lambda x: x if x >= 0 else -1)]
 102    except e:
 103        Assert(False, e.message)
 104
 105def test_conditional_return_types():
 106    '''
 107    11491
 108    '''
 109    class OldK: pass
 110    
 111    class NewK(object): pass
 112    
 113    for x in [
 114                -2, -1, 0, 1, 2, 2**16,
 115                -2L, -1L, 0L, 1L, 2L, 2**32,
 116                3.14,
 117                2j,
 118                "", "abc",
 119                {}, {'a':'b'}, {'a':'b', 'c':'d'},
 120                [], [1], [1, 2],
 121                xrange(0), xrange(1), xrange(2),
 122                OldK, NewK, OldK(), NewK(),
 123                None, str, object,
 124                ]:
 125        temp = 0 if 0 else x
 126        AreEqual(temp, x)
 127
 128def test_conversions():
 129    AreEqual(1 if False else "Hello", "Hello")
 130    AreEqual("Hello" if False else 1, 1)
 131    AreEqual(1 if True else "Hello", 1)
 132    AreEqual("Hello" if False else "Goodbye", "Goodbye")
 133
 134    if (1 if True else False):
 135        pass
 136    else:
 137        Fail("Expression incorrectly evaluated")
 138
 139def test_cp13299():
 140    true_conditions = [ 1, 1L, -1, -1L, True, 1.1, -1.1, "abc", int, 0.1, -0.1]
 141    false_conditions = [ 0, 0L, None, 0.0, -0, -0.0, False, (), [], ""]
 142    
 143    for condition in true_conditions:
 144        x = condition if condition else False
 145        AreEqual(x, condition)
 146
 147    for condition in false_conditions:
 148        x = True if condition else condition
 149        AreEqual(x, condition)
 150            
 151    AreEqual(3.14 if True else 1, 3.14)
 152    AreEqual(3.14 if True else 1L, 3.14)
 153    AreEqual(3.14 if True else -1, 3.14)
 154    AreEqual(3.14 if True else True, 3.14)
 155    AreEqual(3.14 if True else 1.1, 3.14)
 156    AreEqual(3.14 if True else -1.1, 3.14)
 157    AreEqual(3.14 if True else "abc", 3.14)
 158    AreEqual(3.14 if True else int, 3.14)
 159    AreEqual(3.14 if True else 0, 3.14)
 160    AreEqual(3.14 if True else 0L, 3.14)
 161    AreEqual(3.14 if True else None, 3.14)
 162    AreEqual(3.14 if True else False, 3.14)
 163    AreEqual(3.14 if True else (), 3.14)
 164    AreEqual(3.14 if True else [[1]], 3.14)
 165    AreEqual(3.14 if True else u"", 3.14)
 166    
 167    AreEqual(1 if False else 3.14, 3.14)
 168    AreEqual(1L if False else 3.14, 3.14)
 169    AreEqual(-1 if False else 3.14, 3.14)
 170    AreEqual(True if False else 3.14, 3.14)
 171    AreEqual(1.1 if False else 3.14, 3.14)
 172    AreEqual(-1.1 if False else 3.14, 3.14)
 173    AreEqual("abc" if False else 3.14, 3.14)
 174    AreEqual(int if False else 3.14, 3.14)
 175    AreEqual(0 if False else 3.14, 3.14)
 176    AreEqual(0L if False else 3.14, 3.14)
 177    AreEqual(None if False else 3.14, 3.14)
 178    AreEqual(False if False else 3.14, 3.14)
 179    AreEqual(() if False else 3.14, 3.14)
 180    AreEqual([[1]] if False else 3.14, 3.14)
 181    AreEqual(u"" if False else 3.14, 3.14)
 182    
 183def test_large_if():    
 184    def f(value):
 185        if value:
 186            return 42
 187        elif value:
 188            raise Exception()
 189        elif value:
 190            pass
 191        elif value:
 192            pass
 193        elif value:
 194            pass
 195        elif value:
 196            pass
 197        elif value:
 198            pass
 199        elif value:
 200            pass
 201        elif value:
 202            pass
 203        elif value:
 204            pass
 205        elif value:
 206            pass
 207        elif value:
 208            pass
 209        elif value:
 210            pass
 211        elif value:
 212            pass
 213        elif value:
 214            pass
 215        elif value:
 216            pass
 217        elif value:
 218            pass
 219        elif value:
 220            pass
 221        elif value:
 222            pass
 223        elif value:
 224            pass
 225        elif value:
 226            pass
 227        elif value:
 228            pass
 229        elif value:
 230            pass
 231        elif value:
 232            pass
 233        elif value:
 234            pass
 235        elif value:
 236            pass
 237        elif value:
 238            pass
 239        elif value:
 240            pass
 241        elif value:
 242            pass
 243        elif value:
 244            pass
 245        elif value:
 246            pass
 247        elif value:
 248            pass
 249        elif value:
 250            pass
 251        elif value:
 252            pass
 253        elif value:
 254            pass
 255        elif value:
 256            pass
 257        elif value:
 258            pass
 259        elif value:
 260            pass
 261        elif value:
 262            pass
 263        elif value:
 264            pass
 265        elif value:
 266            pass
 267        elif value:
 268            pass
 269        elif value:
 270            pass
 271        elif value:
 272            pass
 273        elif value:
 274            pass
 275        elif value:
 276            pass
 277        elif value:
 278            pass
 279        elif value:
 280            pass
 281        elif value:
 282            pass
 283        elif value:
 284            pass
 285        elif value:
 286            pass
 287        elif value:
 288            pass
 289        elif value:
 290            pass
 291        elif value:
 292            pass
 293        elif value:
 294            pass
 295        elif value:
 296            pass
 297        elif value:
 298            pass
 299        elif value:
 300            pass
 301        elif value:
 302            pass
 303        elif value:
 304            pass
 305        elif value:
 306            pass
 307        elif value:
 308            pass
 309        elif value:
 310            pass
 311        elif value:
 312            pass
 313        elif value:
 314            pass
 315        elif value:
 316            pass
 317        elif value:
 318            pass
 319        elif value:
 320            pass
 321        elif value:
 322            pass
 323        elif value:
 324            pass
 325        elif value:
 326            pass
 327        elif value:
 328            pass
 329        elif value:
 330            pass
 331        elif value:
 332            pass
 333        elif value:
 334            pass
 335        elif value:
 336            pass
 337        elif value:
 338            pass
 339        elif value:
 340            pass
 341        elif value:
 342            pass
 343        elif value:
 344            pass
 345        elif value:
 346            pass
 347        elif value:
 348            pass
 349        elif value:
 350            pass
 351        elif value:
 352            pass
 353        elif value:
 354            pass
 355        elif value:
 356            pass
 357        elif value:
 358            pass
 359        elif value:
 360            pass
 361        elif value:
 362            pass
 363        elif value:
 364            pass
 365        elif value:
 366            pass
 367        elif value:
 368            pass
 369        elif value:
 370            pass
 371        elif value:
 372            pass
 373        elif value:
 374            pass
 375        elif value:
 376            pass
 377        elif value:
 378            pass
 379        elif value:
 380            pass
 381        elif value:
 382            pass
 383        elif value:
 384            pass
 385        elif value:
 386            pass
 387        elif value:
 388            pass
 389        elif value:
 390            pass
 391        elif value:
 392            pass
 393        elif value:
 394            pass
 395        elif value:
 396            pass
 397        elif value:
 398            pass
 399        elif value:
 400            pass
 401        elif value:
 402            pass
 403        elif value:
 404            pass
 405        elif value:
 406            pass
 407        elif value:
 408            pass
 409        elif value:
 410            pass
 411        elif value:
 412            pass
 413        elif value:
 414            pass
 415        elif value:
 416            pass
 417        elif value:
 418            pass
 419        elif value:
 420            pass
 421        elif value:
 422            pass
 423        elif value:
 424            pass
 425        elif value:
 426            pass
 427        elif value:
 428            pass
 429        elif value:
 430            pass
 431        elif value:
 432            pass
 433        elif value:
 434            pass
 435        elif value:
 436            pass
 437        elif value:
 438            pass
 439        elif value:
 440            pass
 441        elif value:
 442            pass
 443        elif value:
 444            pass
 445        elif value:
 446            pass
 447        elif value:
 448            pass
 449        elif value:
 450            pass
 451        elif value:
 452            pass
 453        elif value:
 454            pass
 455        elif value:
 456            pass
 457        elif value:
 458            pass
 459        elif value:
 460            pass
 461        elif value:
 462            pass
 463        elif value:
 464            pass
 465        elif value:
 466            pass
 467        elif value:
 468            pass
 469        elif value:
 470            pass
 471        elif value:
 472            pass
 473        elif value:
 474            pass
 475        elif value:
 476            pass
 477        elif value:
 478            pass
 479        elif value:
 480            pass
 481        elif value:
 482            pass
 483        elif value:
 484            pass
 485        elif value:
 486            pass
 487        elif value:
 488            pass
 489        elif value:
 490            pass
 491        elif value:
 492            pass
 493        elif value:
 494            pass
 495        elif value:
 496            pass
 497        elif value:
 498            pass
 499        elif value:
 500            pass
 501        elif value:
 502            pass
 503        elif value:
 504            pass
 505        elif value:
 506            pass
 507        elif value:
 508            pass
 509        elif value:
 510            pass
 511        elif value:
 512            pass
 513        elif value:
 514            pass
 515        elif value:
 516            pass
 517        elif value:
 518            pass
 519        elif value:
 520            pass
 521        elif value:
 522            pass
 523        elif value:
 524            pass
 525        elif value:
 526            pass
 527        elif value:
 528            pass
 529        elif value:
 530            pass
 531        elif value:
 532            pass
 533        elif value:
 534            pass
 535        elif value:
 536            pass
 537        elif value:
 538            pass
 539        elif value:
 540            pass
 541        elif value:
 542            pass
 543        elif value:
 544            pass
 545        elif value:
 546            pass
 547        elif value:
 548            pass
 549        elif value:
 550            pass
 551        elif value:
 552            pass
 553        elif value:
 554            pass
 555        elif value:
 556            pass
 557        elif value:
 558            pass
 559        elif value:
 560            pass
 561        elif value:
 562            pass
 563        elif value:
 564            pass
 565        elif value:
 566            pass
 567        elif value:
 568            pass
 569        elif value:
 570            pass
 571        elif value:
 572            pass
 573        elif value:
 574            pass
 575        elif value:
 576            pass
 577        elif value:
 578            pass
 579        elif value:
 580            pass
 581        elif value:
 582            pass
 583        elif value:
 584            pass
 585        elif value:
 586            pass
 587        elif value:
 588            pass
 589        elif value:
 590            pass
 591        elif value:
 592            pass
 593        elif value:
 594            pass
 595        elif value:
 596            pass
 597        elif value:
 598            pass
 599        elif value:
 600            pass
 601        elif value:
 602            pass
 603        elif value:
 604            pass
 605        elif value:
 606            pass
 607        elif value:
 608            pass
 609        elif value:
 610            pass
 611        elif value:
 612            pass
 613        elif value:
 614            pass
 615        elif value:
 616            pass
 617        elif value:
 618            pass
 619        elif value:
 620            pass
 621        elif value:
 622            pass
 623        elif value:
 624            pass
 625        elif value:
 626            pass
 627        elif value:
 628            pass
 629        elif value:
 630            pass
 631        elif value:
 632            pass
 633        elif value:
 634            pass
 635        elif value:
 636            pass
 637        elif value:
 638            pass
 639        elif value:
 640            pass
 641        elif value:
 642            pass
 643        elif value:
 644            pass
 645        elif value:
 646            pass
 647        elif value:
 648            pass
 649        elif value:
 650            pass
 651        elif value:
 652            pass
 653        elif value:
 654            pass
 655        elif value:
 656            pass
 657        elif value:
 658            pass
 659        elif value:
 660            pass
 661        elif value:
 662            pass
 663        elif value:
 664            pass
 665        elif value:
 666            pass
 667        elif value:
 668            pass
 669        elif value:
 670            pass
 671        elif value:
 672            pass
 673        elif value:
 674            pass
 675        elif value:
 676            pass
 677        elif value:
 678            pass
 679        elif value:
 680            pass
 681        elif value:
 682            pass
 683        elif value:
 684            pass
 685        elif value:
 686            pass
 687        elif value:
 688            pass
 689        elif value:
 690            pass
 691        elif value:
 692            pass
 693        elif value:
 694            pass
 695        elif value:
 696            pass
 697        elif value:
 698            pass
 699        elif value:
 700            pass
 701        elif value:
 702            pass
 703        elif value:
 704            pass
 705        elif value:
 706            pass
 707        elif value:
 708            pass
 709        elif value:
 710            pass
 711        elif value:
 712            pass
 713        elif value:
 714            pass
 715        elif value:
 716            pass
 717        elif value:
 718            pass
 719        elif value:
 720            pass
 721        elif value:
 722            pass
 723        elif value:
 724            pass
 725        elif value:
 726            pass
 727        elif value:
 728            pass
 729        elif value:
 730            pass
 731        elif value:
 732            pass
 733        elif value:
 734            pass
 735        elif value:
 736            pass
 737        elif value:
 738            pass
 739        elif value:
 740            pass
 741        elif value:
 742            pass
 743        elif value:
 744            pass
 745        elif value:
 746            pass
 747        elif value:
 748            pass
 749        elif value:
 750            pass
 751        elif value:
 752            pass
 753        elif value:
 754            pass
 755        elif value:
 756            pass
 757        elif value:
 758            pass
 759        elif value:
 760            pass
 761        elif value:
 762            pass
 763        elif value:
 764            pass
 765        elif value:
 766            pass
 767        elif value:
 768            pass
 769        elif value:
 770            pass
 771        elif value:
 772            pass
 773        elif value:
 774            pass
 775        elif value:
 776            pass
 777        elif value:
 778            pass
 779        elif value:
 780            pass
 781        elif value:
 782            pass
 783        elif value:
 784            pass
 785        elif value:
 786            pass
 787        elif value:
 788            pass
 789        elif value:
 790            pass
 791        elif value:
 792            pass
 793        elif value:
 794            pass
 795        elif value:
 796            pass
 797        elif value:
 798            pass
 799        elif value:
 800            pass
 801        elif value:
 802            pass
 803        elif value:
 804            pass
 805        elif value:
 806            pass
 807        elif value:
 808            pass
 809        elif value:
 810            pass
 811        elif value:
 812            pass
 813        elif value:
 814            pass
 815        elif value:
 816            pass
 817        elif value:
 818            pass
 819        elif value:
 820            pass
 821        elif value:
 822            pass
 823        elif value:
 824            pass
 825        elif value:
 826            pass
 827        elif value:
 828            pass
 829        elif value:
 830            pass
 831        elif value:
 832            pass
 833        elif value:
 834            pass
 835        elif value:
 836            pass
 837        elif value:
 838            pass
 839        elif value:
 840            pass
 841        elif value:
 842            pass
 843        elif value:
 844            pass
 845        elif value:
 846            pass
 847        elif value:
 848            pass
 849        elif value:
 850            pass
 851        elif value:
 852            pass
 853        elif value:
 854            pass
 855        elif value:
 856            pass
 857        elif value:
 858            pass
 859        elif value:
 860            pass
 861        elif value:
 862            pass
 863        elif value:
 864            pass
 865        elif value:
 866            pass
 867        elif value:
 868            pass
 869        elif value:
 870            pass
 871        elif value:
 872            pass
 873        elif value:
 874            pass
 875        elif value:
 876            pass
 877        elif value:
 878            pass
 879        elif value:
 880            pass
 881        elif value:
 882            pass
 883        elif value:
 884            pass
 885        elif value:
 886            pass
 887        elif value:
 888            pass
 889        elif value:
 890            pass
 891        elif value:
 892            pass
 893        elif value:
 894            pass
 895        elif value:
 896            pass
 897        elif value:
 898            pass
 899        elif value:
 900            pass
 901        elif value:
 902            pass
 903        elif value:
 904            pass
 905        elif value:
 906            pass
 907        elif value:
 908            pass
 909        elif value:
 910            pass
 911        elif value:
 912            pass
 913        elif value:
 914            pass
 915        elif value:
 916            pass
 917        elif value:
 918            pass
 919        elif value:
 920            pass
 921        elif value:
 922            pass
 923        elif value:
 924            pass
 925        elif value:
 926            pass
 927        elif value:
 928            pass
 929        elif value:
 930            pass
 931        elif value:
 932            pass
 933        elif value:
 934            pass
 935        elif value:
 936            pass
 937        elif value:
 938            pass
 939        elif value:
 940            pass
 941        elif value:
 942            pass
 943        elif value:
 944            pass
 945        elif value:
 946            pass
 947        elif value:
 948            pass
 949        elif value:
 950            pass
 951        elif value:
 952            pass
 953        elif value:
 954            pass
 955        elif value:
 956            pass
 957        elif value:
 958            pass
 959        elif value:
 960            pass
 961        elif value:
 962            pass
 963        elif value:
 964            pass
 965        elif value:
 966            pass
 967        elif value:
 968            pass
 969        elif value:
 970            pass
 971        elif value:
 972            pass
 973        elif value:
 974            pass
 975        elif value:
 976            pass
 977        elif value:
 978            pass
 979        elif value:
 980            pass
 981        elif value:
 982            pass
 983        elif value:
 984            pass
 985        elif value:
 986            pass
 987        elif value:
 988            pass
 989        elif value:
 990            pass
 991        elif value:
 992            pass
 993        elif value:
 994            pass
 995        elif value:
 996            pass
 997        elif value:
 998            pass
 999        elif value:
1000            pass
1001        elif value:
1002            pass
1003        elif value:
1004            pass
1005        elif value:
1006            pass
1007        elif value:
1008            pass
1009        elif value:
1010            pass
1011        elif value:
1012            pass
1013        elif value:
1014            pass
1015        elif value:
1016            pass
1017        elif value:
1018            pass
1019        elif value:
1020            pass
1021        elif value:
1022            pass
1023        elif value:
1024            pass
1025        elif value:
1026            pass
1027        elif value:
1028            pass
1029        elif value:
1030            pass
1031        elif value:
1032            pass
1033        elif value:
1034            pass
1035        elif value:
1036            pass
1037        elif value:
1038            pass
1039        elif value:
1040            pass
1041        elif value:
1042            pass
1043        elif value:
1044            pass
1045        elif value:
1046            pass
1047        elif value:
1048            pass
1049        elif value:
1050            pass
1051        elif value:
1052            pass
1053        elif value:
1054            pass
1055        elif value:
1056            pass
1057        elif value:
1058            pass
1059        elif value:
1060            pass
1061        elif value:
1062            pass
1063        elif value:
1064            pass
1065        elif value:
1066            pass
1067        elif value:
1068            pass
1069        elif value:
1070            pass
1071        elif value:
1072            pass
1073        elif value:
1074            pass
1075        elif value:
1076            pass
1077        elif value:
1078            pass
1079        elif value:
1080            pass
1081        elif value:
1082            pass
1083        elif value:
1084            pass
1085        elif value:
1086            pass
1087        elif value:
1088            pass
1089        elif value:
1090            pass
1091        elif value:
1092            pass
1093        elif value:
1094            pass
1095        elif value:
1096            pass
1097        elif value:
1098            pass
1099        elif value:
1100            pass
1101        elif value:
1102            pass
1103        elif value:
1104            pass
1105        elif value:
1106            pass
1107        elif value:
1108            pass
1109        elif value:
1110            pass
1111        elif value:
1112            pass
1113        elif value:
1114            pass
1115        elif value:
1116            pass
1117        elif value:
1118            pass
1119        elif value:
1120            pass
1121        elif value:
1122            pass
1123        elif value:
1124            pass
1125        elif value:
1126            pass
1127        elif value:
1128            pass
1129        elif value:
1130            pass
1131        elif value:
1132            pass
1133        elif value:
1134            pass
1135        elif value:
1136            pass
1137        elif value:
1138            pass
1139        elif value:
1140            pass
1141        elif value:
1142            pass
1143        elif value:
1144            pass
1145        elif True:
1146            return 23
1147
1148    for i in xrange(10000):
1149        AreEqual(f(True), 42)
1150
1151    for i in xrange(10000):
1152        AreEqual(f(False), 23)
1153
1154run_test(__name__)