Source

pyobjc / pyobjc-core / PyObjCTest / test_methods.py

Full commit
   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
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
"""
XXX: Should verify if test_methods2.py implements all tests in this file.

Test lowlevel message passing details (Python -> ObjC)


NOTE: 'long long' return-value test for calls from Objective-C are disabled,
  they crash the interpreter.

Done:
- Return simple values, but need to add limit-cases (exactly INT_MAX et.al.)
- Return structs, but need to add more complex cases
- Pass in basic types
- Pass in struct types, but need to add more complex cases
- Pass by reference of basic types (in, out, inout)
  [Need to add python side of case 'id']

Todo:
- Pass by reference of struct types (in, out, inout)
- more than 1 argument
- More argument conversions

Also Todo: Passing from Objective-C to python
- Using NSInvocation '[obj forwardInvocation:inv]'
- Using normal calls
- Done: Return simple values, return structs, pass in basic types (c part)

NOTES:
- Always use makeCFloat when testing the value of a C float against a
  precomputed (literal) value. Just comparing against a Python float won't work
  as that is a C double which has a different representation from C floats
  resulting in spurious test failures.
- See also testbndl.m, the implementation there must be synchronized
  with this file.
"""
from __future__ import unicode_literals
from PyObjCTools.TestSupport import *
import objc
import sys
import struct

# Can't set the right signatures in plain Objective-C.
for method, argmeta in [
        ( b"passInOutChar:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutChar:", { 2: dict(type_modifier=b'o')}),
        ( b"passInChar:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutUChar:", { 2: dict(type_modifier=b'N', type='^C')}),
        ( b"passOutUChar:", { 2: dict(type_modifier=b'o', type='^C')}),
        ( b"passInUChar:", { 2: dict(type_modifier=b'n', type='^C')}),
        ( b"passInOutShort:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutShort:", { 2: dict(type_modifier=b'o')}),
        ( b"passInShort:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutUShort:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutUShort:", { 2: dict(type_modifier=b'o')}),
        ( b"passInUShort:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutInt:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutInt:", { 2: dict(type_modifier=b'o')}),
        ( b"passInInt:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutUInt:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutUInt:", { 2: dict(type_modifier=b'o')}),
        ( b"passInUInt:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutLong:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutLong:", { 2: dict(type_modifier=b'o')}),
        ( b"passInLong:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutULong:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutULong:", { 2: dict(type_modifier=b'o')}),
        ( b"passInULong:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutLongLong:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutLongLong:", { 2: dict(type_modifier=b'o')}),
        ( b"passInLongLong:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutULongLong:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutULongLong:", { 2: dict(type_modifier=b'o')}),
        ( b"passInULongLong:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutFloat:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutFloat:", { 2: dict(type_modifier=b'o')}),
        ( b"passInFloat:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutDouble:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutDouble:", { 2: dict(type_modifier=b'o')}),
        ( b"passInDouble:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutCharp:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutCharp:", { 2: dict(type_modifier=b'o')}),
        ( b"passInCharp:", { 2: dict(type_modifier=b'n')}),
        ( b"passInOutID:", { 2: dict(type_modifier=b'N')}),
        ( b"passOutID:", { 2: dict(type_modifier=b'o')}),
        ( b"passInID:", { 2: dict(type_modifier=b'n')}),
    ]:

    objc.registerMetaDataForSelector(b"OC_TestClass1", method,
                    dict(arguments=argmeta))


from PyObjCTest.testbndl import *

def makeCFloat(value):
    """
    C floats and doubles have a different representation, this function returns
    the result of converting a python float (== C double) to a C float and back.
    """
    if sys.version_info[0] == 2:
        struct_fmt = b'f'
    else:
        struct_fmt = 'f'

    return struct.unpack(struct_fmt,struct.pack(struct_fmt, value))[0]


class PyOCTestSimpleReturns(TestCase):
    #
    # Test returns of simple types from instance and classs methods
    #
    def testClsLongLong(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.longlongClsFunc(), -(1 << 60))
        self.assertEqual(OC_TestClass1.longlongClsFunc(), -42)
        self.assertEqual(OC_TestClass1.longlongClsFunc(), 0)
        self.assertEqual(OC_TestClass1.longlongClsFunc(), 42)
        self.assertEqual(OC_TestClass1.longlongClsFunc(), (1 << 60))

    def testClsULongLong(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.ulonglongClsFunc(), 0)
        self.assertEqual(OC_TestClass1.ulonglongClsFunc(), 42)
        self.assertEqual(OC_TestClass1.ulonglongClsFunc(), (1 << 63))

    def testClsLong(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.longClsFunc(), -(1 << 30))
        self.assertEqual(OC_TestClass1.longClsFunc(), -42)
        self.assertEqual(OC_TestClass1.longClsFunc(), 0)
        self.assertEqual(OC_TestClass1.longClsFunc(), 42)
        self.assertEqual(OC_TestClass1.longClsFunc(), (1 << 30))

    def testClsULong(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.ulongClsFunc(), 0)
        self.assertEqual(OC_TestClass1.ulongClsFunc(), 42)
        self.assertEqual(OC_TestClass1.ulongClsFunc(), (1 << 30))

    def testClsInt(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.intClsFunc(), -(1 << 30))
        self.assertEqual(OC_TestClass1.intClsFunc(), -42)
        self.assertEqual(OC_TestClass1.intClsFunc(), 0)
        self.assertEqual(OC_TestClass1.intClsFunc(), 42)
        self.assertEqual(OC_TestClass1.intClsFunc(), (1 << 30))

    def testClsUInt(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.uintClsFunc(), 0)
        self.assertEqual(OC_TestClass1.uintClsFunc(), 42)
        self.assertEqual(OC_TestClass1.uintClsFunc(), (1 << 30))

    def testClsShort(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.shortClsFunc(), -(1 << 14))
        self.assertEqual(OC_TestClass1.shortClsFunc(), -42)
        self.assertEqual(OC_TestClass1.shortClsFunc(), 0)
        self.assertEqual(OC_TestClass1.shortClsFunc(), 42)
        self.assertEqual(OC_TestClass1.shortClsFunc(), (1 << 14))

    def testClsUShort(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.ushortClsFunc(), 0)
        self.assertEqual(OC_TestClass1.ushortClsFunc(), 42)
        self.assertEqual(OC_TestClass1.ushortClsFunc(), (1 << 14))

    def testClsChar(self):
        # Fails with libffi due to the way we treat 'char' at the moment
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.charClsFunc(), -128)
        self.assertEqual(OC_TestClass1.charClsFunc(), 0)
        self.assertEqual(OC_TestClass1.charClsFunc(), 127)

    def testClsUChar(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.ucharClsFunc(), 0)
        self.assertEqual(OC_TestClass1.ucharClsFunc(), 128)
        self.assertEqual(OC_TestClass1.ucharClsFunc(), 255)

    def testClsFloat(self):
        # Fails, possibly rounding error
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.floatClsFunc(), makeCFloat(0.128))
        self.assertEqual(OC_TestClass1.floatClsFunc(), makeCFloat(1.0))
        self.assertEqual(OC_TestClass1.floatClsFunc(), makeCFloat(42.0))
        self.assertEqual(OC_TestClass1.floatClsFunc(), makeCFloat(1e10))

    def testClsDouble(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.doubleClsFunc(), 0.128)
        self.assertEqual(OC_TestClass1.doubleClsFunc(), 1.0)
        self.assertEqual(OC_TestClass1.doubleClsFunc(), 42.0)
        self.assertEqual(OC_TestClass1.doubleClsFunc(), 1e10)

    def testClsCharp(self):
        OC_TestClass1.clsReset()
        self.assertEqual(OC_TestClass1.charpClsFunc(), b'hello')
        self.assertEqual(OC_TestClass1.charpClsFunc(), b'world')
        self.assertEqual(OC_TestClass1.charpClsFunc(), b'foobar')

    def testClsID(self):
        OC_TestClass1.clsReset()
        self.assertEqual(len(OC_TestClass1.idClsFunc()), 0)
        self.assertEqual(type(OC_TestClass1.idClsFunc()).__name__, 'NSHost')
        self.assertIn(str(OC_TestClass1.idClsFunc()), ('{}', '{\n}'))
        self.assertEqual(OC_TestClass1.idClsFunc(), None)

    # testCls* ends here

    def testLongLong(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.longlongFunc(), -(1 << 60))
        self.assertEqual(obj.longlongFunc(), -42)
        self.assertEqual(obj.longlongFunc(), 0)
        self.assertEqual(obj.longlongFunc(), 42)
        self.assertEqual(obj.longlongFunc(), (1 << 60))

    def testULongLong(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.ulonglongFunc(), 0)
        self.assertEqual(obj.ulonglongFunc(), 42)
        self.assertEqual(obj.ulonglongFunc(), (1 << 63))

    def testLong(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.longFunc(), -(1 << 30))
        self.assertEqual(obj.longFunc(), -42)
        self.assertEqual(obj.longFunc(), 0)
        self.assertEqual(obj.longFunc(), 42)
        self.assertEqual(obj.longFunc(), (1 << 30))

    def testULong(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.ulongFunc(), 0)
        self.assertEqual(obj.ulongFunc(), 42)
        self.assertEqual(obj.ulongFunc(), (1 << 30))

    def testInt(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.intFunc(), -(1 << 30))
        self.assertEqual(obj.intFunc(), -42)
        self.assertEqual(obj.intFunc(), 0)
        self.assertEqual(obj.intFunc(), 42)
        self.assertEqual(obj.intFunc(), (1 << 30))

    def testUInt(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.uintFunc(), 0)
        self.assertEqual(obj.uintFunc(), 42)
        self.assertEqual(obj.uintFunc(), (1 << 30))

    def testShort(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.shortFunc(), -(1 << 14))
        self.assertEqual(obj.shortFunc(), -42)
        self.assertEqual(obj.shortFunc(), 0)
        self.assertEqual(obj.shortFunc(), 42)
        self.assertEqual(obj.shortFunc(), (1 << 14))

    def testUShort(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.ushortFunc(), 0)
        self.assertEqual(obj.ushortFunc(), 42)
        self.assertEqual(obj.ushortFunc(), (1 << 14))

    def testChar(self):
        obj = OC_TestClass1.new()
        # Fails with libffi due to the way we treat 'char' at the moment
        obj.reset()
        self.assertEqual(obj.charFunc(), -128)
        self.assertEqual(obj.charFunc(), 0)
        self.assertEqual(obj.charFunc(), 127)

    def testUChar(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.ucharFunc(), 0)
        self.assertEqual(obj.ucharFunc(), 128)
        self.assertEqual(obj.ucharFunc(), 255)

    def testFloat(self):
        # Fails, possibly rounding error
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.floatFunc(), makeCFloat(0.128))
        self.assertEqual(obj.floatFunc(), makeCFloat(1.0))
        self.assertEqual(obj.floatFunc(), makeCFloat(42.0))
        self.assertEqual(obj.floatFunc(), makeCFloat(1e10))

    def testDouble(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.doubleFunc(), 0.128)
        self.assertEqual(obj.doubleFunc(), 1.0)
        self.assertEqual(obj.doubleFunc(), 42.0)
        self.assertEqual(obj.doubleFunc(), 1e10)

    def testCharp(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(obj.charpFunc(), b'hello')
        self.assertEqual(obj.charpFunc(), b'world')
        self.assertEqual(obj.charpFunc(), b'foobar')

    def testID(self):
        obj = OC_TestClass1.new()
        obj.reset()
        self.assertEqual(len(obj.idFunc()), 0)
        self.assertEqual(type(obj.idFunc()).__name__, 'NSHost')
        self.assertIn(str(obj.idFunc()), ('{}', '{\n}'))
        self.assertEqual(obj.idFunc(), None)

    def testStruct1(self):
        obj = OC_TestClass1.new()

        self.assertEqual(obj.dummyFunc(), (-1, 1))

    def testStruct2(self):
        obj = OC_TestClass1.new()

        self.assertEqual(obj.dummy2Func(), ((1,2,3,4),))


class PyOCTestSimpleArguments(TestCase):
    #
    # Test argument passing of single basic values.
    #
    def setUp(self):
        self.obj = OC_TestClass1.new()

    def testLongLong(self):
        self.assertEqual(self.obj.longlongArg_(0), 0)
        self.assertEqual(self.obj.longlongArg_(10), 5)
        self.assertEqual(self.obj.longlongArg_(10), 5)
        self.assertEqual(self.obj.longlongArg_(1 << 60), (1 << 59))
        self.assertEqual(self.obj.longlongArg_(-10), -5)
        self.assertEqual(self.obj.longlongArg_(-10), -5)
        self.assertEqual(self.obj.longlongArg_(-1 << 60), -(1 << 59))

        # TODO: Out of range values

    def testULongLong(self):
        self.assertEqual(self.obj.ulonglongArg_(0), 0)
        self.assertEqual(self.obj.ulonglongArg_(10), 5)
        self.assertEqual(self.obj.ulonglongArg_(10), 5)
        self.assertEqual(self.obj.ulonglongArg_(1 << 60), (1 << 59))

        # TODO: Out of range values

    def testLong(self):
        self.assertEqual(self.obj.longArg_(0), 0)
        self.assertEqual(self.obj.longArg_(10), 5)
        self.assertEqual(self.obj.longArg_(10), 5)
        self.assertEqual(self.obj.longArg_(1 << 30), (1 << 29))
        self.assertEqual(self.obj.longArg_(-10), -5)
        self.assertEqual(self.obj.longArg_(-10), -5)
        self.assertEqual(self.obj.longArg_(-1 << 30), -(1 << 29))

        # TODO: Out of range values

    def testULong(self):
        self.assertEqual(self.obj.ulongArg_(0), 0)
        self.assertEqual(self.obj.ulongArg_(10), 5)
        self.assertEqual(self.obj.ulongArg_(10), 5)
        self.assertEqual(self.obj.ulongArg_(1 << 30), (1 << 29))

        # TODO: Out of range values

    def testInt(self):
        self.assertEqual(self.obj.intArg_(0), 0)
        self.assertEqual(self.obj.intArg_(10), 5)
        self.assertEqual(self.obj.intArg_(10), 5)
        self.assertEqual(self.obj.intArg_(1 << 30), (1 << 29))
        self.assertEqual(self.obj.intArg_(-10), -5)
        self.assertEqual(self.obj.intArg_(-10), -5)
        self.assertEqual(self.obj.intArg_(-1 << 30), -(1 << 29))

        self.assertEqual(self.obj.intArg_(10.0), 5)

        self.assertRaises(ValueError, self.obj.intArg_, sys.maxsize+1)
        self.assertRaises(ValueError, self.obj.intArg_, -sys.maxsize-2)
        self.assertRaises(ValueError, self.obj.intArg_, -float(sys.maxsize)-2)

    def testUInt(self):
        self.assertEqual(self.obj.uintArg_(0), 0)
        self.assertEqual(self.obj.uintArg_(10), 5)
        self.assertEqual(self.obj.uintArg_(10), 5)
        self.assertEqual(self.obj.uintArg_(1 << 30), (1 << 29))

        self.assertEqual(self.obj.uintArg_(10.0), 5)

        self.assertRaises(ValueError, self.obj.uintArg_, -5)
        self.assertRaises(ValueError, self.obj.uintArg_, -5)
        self.assertRaises(ValueError, self.obj.uintArg_, 1+2*(sys.maxsize+1))


    def testShort(self):
        self.assertEqual(self.obj.shortArg_(0), 0)
        self.assertEqual(self.obj.shortArg_(10), 5)
        self.assertEqual(self.obj.shortArg_(10), 5)
        self.assertEqual(self.obj.shortArg_(1 << 14), (1 << 13))
        self.assertEqual(self.obj.shortArg_(-10), -5)
        self.assertEqual(self.obj.shortArg_(-10), -5)
        self.assertEqual(self.obj.shortArg_(-(1 << 14)), -(1 << 13))

        self.assertEqual(self.obj.shortArg_(10.0), 5)

        # Out of range arguments, assumes a short is 16 bits
        self.assertRaises(ValueError, self.obj.shortArg_, -(1<<16)-1)
        self.assertRaises(ValueError, self.obj.shortArg_, 1<<16)
        self.assertRaises(ValueError, self.obj.shortArg_, -(1<<16)-1)
        self.assertRaises(ValueError, self.obj.shortArg_, 1<<16)

    def testUShort(self):
        self.assertEqual(self.obj.ushortArg_(0), 0)
        self.assertEqual(self.obj.ushortArg_(10), 5)
        self.assertEqual(self.obj.ushortArg_(10), 5)
        self.assertEqual(self.obj.ushortArg_(1 << 14), (1 << 13))

        self.assertEqual(self.obj.ushortArg_(10.0), 5)

        # Out of range arguments, assumes a short is 16 bits
        self.assertRaises(ValueError, self.obj.ushortArg_, -5)
        self.assertRaises(ValueError, self.obj.ushortArg_, -(1<<16)-1)
        self.assertRaises(ValueError, self.obj.ushortArg_, 1<<16)
        self.assertRaises(ValueError, self.obj.ushortArg_, -5)
        self.assertRaises(ValueError, self.obj.ushortArg_, -(1<<16)-1)
        self.assertRaises(ValueError, self.obj.ushortArg_, 1<<16)


    def testChar(self):
        self.assertEqual(self.obj.charArg_(0), (0))
        self.assertEqual(self.obj.charArg_(10), (5))
        self.assertEqual(self.obj.charArg_(10), (5))
        self.assertEqual(self.obj.charArg_(1 << 6), (1 << 5))
        self.assertEqual(self.obj.charArg_(b'\x00'), 0x00)
        self.assertEqual(self.obj.charArg_(b'\x10'), int(ord(b'\x10')/2))

        # TODO: Out of range arguments

    def testUChar(self):
        self.assertEqual(self.obj.ucharArg_(0), 0)
        self.assertEqual(self.obj.ucharArg_(10), 5)
        self.assertEqual(self.obj.ucharArg_(10), 5)
        self.assertEqual(self.obj.ucharArg_(1 << 7), (1 << 6))
        self.assertEqual(self.obj.ucharArg_(b'\x00'), 0)
        self.assertEqual(self.obj.ucharArg_(b'\x10'), int(ord(b'\x10')/2))

        self.assertEqual(self.obj.ucharArg_(10.0), 5)

        # Out of range arguments
        self.assertRaises(ValueError, self.obj.ucharArg_, -5)
        self.assertRaises(ValueError, self.obj.ucharArg_, -256)
        self.assertRaises(ValueError, self.obj.ucharArg_, 256)
        self.assertRaises(ValueError, self.obj.ucharArg_, -5)
        self.assertRaises(ValueError, self.obj.ucharArg_, -256)
        self.assertRaises(ValueError, self.obj.ucharArg_, 256)

    def testCharp(self):
        self.assertEqual(self.obj.charpArg_(b"hello world"), b'dlrow olleh')

        self.assertRaises(ValueError, self.obj.charpArg_, 256)
        self.assertRaises(ValueError, self.obj.charpArg_, "hello world")

    def testIDPython(self):
        # Test a Python object as the argument
        s = self.obj.idArg_("hello")
        self.assertEqual(len(s), 1)
        self.assertEqual(s[0], "hello")

    def testIDOC(self):
        # Test an Objective-C object as the argument

        # NSHost gives problems on GNUstep, better check those problems
        # seperately in the Foundation test suite.
        #c = objc.lookUpClass("NSHost")
        #o = c.hostWithAddress_('127.0.0.1')

        c = objc.lookUpClass("NSScanner")
        o = c.scannerWithString_("hello world")
        s = self.obj.idArg_(o)
        self.assertEqual(len(s), 1)
        self.assertIs(s[0], o)

    def testStruct1(self):
        self.assertEqual(self.obj.dummyArg_((-1, 1)), (-2, 2))

        self.assertRaises(TypeError, self.obj.dummyArg_, 256)
        self.assertRaises(ValueError, self.obj.dummyArg_, (-1,))
        self.assertRaises(ValueError, self.obj.dummyArg_, (-1,1,2))

    def testStruct2(self):
        self.assertEqual(self.obj.dummy2Arg_(((1,2,3,4),)), ((8,6,4,2),))

        self.assertRaises(ValueError, self.obj.dummy2Arg_, ((8,6,4,2),1))
        self.assertRaises(ValueError, self.obj.dummy2Arg_, ((8,6,4,),))
        self.assertRaises(ValueError, self.obj.dummy2Arg_, ((8,6,4,2,1),))


class PyOCTestByReferenceArguments(TestCase):
    #
    # Test argument passing of single basic values by reference.
    #
    def setUp(self):
        self.obj = OC_TestClass1.new()

    def testCharIn(self):
        self.assertEqual(self.obj.passInChar_(b'\x10'), 0x19)

    def testCharOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutChar_(None), -128)
        self.assertEqual(self.obj.passOutChar_(None), 0)
        self.assertEqual(self.obj.passOutChar_(None), 127)

    def testCharInOut(self):
        self.assertEqual(self.obj.passInOutChar_(b'\x10'), 0x3a)

    def testUCharIn(self):
        self.assertEqual(self.obj.passInUChar_(10), 19)

    def testUCharOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutUChar_(None), 0)
        self.assertEqual(self.obj.passOutUChar_(None), 128)
        self.assertEqual(self.obj.passOutUChar_(None), 255)

    def testUCharInOut(self):
        self.assertEqual(self.obj.passInOutUChar_(10), 52)

    def testShortIn(self):
        self.assertEqual(self.obj.passInShort_(10), 19)
        self.assertEqual(self.obj.passInShort_(-100), -91)
        self.assertEqual(self.obj.passInShort_(-100.0), -91)

    def testShortOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutShort_(None), -(1 << 14))
        self.assertEqual(self.obj.passOutShort_(None), -42)
        self.assertEqual(self.obj.passOutShort_(None), 0)
        self.assertEqual(self.obj.passOutShort_(None), 42)
        self.assertEqual(self.obj.passOutShort_(None), 1 << 14)

    def testShortInOut(self):
        self.assertEqual(self.obj.passInOutShort_(10), 52)
        self.assertEqual(self.obj.passInOutShort_(-100), -58)
        self.assertEqual(self.obj.passInOutShort_(-100.0), -58)

    def testUShortIn(self):
        self.assertEqual(self.obj.passInUShort_(10), 19)

    def testUShortOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutUShort_(None), 0)
        self.assertEqual(self.obj.passOutUShort_(None), 42)
        self.assertEqual(self.obj.passOutUShort_(None), (1 << 14))

    def testUShortInOut(self):
        self.assertEqual(self.obj.passInOutUShort_(10), 52)

    def testIntIn(self):
        self.assertEqual(self.obj.passInInt_(10), 19)
        self.assertEqual(self.obj.passInInt_(-100), -91)

    def testIntOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutInt_(None), -(1 << 30))
        self.assertEqual(self.obj.passOutInt_(None), -42)
        self.assertEqual(self.obj.passOutInt_(None), 0)
        self.assertEqual(self.obj.passOutInt_(None), 42)
        self.assertEqual(self.obj.passOutInt_(None), (1 << 30))

    def testIntInOut(self):
        self.assertEqual(self.obj.passInOutInt_(10), 52)
        self.assertEqual(self.obj.passInOutInt_(-100), -58)
        self.assertEqual(self.obj.passInOutInt_(-100.0), -58)

    def testUIntIn(self):
        self.assertEqual(self.obj.passInUInt_(10), 19)

    def testUIntOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutUInt_(None), 0)
        self.assertEqual(self.obj.passOutUInt_(None), 42)
        self.assertEqual(self.obj.passOutUInt_(None), (1 << 30))

    def testUIntInOut(self):
        self.assertEqual(self.obj.passInOutUInt_(10), 52)
        self.assertEqual(self.obj.passInOutUInt_(10.0), 52)

    def testLongIn(self):
        self.assertEqual(self.obj.passInLong_(10), 19)
        self.assertEqual(self.obj.passInLong_(-100), -91)

    def testLongOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutLong_(None), -(1 << 30))
        self.assertEqual(self.obj.passOutLong_(None), -42)
        self.assertEqual(self.obj.passOutLong_(None), 0)
        self.assertEqual(self.obj.passOutLong_(None), 42)
        self.assertEqual(self.obj.passOutLong_(None), (1 << 30))

    def testLongInOut(self):
        self.assertEqual(self.obj.passInOutLong_(10), 52)
        self.assertEqual(self.obj.passInOutLong_(-100), -58)
        self.assertEqual(self.obj.passInOutLong_(-100.0), -58)

    def testULongIn(self):
        self.assertEqual(self.obj.passInULong_(10), 19)

    def testULongOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutULong_(None), 0)
        self.assertEqual(self.obj.passOutULong_(None), 42)
        self.assertEqual(self.obj.passOutULong_(None), (1 << 30))

    def testULongInOut(self):
        self.assertEqual(self.obj.passInOutULong_(10), 52)
        self.assertEqual(self.obj.passInOutULong_(10.0), 52)

    def testLongLongIn(self):
        self.assertEqual(self.obj.passInLongLong_(10), 19)
        self.assertEqual(self.obj.passInLongLong_(-100), -91)

    def testLongLongOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutLongLong_(None), -(1 << 60))
        self.assertEqual(self.obj.passOutLongLong_(None), -42)
        self.assertEqual(self.obj.passOutLongLong_(None), 0)
        self.assertEqual(self.obj.passOutLongLong_(None), 42)
        self.assertEqual(self.obj.passOutLongLong_(None), (1 << 60))

    def testLongLongInOut(self):
        self.assertEqual(self.obj.passInOutLongLong_(10), 52)
        self.assertEqual(self.obj.passInOutLongLong_(-100), -58)
        self.assertEqual(self.obj.passInOutLongLong_(-100.0), -58)

    def testULongLongIn(self):
        self.assertEqual(self.obj.passInULongLong_(10), 19)

    def testULongLongOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutULongLong_(None), 0)
        self.assertEqual(self.obj.passOutULongLong_(None), 42)
        self.assertEqual(self.obj.passOutULongLong_(None), (1 << 63))

    def testULongLongInOut(self):
        self.assertEqual(self.obj.passInOutULongLong_(10), 52)
        self.assertEqual(self.obj.passInOutULongLong_(10.0), 52)

    def testFloatIn(self):
        self.assertEqual(self.obj.passInFloat_(10), makeCFloat(90.0))
        self.assertEqual(self.obj.passInFloat_(0.11), makeCFloat(0.99))

    def testFloatOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutFloat_(None), makeCFloat(0.128))
        self.assertEqual(self.obj.passOutFloat_(None), makeCFloat(1.0))
        self.assertEqual(self.obj.passOutFloat_(None), makeCFloat(42.0))
        self.assertEqual(self.obj.passOutFloat_(None), makeCFloat(1e10))

    def testFloatInOut(self):
        self.assertEqual(self.obj.passInOutFloat_(10), makeCFloat(420))
        self.assertEqual(self.obj.passInOutFloat_(10.0), makeCFloat(420))
        self.assertEqual(self.obj.passInOutFloat_(0.01), makeCFloat(0.42))

    def testDoubleIn(self):
        self.assertEqual(self.obj.passInDouble_(10), 90.0)
        self.assertEqual(self.obj.passInDouble_(0.11), 0.99)

    def testDoubleOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutDouble_(None), 0.128)
        self.assertEqual(self.obj.passOutDouble_(None), 1.0)
        self.assertEqual(self.obj.passOutDouble_(None), 42.0)
        self.assertEqual(self.obj.passOutDouble_(None), 1e10)

    def testDoubleInOut(self):
        self.assertEqual(self.obj.passInOutDouble_(10), 420)
        self.assertEqual(self.obj.passInOutDouble_(10.0), 420)
        self.assertEqual(self.obj.passInOutDouble_(0.01), 0.42)

    def testCharpIn(self):
        self.assertEqual(self.obj.passInCharp_(b"hello"), b"hheelllloo")
        self.assertEqual(self.obj.passInCharp_(b"abcde"), b"aabbccddee")

    def testCharpOut(self):
        self.obj.reset()
        self.assertEqual(self.obj.passOutCharp_(None), b"hello")
        self.assertEqual(self.obj.passOutCharp_(None), b"world")
        self.assertEqual(self.obj.passOutCharp_(None), b"foobar")

    def testCharpInOut(self):
        self.assertEqual(self.obj.passInOutCharp_(b"hello"), b"hheelllloo")
        self.assertEqual(self.obj.passInOutCharp_(b"abcdef"), b"aabbccddeeff")

    # TODO: structs (including Objective-C part)



#
# Below this point only TestCases that test calling Python from Objective-C
#
# Notes:
# - There is both a pure python and an Python-ObjC hybrid class
#   (MyPyClass and MyOCClass). The former is not used in the first few tests,
#   we need to decide what functionality we'll support for these (e.g. do
#   we support selector objects, or only normal methods?)
# - Every test below should exists in two variants:
#   * 'invoke' - Calls back from ObjC to Python using an NSInvocation
#                and the forwardInvocation method
#   * 'call'   - Calls back from ObjC to Python using a normal ObjC method call

# Values for
# - 8-bit char
# - 16-bit short
# - 32-bit int/long
# - 64-bit long long
# Values are: max-negative, -42, 0, 42, max-positive, out-of-range, bad-type, None
#
CHAR_NUMBERS=[ -128, -42, 0, 42, 127, b'a', 128, "hello", None ]
UCHAR_NUMBERS=[ 0, 42, 255, b'a', 256, "hello", None ]
SHORT_NUMBERS=[ -32768, -42, 0, 32767, 32768, "hello", None ]
USHORT_NUMBERS=[ 0, 42, 65535, 65536, "hello", None ]
INT_NUMBERS=[ -2147483648, -42, 0, 2147483647, 2147483648, "hello", None ]
UINT_NUMBERS=[ 0, 42, 4294967295, 4294967296, "hello", None ]
LONG_NUMBERS=[ -2147483648, -42, 0, 2147483647, sys.maxsize+1, "hello", None ]
ULONG_NUMBERS=[ 0, 42, 4294967295, 2*(sys.maxsize+1), "hello", None ]
LONGLONG_NUMBERS=[ -9223372036854775808, -42, 0, 42, 9223372036854775807, 9223372036854775808, "hello", None ]
ULONGLONG_NUMBERS=[0, 42, 18446744073709551615, 18446744073709551616, "hello", None ]

FLOAT_NUMBERS = [ makeCFloat(0.1), makeCFloat(100.0) ]
DOUBLE_NUMBERS = [ 1.5, 3.5, 1e10, 1.99e10 ]
OBJECTS = [ "hello", 1.0, range(4), lambda x: 10 ]
DUMMY_OBJECTS = [ (1, 1), (-10, -10), (-4, -5), (0, 0), (10, 20) ]
DUMMY2_OBJECTS = [ ((1, 2, 3, 4),), ((-9, -8, -7, -6),)]
POINTS=[ (1.0, 2.0), (1e10, 2e10), (-0.5, 0.5) ]

class MyPyClass:
    def __init__(self):
        self.idx = 0

    def reset(self):
        self.idx = 0

    def idFunc(self):
        i = self.idx
        self.idx += 1
        return OBJECTS[i]

class MyOCClass (objc.lookUpClass('NSObject')):
    def __init__(self):
        self.idx = 0

    def reset(self):
        self.idx = 0

    def charFunc(self):
        i = self.idx
        self.idx += 1
        return CHAR_NUMBERS[i]
    charFunc = objc.selector(charFunc, signature=OC_TestClass1.charFunc.signature)

    def ucharFunc(self):
        i = self.idx
        self.idx += 1
        return UCHAR_NUMBERS[i]
    ucharFunc = objc.selector(ucharFunc, signature=OC_TestClass1.ucharFunc.signature)

    def shortFunc(self):
        i = self.idx
        self.idx += 1
        return SHORT_NUMBERS[i]
    shortFunc = objc.selector(shortFunc, signature=OC_TestClass1.shortFunc.signature)

    def ushortFunc(self):
        i = self.idx
        self.idx += 1
        return USHORT_NUMBERS[i]
    ushortFunc = objc.selector(ushortFunc, signature=OC_TestClass1.ushortFunc.signature)

    def intFunc(self):
        i = self.idx
        self.idx += 1
        return INT_NUMBERS[i]
    intFunc = objc.selector(intFunc, signature=OC_TestClass1.intFunc.signature)

    def uintFunc(self):
        i = self.idx
        self.idx += 1
        return UINT_NUMBERS[i]
    uintFunc = objc.selector(uintFunc, signature=OC_TestClass1.uintFunc.signature)

    def longFunc(self):
        i = self.idx
        self.idx += 1
        return LONG_NUMBERS[i]
    longFunc = objc.selector(longFunc, signature=OC_TestClass1.longFunc.signature)

    def ulongFunc(self):
        i = self.idx
        self.idx += 1
        return ULONG_NUMBERS[i]
    ulongFunc = objc.selector(ulongFunc, signature=OC_TestClass1.ulongFunc.signature)

    def longlongFunc(self):
        i = self.idx
        self.idx += 1
        return LONGLONG_NUMBERS[i]
    longlongFunc = objc.selector(longlongFunc, signature=OC_TestClass1.longlongFunc.signature)

    def ulonglongFunc(self):
        i = self.idx
        self.idx += 1
        return ULONGLONG_NUMBERS[i]
    ulonglongFunc = objc.selector(ulonglongFunc, signature=OC_TestClass1.ulonglongFunc.signature)

    def floatFunc(self):
        i = self.idx
        self.idx += 1
        return FLOAT_NUMBERS[i]
    floatFunc = objc.selector(floatFunc, signature=OC_TestClass1.floatFunc.signature)

    def doubleFunc(self):
        i = self.idx
        self.idx += 1
        return DOUBLE_NUMBERS[i]
    doubleFunc = objc.selector(doubleFunc, signature=OC_TestClass1.doubleFunc.signature)

    def idFunc(self):
        i = self.idx
        self.idx += 1
        return OBJECTS[i]

    def dummyFunc(self):
        i = self.idx
        self.idx += 1
        return DUMMY_OBJECTS[i]
    dummyFunc = objc.selector(dummyFunc, signature=OC_TestClass1.dummyFunc.signature)

    def dummy2Func(self):
        i = self.idx
        self.idx += 1
        return DUMMY2_OBJECTS[i]
    dummy2Func = objc.selector(dummy2Func, signature=OC_TestClass1.dummy2Func.signature)

    def nspointFunc(self):
        i = self.idx
        self.idx += 1
        return POINTS[i]
    nspointFunc = objc.selector(nspointFunc, signature=OC_TestClass1.nspointFunc.signature)

    def charArg_(self, arg):
        return arg * 2
    charArg_ = objc.selector(charArg_, signature=OC_TestClass1.charArg_.signature)
    def ucharArg_(self, arg):
        return arg * 2
    ucharArg_ = objc.selector(ucharArg_, signature=OC_TestClass1.ucharArg_.signature)

    def shortArg_(self, arg):
        return arg * 2
    shortArg_ = objc.selector(shortArg_, signature=OC_TestClass1.shortArg_.signature)

    def ushortArg_(self, arg):
        return arg * 2
    ushortArg_ = objc.selector(ushortArg_, signature=OC_TestClass1.ushortArg_.signature)

    def intArg_(self, arg):
        return arg * 2
    intArg_ = objc.selector(intArg_, signature=OC_TestClass1.intArg_.signature)

    def uintArg_(self, arg):
        return arg * 2
    uintArg_ = objc.selector(uintArg_, signature=OC_TestClass1.uintArg_.signature)

    def longArg_(self, arg):
        return arg * 2
    longArg_ = objc.selector(longArg_, signature=OC_TestClass1.longArg_.signature)

    def ulongArg_(self, arg):
        return arg * 2
    ulongArg_ = objc.selector(ulongArg_, signature=OC_TestClass1.ulongArg_.signature)

    def longlongArg_(self, arg):
        return arg * 2
    longlongArg_ = objc.selector(longlongArg_, signature=OC_TestClass1.longlongArg_.signature)

    def ulonglongArg_(self, arg):
        return arg * 2
    ulonglongArg_ = objc.selector(ulonglongArg_, signature=OC_TestClass1.ulonglongArg_.signature)

    def floatArg_(self, arg):
        return arg * 2
    floatArg_ = objc.selector(floatArg_, signature=OC_TestClass1.floatArg_.signature)

    def doubleArg_(self, arg):
        return arg * 2
    doubleArg_ = objc.selector(doubleArg_, signature=OC_TestClass1.doubleArg_.signature)


class OCPyTestSimpleCalls(TestCase):
    #
    # Test argument passing of single basic values by reference.
    #
    def setUp(self):
        self.pyobj = MyPyClass()
        self.ocobj = MyOCClass.new()
        self.obj = OC_TestClass2.new()

    def testCChar(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in CHAR_NUMBERS[:-3]:
            if isinstance(o, (bytes, str)):
                o = ord(o)
            self.assertEqual(self.obj.callInstanceCharFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceCharFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceCharFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceCharFuncOf_, self.ocobj)

    def testIChar(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in CHAR_NUMBERS[:-3]:
            if isinstance(o, (str, bytes)):
                o = ord(o)
            self.assertEqual(self.obj.invokeInstanceCharFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceCharFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceCharFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceCharFuncOf_, self.ocobj)

    def testCUChar(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in UCHAR_NUMBERS[:-3]:
            if isinstance(o, (str, bytes)):
                o = ord(o)
            self.assertEqual(self.obj.callInstanceUnsignedCharFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceUnsignedCharFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedCharFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedCharFuncOf_, self.ocobj)

    def testIUChar(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in UCHAR_NUMBERS[:-3]:
            if isinstance(o, (bytes, str)):
                o = ord(o)
            self.assertEqual(self.obj.invokeInstanceUnsignedCharFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedCharFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedCharFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedCharFuncOf_, self.ocobj)

    def testCShort(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in SHORT_NUMBERS[:-3]:
            self.assertEqual(self.obj.callInstanceShortFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceShortFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceShortFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceShortFuncOf_, self.ocobj)

    def testIShort(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in SHORT_NUMBERS[:-3]:
            self.assertEqual(self.obj.invokeInstanceShortFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceShortFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceShortFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceShortFuncOf_, self.ocobj)

    def testCUShort(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in USHORT_NUMBERS[:-3]:
            self.assertEqual(self.obj.callInstanceUnsignedShortFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceUnsignedShortFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedShortFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedShortFuncOf_, self.ocobj)

    def testIUShort(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in USHORT_NUMBERS[:-3]:
            self.assertEqual(self.obj.invokeInstanceUnsignedShortFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedShortFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedShortFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedShortFuncOf_, self.ocobj)

    def testCInt(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in INT_NUMBERS[:-3]:
            self.assertEqual(self.obj.callInstanceIntFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceIntFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceIntFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceIntFuncOf_, self.ocobj)

    def testIInt(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in INT_NUMBERS[:-3]:
            self.assertEqual(self.obj.invokeInstanceIntFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceIntFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceIntFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceIntFuncOf_, self.ocobj)

    def testCUInt(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in UINT_NUMBERS[:-3]:
            self.assertEqual(self.obj.callInstanceUnsignedIntFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceUnsignedIntFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedIntFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedIntFuncOf_, self.ocobj)

    def testIUInt(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in UINT_NUMBERS[:-3]:
            self.assertEqual(self.obj.invokeInstanceUnsignedIntFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedIntFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedIntFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedIntFuncOf_, self.ocobj)

    def testCLong(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in LONG_NUMBERS[:-3]:
            self.assertEqual(self.obj.callInstanceLongFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceLongFuncOf_, self.ocobj)

    def testILong(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in LONG_NUMBERS[:-3]:
            self.assertEqual(self.obj.invokeInstanceLongFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceLongFuncOf_, self.ocobj)

    def testCULong(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in ULONG_NUMBERS[:-3]:
            self.assertEqual(self.obj.callInstanceUnsignedLongFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongFuncOf_, self.ocobj)

    def testIULong(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in ULONG_NUMBERS[:-3]:
            self.assertEqual(self.obj.invokeInstanceUnsignedLongFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongFuncOf_, self.ocobj)

    def testCLongLong(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in LONGLONG_NUMBERS[:-3]:
            self.assertEqual(self.obj.callInstanceLongLongFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceLongLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceLongLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceLongLongFuncOf_, self.ocobj)

    def testILongLong(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in LONGLONG_NUMBERS[:-3]:
            self.assertEqual(self.obj.invokeInstanceLongLongFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceLongLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceLongLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceLongLongFuncOf_, self.ocobj)

    def testCULongLong(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in ULONGLONG_NUMBERS[:-3]:
            self.assertEqual(self.obj.callInstanceUnsignedLongLongFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.callInstanceUnsignedLongLongFuncOf_, self.ocobj)

    def testIULongLong(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in ULONGLONG_NUMBERS[:-3]:
            self.assertEqual(self.obj.invokeInstanceUnsignedLongLongFuncOf_(self.ocobj), o)

        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongLongFuncOf_, self.ocobj)
        self.assertRaises(ValueError, self.obj.invokeInstanceUnsignedLongLongFuncOf_, self.ocobj)

    def testCFloat(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in FLOAT_NUMBERS:
            self.assertEqual(self.obj.callInstanceFloatFuncOf_(self.ocobj), o)

    def testIFloat(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in FLOAT_NUMBERS:
            self.assertEqual(self.obj.invokeInstanceFloatFuncOf_(self.ocobj), o)

    def testCDouble(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in DOUBLE_NUMBERS:
            self.assertEqual(self.obj.callInstanceDoubleFuncOf_(self.ocobj), o)

    def testIDouble(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in DOUBLE_NUMBERS:
            self.assertEqual(self.obj.invokeInstanceDoubleFuncOf_(self.ocobj), o)

    def testCId(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in OBJECTS:
            self.assertEqual(self.obj.callInstanceIdFuncOf_(self.ocobj), o)

    def testIId(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in OBJECTS:
            self.assertEqual(self.obj.invokeInstanceIdFuncOf_(self.ocobj), o)

    def testCId2(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in OBJECTS:
            self.assertEqual(self.obj.callInstanceIdFuncOf_(self.pyobj), o)

    def testIId2(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in OBJECTS:
            self.assertEqual(self.obj.invokeInstanceIdFuncOf_(self.pyobj), o)

    def testCStruct1(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in DUMMY_OBJECTS:
            self.assertEqual(self.obj.callInstanceDummyFuncOf_(self.ocobj), o)

    def testIStruct1(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in DUMMY_OBJECTS:
            self.assertEqual(self.obj.invokeInstanceDummyFuncOf_(self.ocobj), o)

    def testCStruct2(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in DUMMY2_OBJECTS:
            self.assertEqual(self.obj.callInstanceDummy2FuncOf_(self.ocobj), o)

    def testIStruct2(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in DUMMY2_OBJECTS:
            self.assertEqual(self.obj.invokeInstanceDummy2FuncOf_(self.ocobj), o)

    def testCNSPoint(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in POINTS:
            self.assertEqual(self.obj.callInstanceNSPointFuncOf_(self.ocobj), o)

    def testINSPoint(self):
        self.pyobj.reset()
        self.ocobj.reset()

        for o in POINTS:
            self.assertEqual(self.obj.invokeInstanceNSPointFuncOf_(self.ocobj), o)

class OCPyTestSimpleArguments(TestCase):
    #
    # Test argument passing of single basic values.
    #
    # TODO: Copy and adapt rest of OCPyTestSimpleArguments
    #

    def setUp(self):
        self.ocobj = MyOCClass.new()
        self.obj = OC_TestClass2.new()

    def testCLongLong(self):
        self.assertEqual(self.obj.callInstanceLongLongArg_on_(0, self.ocobj), 0)
        self.assertEqual(self.obj.callInstanceLongLongArg_on_(10, self.ocobj), 20)
        self.assertEqual(self.obj.callInstanceLongLongArg_on_(1 << 60, self.ocobj), 1 << 61)
        self.assertEqual(self.obj.callInstanceLongLongArg_on_(-10, self.ocobj), -20)
        self.assertEqual(self.obj.callInstanceLongLongArg_on_(-(1 << 60), self.ocobj), -(1 << 61))

    def testILongLong(self):
        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(0, self.ocobj), 0)
        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(10, self.ocobj), 20)
        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(1 << 60, self.ocobj), 1 << 61)
        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(-10, self.ocobj), -20)
        self.assertEqual(self.obj.invokeInstanceLongLongArg_on_(-(1 << 60), self.ocobj), -(1 << 61))

if __name__ == '__main__':
    main()