1// 2// Copyright (c) ZeroC, Inc. All rights reserved. 3// 4 5import {Ice} from "ice"; 6import {Test} from "./generated"; 7import {TestHelper} from "../../../Common/TestHelper"; 8 9const test = TestHelper.test; 10 11export class Client extends TestHelper 12{ 13 allTests() 14 { 15 const communicator = this.communicator(); 16 const out = this.getWriter(); 17 18 class MyInterfaceI extends Ice.InterfaceByValue 19 { 20 constructor() 21 { 22 super(Test.MyInterface.ice_staticId()); 23 } 24 } 25 communicator.getValueFactoryManager().add(id => new MyInterfaceI(), Test.MyInterface.ice_staticId()); 26 27 let inS:Ice.InputStream = null; 28 let outS:Ice.OutputStream = null; 29 30 out.write("testing primitive types... "); 31 32 { 33 const data = new Uint8Array(0); 34 inS = new Ice.InputStream(communicator, data); 35 } 36 37 { 38 outS = new Ice.OutputStream(communicator); 39 outS.startEncapsulation(); 40 outS.writeBool(true); 41 outS.endEncapsulation(); 42 const data = outS.finished(); 43 44 inS = new Ice.InputStream(communicator, data); 45 inS.startEncapsulation(); 46 test(inS.readBool()); 47 inS.endEncapsulation(); 48 49 inS = new Ice.InputStream(communicator, data); 50 inS.startEncapsulation(); 51 test(inS.readBool()); 52 inS.endEncapsulation(); 53 } 54 55 { 56 const data = new Uint8Array(0); 57 inS = new Ice.InputStream(communicator, data); 58 try 59 { 60 inS.readBool(); 61 test(false); 62 } 63 catch(ex) 64 { 65 test(ex instanceof Ice.UnmarshalOutOfBoundsException); 66 } 67 } 68 69 { 70 outS = new Ice.OutputStream(communicator); 71 outS.writeBool(true); 72 const data = outS.finished(); 73 inS = new Ice.InputStream(communicator, data); 74 test(inS.readBool()); 75 } 76 77 { 78 outS = new Ice.OutputStream(communicator); 79 outS.writeByte(1); 80 const data = outS.finished(); 81 inS = new Ice.InputStream(communicator, data); 82 test(inS.readByte() == 1); 83 } 84 85 { 86 outS = new Ice.OutputStream(communicator); 87 outS.writeShort(2); 88 const data = outS.finished(); 89 inS = new Ice.InputStream(communicator, data); 90 test(inS.readShort() == 2); 91 } 92 93 { 94 outS = new Ice.OutputStream(communicator); 95 outS.writeInt(3); 96 const data = outS.finished(); 97 inS = new Ice.InputStream(communicator, data); 98 test(inS.readInt() == 3); 99 } 100 101 { 102 outS = new Ice.OutputStream(communicator); 103 outS.writeLong(new Ice.Long(4)); 104 const data = outS.finished(); 105 inS = new Ice.InputStream(communicator, data); 106 test(inS.readLong().toNumber() == 4); 107 } 108 109 { 110 outS = new Ice.OutputStream(communicator); 111 outS.writeFloat(5.0); 112 const data = outS.finished(); 113 inS = new Ice.InputStream(communicator, data); 114 test(inS.readFloat() == 5.0); 115 } 116 117 { 118 outS = new Ice.OutputStream(communicator); 119 outS.writeDouble(6.0); 120 const data = outS.finished(); 121 inS = new Ice.InputStream(communicator, data); 122 test(inS.readDouble() == 6.0); 123 } 124 125 { 126 outS = new Ice.OutputStream(communicator); 127 outS.writeString("hello world"); 128 const data = outS.finished(); 129 inS = new Ice.InputStream(communicator, data); 130 test(inS.readString() == "hello world"); 131 } 132 133 out.writeLine("ok"); 134 135 out.write("testing constructed types... "); 136 137 { 138 outS = new Ice.OutputStream(communicator); 139 outS.writeEnum(Test.MyEnum.enum3); 140 const data = outS.finished(); 141 inS = new Ice.InputStream(communicator, data); 142 const e:Test.MyEnum = inS.readEnum(Test.MyEnum); 143 test(e == Test.MyEnum.enum3); 144 } 145 146 { 147 outS = new Ice.OutputStream(communicator); 148 const s = new Test.SmallStruct(); 149 s.bo = true; 150 s.by = 1; 151 s.sh = 2; 152 s.i = 3; 153 s.l = new Ice.Long(4); 154 s.f = 5.0; 155 s.d = 6.0; 156 s.str = "7"; 157 s.e = Test.MyEnum.enum2; 158 s.p = Test.MyInterfacePrx.uncheckedCast(communicator.stringToProxy("test:default")); 159 Test.SmallStruct.write(outS, s); 160 const data = outS.finished(); 161 const s2 = Test.SmallStruct.read(new Ice.InputStream(communicator, data)); 162 test(s2.equals(s)); 163 } 164 165 { 166 outS = new Ice.OutputStream(communicator); 167 const o = new Test.OptionalClass(); 168 o.bo = true; 169 o.by = 5; 170 o.sh = 4; 171 o.i = 3; 172 outS.writeValue(o); 173 outS.writePendingValues(); 174 const data = outS.finished(); 175 inS = new Ice.InputStream(communicator, data); 176 let o2:Test.OptionalClass; 177 inS.readValue((obj:Test.OptionalClass) => 178 { 179 o2 = obj; 180 }, 181 Test.OptionalClass); 182 inS.readPendingValues(); 183 184 test(o2.bo == o.bo); 185 test(o2.by == o.by); 186 if(communicator.getProperties().getProperty("Ice.Default.EncodingVersion") == "1.0") 187 { 188 test(o2.sh === undefined); 189 test(o2.i === undefined); 190 } 191 else 192 { 193 test(o2.sh == o.sh); 194 test(o2.i == o.i); 195 } 196 } 197 198 { 199 outS = new Ice.OutputStream(communicator, Ice.Encoding_1_0); 200 const o = new Test.OptionalClass(); 201 o.bo = true; 202 o.by = 5; 203 o.sh = 4; 204 o.i = 3; 205 outS.writeValue(o); 206 outS.writePendingValues(); 207 const data = outS.finished(); 208 inS = new Ice.InputStream(communicator, Ice.Encoding_1_0, data); 209 let o2:Test.OptionalClass; 210 inS.readValue((obj:Test.OptionalClass) => 211 { 212 o2 = obj; 213 }, 214 Test.OptionalClass); 215 inS.readPendingValues(); 216 test(o2.bo == o.bo); 217 test(o2.by == o.by); 218 test(o2.sh === undefined); 219 test(o2.i === undefined); 220 } 221 222 { 223 const arr = [true, false, true, false]; 224 outS = new Ice.OutputStream(communicator); 225 Ice.BoolSeqHelper.write(outS, arr); 226 let data = outS.finished(); 227 inS = new Ice.InputStream(communicator, data); 228 const arr2 = Ice.BoolSeqHelper.read(inS); 229 test(Ice.ArrayUtil.equals(arr, arr2)); 230 231 const arrS = [arr, [], arr]; 232 outS = new Ice.OutputStream(communicator); 233 Test.BoolSSHelper.write(outS, arrS); 234 data = outS.finished(); 235 inS = new Ice.InputStream(communicator, data); 236 const arr2S = Test.BoolSSHelper.read(inS); 237 test(Ice.ArrayUtil.equals(arr2S, arrS)); 238 } 239 240 { 241 const arr = new Uint8Array([0x01, 0x11, 0x12, 0x22]); 242 outS = new Ice.OutputStream(communicator); 243 Ice.ByteSeqHelper.write(outS, arr); 244 let data = outS.finished(); 245 inS = new Ice.InputStream(communicator, data); 246 const arr2 = Ice.ByteSeqHelper.read(inS); 247 test(Ice.ArrayUtil.equals(arr2, arr)); 248 249 const arrS:Uint8Array[] = [arr, new Uint8Array(0), arr]; 250 outS = new Ice.OutputStream(communicator); 251 Test.ByteSSHelper.write(outS, arrS); 252 data = outS.finished(); 253 inS = new Ice.InputStream(communicator, data); 254 const arr2S = Test.ByteSSHelper.read(inS); 255 test(Ice.ArrayUtil.equals(arr2S, arrS)); 256 } 257 258 { 259 const arr = [0x01, 0x11, 0x12, 0x22]; 260 outS = new Ice.OutputStream(communicator); 261 Ice.ShortSeqHelper.write(outS, arr); 262 let data = outS.finished(); 263 inS = new Ice.InputStream(communicator, data); 264 const arr2 = Ice.ShortSeqHelper.read(inS); 265 test(Ice.ArrayUtil.equals(arr2, arr)); 266 267 const arrS = [arr, [], arr]; 268 outS = new Ice.OutputStream(communicator); 269 Test.ShortSSHelper.write(outS, arrS); 270 data = outS.finished(); 271 inS = new Ice.InputStream(communicator, data); 272 const arr2S = Test.ShortSSHelper.read(inS); 273 test(Ice.ArrayUtil.equals(arr2S, arrS)); 274 } 275 276 { 277 const arr = [0x01, 0x11, 0x12, 0x22]; 278 outS = new Ice.OutputStream(communicator); 279 Ice.IntSeqHelper.write(outS, arr); 280 let data = outS.finished(); 281 inS = new Ice.InputStream(communicator, data); 282 const arr2 = Ice.IntSeqHelper.read(inS); 283 test(Ice.ArrayUtil.equals(arr2, arr)); 284 285 const arrS = [arr, [], arr]; 286 outS = new Ice.OutputStream(communicator); 287 Test.IntSSHelper.write(outS, arrS); 288 data = outS.finished(); 289 inS = new Ice.InputStream(communicator, data); 290 const arr2S = Test.IntSSHelper.read(inS); 291 test(Ice.ArrayUtil.equals(arr2S, arrS)); 292 } 293 294 { 295 const arr = [new Ice.Long(0x01), new Ice.Long(0x11), new Ice.Long(0x12), new Ice.Long(0x22)]; 296 outS = new Ice.OutputStream(communicator); 297 Ice.LongSeqHelper.write(outS, arr); 298 let data = outS.finished(); 299 inS = new Ice.InputStream(communicator, data); 300 const arr2 = Ice.LongSeqHelper.read(inS); 301 test(Ice.ArrayUtil.equals(arr2, arr)); 302 303 const arrS = [arr, [], arr]; 304 outS = new Ice.OutputStream(communicator); 305 Test.LongSSHelper.write(outS, arrS); 306 data = outS.finished(); 307 inS = new Ice.InputStream(communicator, data); 308 const arr2S = Test.LongSSHelper.read(inS); 309 test(Ice.ArrayUtil.equals(arr2S, arrS)); 310 } 311 312 { 313 const arr = [1, 2, 3, 4]; 314 outS = new Ice.OutputStream(communicator); 315 Ice.FloatSeqHelper.write(outS, arr); 316 let data = outS.finished(); 317 inS = new Ice.InputStream(communicator, data); 318 const arr2 = Ice.FloatSeqHelper.read(inS); 319 test(Ice.ArrayUtil.equals(arr2, arr)); 320 321 const arrS = [arr, [], arr]; 322 outS = new Ice.OutputStream(communicator); 323 Test.FloatSSHelper.write(outS, arrS); 324 data = outS.finished(); 325 inS = new Ice.InputStream(communicator, data); 326 const arr2S = Test.FloatSSHelper.read(inS); 327 test(Ice.ArrayUtil.equals(arr2S, arrS)); 328 } 329 330 { 331 const arr = [1, 2, 3, 4]; 332 outS = new Ice.OutputStream(communicator); 333 Ice.DoubleSeqHelper.write(outS, arr); 334 let data = outS.finished(); 335 inS = new Ice.InputStream(communicator, data); 336 const arr2 = Ice.DoubleSeqHelper.read(inS); 337 test(Ice.ArrayUtil.equals(arr2, arr)); 338 339 const arrS = [arr, [], arr]; 340 outS = new Ice.OutputStream(communicator); 341 Test.DoubleSSHelper.write(outS, arrS); 342 data = outS.finished(); 343 inS = new Ice.InputStream(communicator, data); 344 const arr2S = Test.DoubleSSHelper.read(inS); 345 test(Ice.ArrayUtil.equals(arr2S, arrS)); 346 } 347 348 { 349 const arr = ["string1", "string2", "string3", "string4"]; 350 outS = new Ice.OutputStream(communicator); 351 Ice.StringSeqHelper.write(outS, arr); 352 let data = outS.finished(); 353 inS = new Ice.InputStream(communicator, data); 354 const arr2 = Ice.StringSeqHelper.read(inS); 355 test(Ice.ArrayUtil.equals(arr2, arr)); 356 357 const arrS = [arr, [], arr]; 358 outS = new Ice.OutputStream(communicator); 359 Test.StringSSHelper.write(outS, arrS); 360 data = outS.finished(); 361 inS = new Ice.InputStream(communicator, data); 362 const arr2S = Test.StringSSHelper.read(inS); 363 test(Ice.ArrayUtil.equals(arr2S, arrS)); 364 } 365 366 { 367 const arr = [Test.MyEnum.enum3, Test.MyEnum.enum2, Test.MyEnum.enum1, Test.MyEnum.enum2]; 368 outS = new Ice.OutputStream(communicator); 369 Test.MyEnumSHelper.write(outS, arr); 370 let data = outS.finished(); 371 inS = new Ice.InputStream(communicator, data); 372 const arr2 = Test.MyEnumSHelper.read(inS); 373 test(Ice.ArrayUtil.equals(arr2, arr)); 374 375 const arrS = [arr, [], arr]; 376 outS = new Ice.OutputStream(communicator); 377 Test.MyEnumSSHelper.write(outS, arrS); 378 data = outS.finished(); 379 inS = new Ice.InputStream(communicator, data); 380 const arr2S = Test.MyEnumSSHelper.read(inS); 381 test(Ice.ArrayUtil.equals(arr2S, arrS)); 382 } 383 384 const smallStructArray = []; 385 for(let i = 0; i < 3; ++i) 386 { 387 const s = new Test.SmallStruct(); 388 s.bo = true; 389 s.by = 1; 390 s.sh = 2; 391 s.i = 3; 392 s.l = new Ice.Long(4); 393 s.f = 5.0; 394 s.d = 6.0; 395 s.str = "7"; 396 s.e = Test.MyEnum.enum2; 397 s.p = Test.MyInterfacePrx.uncheckedCast(communicator.stringToProxy("test:default")); 398 smallStructArray[i] = s; 399 } 400 401 const myClassArray = []; 402 for(let i = 0; i < 4; ++i) 403 { 404 const c = new Test.MyClass(); 405 myClassArray[i] = c; 406 c.c = myClassArray[i]; 407 c.o = myClassArray[i]; 408 c.s = new Test.SmallStruct(); 409 c.s.e = Test.MyEnum.enum2; 410 c.seq1 = [true, false, true, false]; 411 c.seq2 = new Uint8Array([1, 2, 3, 4]); 412 c.seq3 = [1, 2, 3, 4]; 413 c.seq4 = [1, 2, 3, 4]; 414 c.seq5 = [new Ice.Long(1), new Ice.Long(2), new Ice.Long(3), new Ice.Long(4)]; 415 c.seq6 = [1, 2, 3, 4]; 416 c.seq7 = [1, 2, 3, 4]; 417 c.seq8 = ["string1", "string2", "string3", "string4"]; 418 c.seq9 = [Test.MyEnum.enum3, Test.MyEnum.enum2, Test.MyEnum.enum1]; 419 c.seq10 = [null, null, null, null]; // null elements. 420 c.d = new Test.StringMyClassD(); 421 c.d.set("hi", myClassArray[i]); 422 } 423 424 const myInterfaceArray = []; 425 for(let i = 0; i < 4; ++i) 426 { 427 myInterfaceArray[i] = new Ice.InterfaceByValue("::Test::MyInterface"); 428 } 429 430 { 431 outS = new Ice.OutputStream(communicator); 432 Test.MyClassSHelper.write(outS, myClassArray); 433 outS.writePendingValues(); 434 let data = outS.finished(); 435 inS = new Ice.InputStream(communicator, data); 436 const arr2 = Test.MyClassSHelper.read(inS); 437 inS.readPendingValues(); 438 test(arr2.length == myClassArray.length); 439 for(let i = 0; i < arr2.length; ++i) 440 { 441 test(arr2[i] !== null); 442 test(arr2[i].c == arr2[i]); 443 test(arr2[i].o == arr2[i]); 444 test(arr2[i].s.e == Test.MyEnum.enum2); 445 test(Ice.ArrayUtil.equals(arr2[i].seq1, myClassArray[i].seq1)); 446 test(Ice.ArrayUtil.equals(arr2[i].seq2, myClassArray[i].seq2)); 447 test(Ice.ArrayUtil.equals(arr2[i].seq3, myClassArray[i].seq3)); 448 test(Ice.ArrayUtil.equals(arr2[i].seq4, myClassArray[i].seq4)); 449 test(Ice.ArrayUtil.equals(arr2[i].seq5, myClassArray[i].seq5)); 450 test(Ice.ArrayUtil.equals(arr2[i].seq6, myClassArray[i].seq6)); 451 test(Ice.ArrayUtil.equals(arr2[i].seq7, myClassArray[i].seq7)); 452 test(Ice.ArrayUtil.equals(arr2[i].seq8, myClassArray[i].seq8)); 453 test(Ice.ArrayUtil.equals(arr2[i].seq9, myClassArray[i].seq9)); 454 test(arr2[i].d.get("hi") == arr2[i]); 455 } 456 const arrS = [myClassArray, [], myClassArray]; 457 outS = new Ice.OutputStream(communicator); 458 Test.MyClassSSHelper.write(outS, arrS); 459 data = outS.finished(); 460 inS = new Ice.InputStream(communicator, data); 461 const arr2S = Test.MyClassSSHelper.read(inS); 462 test(arr2S.length == arrS.length); 463 test(arr2S[0].length == arrS[0].length); 464 test(arr2S[1].length == arrS[1].length); 465 test(arr2S[2].length == arrS[2].length); 466 } 467 468 { 469 outS = new Ice.OutputStream(communicator); 470 Test.MyInterfaceSHelper.write(outS, myInterfaceArray); 471 outS.writePendingValues(); 472 let data = outS.finished(); 473 inS = new Ice.InputStream(communicator, data); 474 const arr2 = Test.MyInterfaceSHelper.read(inS); 475 inS.readPendingValues(); 476 test(arr2.length == myInterfaceArray.length); 477 const arrS = [myInterfaceArray, [], myInterfaceArray]; 478 outS = new Ice.OutputStream(communicator); 479 Test.MyInterfaceSSHelper.write(outS, arrS); 480 data = outS.finished(); 481 inS = new Ice.InputStream(communicator, data); 482 const arr2S = Test.MyInterfaceSSHelper.read(inS); 483 test(arr2S.length == arrS.length); 484 test(arr2S[0].length == arrS[0].length); 485 test(arr2S[1].length == arrS[1].length); 486 test(arr2S[2].length == arrS[2].length); 487 } 488 489 { 490 outS = new Ice.OutputStream(communicator); 491 const ex = new Test.MyException(); 492 493 const c = new Test.MyClass(); 494 c.c = c; 495 c.o = c; 496 c.s = new Test.SmallStruct(); 497 c.s.e = Test.MyEnum.enum2; 498 c.seq1 = [true, false, true, false]; 499 c.seq2 = new Uint8Array([1, 2, 3, 4]); 500 c.seq3 = [1, 2, 3, 4]; 501 c.seq4 = [1, 2, 3, 4]; 502 c.seq5 = [new Ice.Long(1), new Ice.Long(2), new Ice.Long(3), new Ice.Long(4)]; 503 c.seq6 = [1, 2, 3, 4]; 504 c.seq7 = [1, 2, 3, 4]; 505 c.seq8 = ["string1", "string2", "string3", "string4"]; 506 c.seq9 = [Test.MyEnum.enum3, Test.MyEnum.enum2, Test.MyEnum.enum1]; 507 c.seq10 = [null, null, null, null]; // null elements. 508 c.d = new Test.StringMyClassD(); 509 c.d.set("hi", c); 510 511 ex.c = c; 512 513 outS.writeException(ex); 514 const data = outS.finished(); 515 516 inS = new Ice.InputStream(communicator, data); 517 try 518 { 519 inS.throwException(); 520 test(false); 521 } 522 catch(ex1) 523 { 524 test(ex1 instanceof Test.MyException); 525 test(ex1.c.s.e == c.s.e); 526 test(Ice.ArrayUtil.equals(ex1.c.seq1, c.seq1)); 527 test(Ice.ArrayUtil.equals(ex1.c.seq2, c.seq2)); 528 test(Ice.ArrayUtil.equals(ex1.c.seq3, c.seq3)); 529 test(Ice.ArrayUtil.equals(ex1.c.seq4, c.seq4)); 530 test(Ice.ArrayUtil.equals(ex1.c.seq5, c.seq5)); 531 test(Ice.ArrayUtil.equals(ex1.c.seq6, c.seq6)); 532 test(Ice.ArrayUtil.equals(ex1.c.seq7, c.seq7)); 533 test(Ice.ArrayUtil.equals(ex1.c.seq8, c.seq8)); 534 test(Ice.ArrayUtil.equals(ex1.c.seq9, c.seq9)); 535 } 536 } 537 538 { 539 const dict = new Test.ByteBoolD(); 540 dict.set(4, true); 541 dict.set(1, false); 542 outS = new Ice.OutputStream(communicator); 543 Test.ByteBoolDHelper.write(outS, dict); 544 const data = outS.finished(); 545 inS = new Ice.InputStream(communicator, data); 546 const dict2 = Test.ByteBoolDHelper.read(inS); 547 test(Ice.MapUtil.equals(dict2, dict)); 548 } 549 550 { 551 const dict = new Test.ShortIntD(); 552 dict.set(1, 9); 553 dict.set(4, 8); 554 outS = new Ice.OutputStream(communicator); 555 Test.ShortIntDHelper.write(outS, dict); 556 const data = outS.finished(); 557 inS = new Ice.InputStream(communicator, data); 558 const dict2 = Test.ShortIntDHelper.read(inS); 559 test(Ice.MapUtil.equals(dict2, dict)); 560 } 561 562 { 563 const dict = new Test.LongFloatD(); 564 dict.set(new Ice.Long(123809828), 0.5); 565 dict.set(new Ice.Long(123809829), 0.6); 566 outS = new Ice.OutputStream(communicator); 567 Test.LongFloatDHelper.write(outS, dict); 568 const data = outS.finished(); 569 inS = new Ice.InputStream(communicator, data); 570 const dict2 = Test.LongFloatDHelper.read(inS); 571 test(dict2.size == 2); 572 test(dict2.get(new Ice.Long(123809828)) == 0.5); 573 test(Math.abs(dict2.get(new Ice.Long(123809829)) - 0.6) <= 0.001); 574 } 575 576 { 577 const dict = new Test.StringStringD(); 578 dict.set("key1", "value1"); 579 dict.set("key2", "value2"); 580 outS = new Ice.OutputStream(communicator); 581 Test.StringStringDHelper.write(outS, dict); 582 const data = outS.finished(); 583 inS = new Ice.InputStream(communicator, data); 584 const dict2 = Test.StringStringDHelper.read(inS); 585 test(Ice.MapUtil.equals(dict2, dict)); 586 } 587 588 { 589 const dict = new Test.StringMyClassD(); 590 let c = new Test.MyClass(); 591 c.s = new Test.SmallStruct(); 592 c.s.e = Test.MyEnum.enum2; 593 dict.set("key1", c); 594 c = new Test.MyClass(); 595 c.s = new Test.SmallStruct(); 596 c.s.e = Test.MyEnum.enum3; 597 dict.set("key2", c); 598 outS = new Ice.OutputStream(communicator); 599 Test.StringMyClassDHelper.write(outS, dict); 600 outS.writePendingValues(); 601 const data = outS.finished(); 602 inS = new Ice.InputStream(communicator, data); 603 const dict2 = Test.StringMyClassDHelper.read(inS); 604 inS.readPendingValues(); 605 test(dict2.size == dict.size); 606 test(dict2.get("key1").s.e == Test.MyEnum.enum2); 607 test(dict2.get("key2").s.e == Test.MyEnum.enum3); 608 } 609 out.writeLine("ok"); 610 } 611 612 async run(args:string[]) 613 { 614 let communicator:Ice.Communicator; 615 try 616 { 617 [communicator] = this.initialize(args); 618 this.allTests(); 619 } 620 finally 621 { 622 if(communicator) 623 { 624 await communicator.destroy(); 625 } 626 } 627 } 628} 629