1 /* 2 * Copyright (C) 2011-2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.renderscript.cts; 18 19 import android.renderscript.Allocation; 20 21 import android.renderscript.Byte2; 22 import android.renderscript.Byte3; 23 import android.renderscript.Byte4; 24 25 import android.renderscript.Double2; 26 import android.renderscript.Double3; 27 import android.renderscript.Double4; 28 29 import android.renderscript.Element; 30 31 import android.renderscript.Float2; 32 import android.renderscript.Float3; 33 import android.renderscript.Float4; 34 35 import android.renderscript.Int2; 36 import android.renderscript.Int3; 37 import android.renderscript.Int4; 38 39 import android.renderscript.Long2; 40 import android.renderscript.Long3; 41 import android.renderscript.Long4; 42 43 import android.renderscript.RSRuntimeException; 44 45 import android.renderscript.Short2; 46 import android.renderscript.Short3; 47 import android.renderscript.Short4; 48 49 import android.renderscript.Type; 50 51 public class ComputeTest extends RSBaseCompute { 52 testJavaVectorTypes()53 public void testJavaVectorTypes() { 54 Byte2 b2 = new Byte2(); 55 b2.x = 1; 56 b2.y = 2; 57 b2 = new Byte2((byte)1, (byte)2); 58 assertTrue(b2.x == 1); 59 assertTrue(b2.y == 2); 60 Byte3 b3 = new Byte3(); 61 b3.x = 1; 62 b3.y = 2; 63 b3.z = 2; 64 b3 = new Byte3((byte)1, (byte)2, (byte)3); 65 assertTrue(b3.x == 1); 66 assertTrue(b3.y == 2); 67 assertTrue(b3.z == 3); 68 Byte4 b4 = new Byte4(); 69 b4.x = 1; 70 b4.y = 2; 71 b4.x = 3; 72 b4.w = 4; 73 b4 = new Byte4((byte)1, (byte)2, (byte)3, (byte)4); 74 assertTrue(b4.x == 1); 75 assertTrue(b4.y == 2); 76 assertTrue(b4.z == 3); 77 assertTrue(b4.w == 4); 78 79 Double2 d2 = new Double2(); 80 d2.x = 1.0; 81 d2.y = 2.0; 82 d2 = new Double2(1.0, 2.0); 83 assertTrue(d2.x == 1.0); 84 assertTrue(d2.y == 2.0); 85 Double3 d3 = new Double3(); 86 d3.x = 1.0; 87 d3.y = 2.0; 88 d3.z = 3.0; 89 d3 = new Double3(1.0, 2.0, 3.0); 90 assertTrue(d3.x == 1.0); 91 assertTrue(d3.y == 2.0); 92 assertTrue(d3.z == 3.0); 93 Double4 d4 = new Double4(); 94 d4.x = 1.0; 95 d4.y = 2.0; 96 d4.x = 3.0; 97 d4.w = 4.0; 98 d4 = new Double4(1.0, 2.0, 3.0, 4.0); 99 assertTrue(d4.x == 1.0); 100 assertTrue(d4.y == 2.0); 101 assertTrue(d4.z == 3.0); 102 assertTrue(d4.w == 4.0); 103 104 Float2 f2 = new Float2(); 105 f2.x = 1.0f; 106 f2.y = 2.0f; 107 f2 = new Float2(1.0f, 2.0f); 108 assertTrue(f2.x == 1.0f); 109 assertTrue(f2.y == 2.0f); 110 Float3 f3 = new Float3(); 111 f3.x = 1.0f; 112 f3.y = 2.0f; 113 f3.z = 3.0f; 114 f3 = new Float3(1.0f, 2.0f, 3.0f); 115 assertTrue(f3.x == 1.0f); 116 assertTrue(f3.y == 2.0f); 117 assertTrue(f3.z == 3.0f); 118 Float4 f4 = new Float4(); 119 f4.x = 1.0f; 120 f4.y = 2.0f; 121 f4.x = 3.0f; 122 f4.w = 4.0f; 123 f4 = new Float4(1.0f, 2.0f, 3.0f, 4.0f); 124 assertTrue(f4.x == 1.0f); 125 assertTrue(f4.y == 2.0f); 126 assertTrue(f4.z == 3.0f); 127 assertTrue(f4.w == 4.0f); 128 129 Int2 i2 = new Int2(); 130 i2.x = 1; 131 i2.y = 2; 132 i2 = new Int2(1, 2); 133 assertTrue(i2.x == 1); 134 assertTrue(i2.y == 2); 135 Int3 i3 = new Int3(); 136 i3.x = 1; 137 i3.y = 2; 138 i3.z = 3; 139 i3 = new Int3(1, 2, 3); 140 assertTrue(i3.x == 1); 141 assertTrue(i3.y == 2); 142 assertTrue(i3.z == 3); 143 Int4 i4 = new Int4(); 144 i4.x = 1; 145 i4.y = 2; 146 i4.x = 3; 147 i4.w = 4; 148 i4 = new Int4(1, 2, 3, 4); 149 assertTrue(i4.x == 1); 150 assertTrue(i4.y == 2); 151 assertTrue(i4.z == 3); 152 assertTrue(i4.w == 4); 153 154 Long2 l2 = new Long2(); 155 l2.x = 1; 156 l2.y = 2; 157 l2 = new Long2(1, 2); 158 assertTrue(l2.x == 1); 159 assertTrue(l2.y == 2); 160 Long3 l3 = new Long3(); 161 l3.x = 1; 162 l3.y = 2; 163 l3.z = 3; 164 l3 = new Long3(1, 2, 3); 165 assertTrue(l3.x == 1); 166 assertTrue(l3.y == 2); 167 assertTrue(l3.z == 3); 168 Long4 l4 = new Long4(); 169 l4.x = 1; 170 l4.y = 2; 171 l4.x = 3; 172 l4.w = 4; 173 l4 = new Long4(1, 2, 3, 4); 174 assertTrue(l4.x == 1); 175 assertTrue(l4.y == 2); 176 assertTrue(l4.z == 3); 177 assertTrue(l4.w == 4); 178 179 Short2 s2 = new Short2(); 180 s2.x = 1; 181 s2.y = 2; 182 s2 = new Short2((short)1, (short)2); 183 assertTrue(s2.x == 1); 184 assertTrue(s2.y == 2); 185 Short3 s3 = new Short3(); 186 s3.x = 1; 187 s3.y = 2; 188 s3.z = 3; 189 s3 = new Short3((short)1, (short)2, (short)3); 190 assertTrue(s3.x == 1); 191 assertTrue(s3.y == 2); 192 assertTrue(s3.z == 3); 193 Short4 s4 = new Short4(); 194 s4.x = 1; 195 s4.y = 2; 196 s4.x = 3; 197 s4.w = 4; 198 s4 = new Short4((short)1, (short)2, (short)3, (short)4); 199 assertTrue(s4.x == 1); 200 assertTrue(s4.y == 2); 201 assertTrue(s4.z == 3); 202 assertTrue(s4.w == 4); 203 } 204 initializeGlobals(ScriptC_primitives s)205 private boolean initializeGlobals(ScriptC_primitives s) { 206 float pF = s.get_floatTest(); 207 if (pF != 1.99f) { 208 return false; 209 } 210 s.set_floatTest(2.99f); 211 212 double pD = s.get_doubleTest(); 213 if (pD != 2.05) { 214 return false; 215 } 216 s.set_doubleTest(3.05); 217 218 byte pC = s.get_charTest(); 219 if (pC != -8) { 220 return false; 221 } 222 s.set_charTest((byte)-16); 223 224 short pS = s.get_shortTest(); 225 if (pS != -16) { 226 return false; 227 } 228 s.set_shortTest((short)-32); 229 230 int pI = s.get_intTest(); 231 if (pI != -32) { 232 return false; 233 } 234 s.set_intTest(-64); 235 236 long pL = s.get_longTest(); 237 if (pL != 17179869184l) { 238 return false; 239 } 240 s.set_longTest(17179869185l); 241 242 long puL = s.get_ulongTest(); 243 if (puL != 4611686018427387904L) { 244 return false; 245 } 246 s.set_ulongTest(4611686018427387903L); 247 248 long pLL = s.get_longlongTest(); 249 if (pLL != 68719476736L) { 250 return false; 251 } 252 s.set_longlongTest(68719476735L); 253 254 long pu64 = s.get_uint64_tTest(); 255 if (pu64 != 117179869184l) { 256 return false; 257 } 258 s.set_uint64_tTest(117179869185l); 259 260 ScriptField_AllVectorTypes avt; 261 avt = new ScriptField_AllVectorTypes(mRS, 1, 262 Allocation.USAGE_SCRIPT); 263 ScriptField_AllVectorTypes.Item avtItem; 264 avtItem = new ScriptField_AllVectorTypes.Item(); 265 avtItem.b2.x = 1; 266 avtItem.b2.y = 2; 267 avtItem.b3.x = 1; 268 avtItem.b3.y = 2; 269 avtItem.b3.z = 3; 270 avtItem.b4.x = 1; 271 avtItem.b4.y = 2; 272 avtItem.b4.z = 3; 273 avtItem.b4.w = 4; 274 275 avtItem.s2.x = 1; 276 avtItem.s2.y = 2; 277 avtItem.s3.x = 1; 278 avtItem.s3.y = 2; 279 avtItem.s3.z = 3; 280 avtItem.s4.x = 1; 281 avtItem.s4.y = 2; 282 avtItem.s4.z = 3; 283 avtItem.s4.w = 4; 284 285 avtItem.i2.x = 1; 286 avtItem.i2.y = 2; 287 avtItem.i3.x = 1; 288 avtItem.i3.y = 2; 289 avtItem.i3.z = 3; 290 avtItem.i4.x = 1; 291 avtItem.i4.y = 2; 292 avtItem.i4.z = 3; 293 avtItem.i4.w = 4; 294 295 avtItem.f2.x = 1.0f; 296 avtItem.f2.y = 2.0f; 297 avtItem.f3.x = 1.0f; 298 avtItem.f3.y = 2.0f; 299 avtItem.f3.z = 3.0f; 300 avtItem.f4.x = 1.0f; 301 avtItem.f4.y = 2.0f; 302 avtItem.f4.z = 3.0f; 303 avtItem.f4.w = 4.0f; 304 305 avt.set(avtItem, 0, true); 306 s.bind_avt(avt); 307 308 return true; 309 } 310 311 /** 312 * Test primitive types. 313 */ testPrimitives()314 public void testPrimitives() { 315 ScriptC_primitives t = new ScriptC_primitives(mRS); 316 317 assertTrue(initializeGlobals(t)); 318 t.invoke_test(); 319 waitForMessage(); 320 checkForErrors(); 321 } 322 checkInit(ScriptC_array_init s)323 private void checkInit(ScriptC_array_init s) { 324 float[] fa = s.get_fa(); 325 assertTrue(fa[0] == 1.0); 326 assertTrue(fa[1] == 9.9999f); 327 assertTrue(fa[2] == 0); 328 assertTrue(fa[3] == 0); 329 assertTrue(fa.length == 4); 330 331 double[] da = s.get_da(); 332 assertTrue(da[0] == 7.0); 333 assertTrue(da[1] == 8.88888); 334 assertTrue(da.length == 2); 335 336 byte[] ca = s.get_ca(); 337 assertTrue(ca[0] == 'a'); 338 assertTrue(ca[1] == 7); 339 assertTrue(ca[2] == 'b'); 340 assertTrue(ca[3] == 'c'); 341 assertTrue(ca.length == 4); 342 343 short[] sa = s.get_sa(); 344 assertTrue(sa[0] == 1); 345 assertTrue(sa[1] == 1); 346 assertTrue(sa[2] == 2); 347 assertTrue(sa[3] == 3); 348 assertTrue(sa.length == 4); 349 350 int[] ia = s.get_ia(); 351 assertTrue(ia[0] == 5); 352 assertTrue(ia[1] == 8); 353 assertTrue(ia[2] == 0); 354 assertTrue(ia[3] == 0); 355 assertTrue(ia.length == 4); 356 357 long[] la = s.get_la(); 358 assertTrue(la[0] == 13); 359 assertTrue(la[1] == 21); 360 assertTrue(la.length == 2); 361 362 long[] lla = s.get_lla(); 363 assertTrue(lla[0] == 34); 364 assertTrue(lla[1] == 0); 365 assertTrue(lla[2] == 0); 366 assertTrue(lla[3] == 0); 367 assertTrue(lla.length == 4); 368 369 boolean[] ba = s.get_ba(); 370 assertTrue(ba[0] == true); 371 assertTrue(ba[1] == false); 372 assertTrue(ba[2] == false); 373 assertTrue(ba.length == 3); 374 } 375 376 /** 377 * Test array initialization. 378 */ testArrayInit()379 public void testArrayInit() { 380 ScriptC_array_init t = new ScriptC_array_init(mRS); 381 382 checkInit(t); 383 t.invoke_array_init_test(); 384 Element[] e = new Element[2]; 385 e[0] = Element.FONT(mRS); 386 e[1] = Element.ELEMENT(mRS); 387 t.set_elemArr(e); 388 mRS.finish(); 389 waitForMessage(); 390 checkForErrors(); 391 } 392 393 initializeVector(ScriptC_vector s)394 private boolean initializeVector(ScriptC_vector s) { 395 Float2 F2 = s.get_f2(); 396 if (F2.x != 1.0f || F2.y != 2.0f) { 397 return false; 398 } 399 F2.x = 2.99f; 400 F2.y = 3.99f; 401 s.set_f2(F2); 402 403 Float3 F3 = s.get_f3(); 404 if (F3.x != 1.0f || F3.y != 2.0f || F3.z != 3.0f) { 405 return false; 406 } 407 F3.x = 2.99f; 408 F3.y = 3.99f; 409 F3.z = 4.99f; 410 s.set_f3(F3); 411 412 Float4 F4 = s.get_f4(); 413 if (F4.x != 1.0f || F4.y != 2.0f || F4.z != 3.0f || F4.w != 4.0f) { 414 return false; 415 } 416 F4.x = 2.99f; 417 F4.y = 3.99f; 418 F4.z = 4.99f; 419 F4.w = 5.99f; 420 s.set_f4(F4); 421 422 Double2 D2 = s.get_d2(); 423 if (D2.x != 1.0 || D2.y != 2.0) { 424 return false; 425 } 426 D2.x = 2.99; 427 D2.y = 3.99; 428 s.set_d2(D2); 429 430 Double3 D3 = s.get_d3(); 431 if (D3.x != 1.0 || D3.y != 2.0 || D3.z != 3.0) { 432 return false; 433 } 434 D3.x = 2.99; 435 D3.y = 3.99; 436 D3.z = 4.99; 437 s.set_d3(D3); 438 439 Double4 D4 = s.get_d4(); 440 if (D4.x != 1.0 || D4.y != 2.0 || D4.z != 3.0 || D4.w != 4.0) { 441 return false; 442 } 443 D4.x = 2.99; 444 D4.y = 3.99; 445 D4.z = 4.99; 446 D4.w = 5.99; 447 s.set_d4(D4); 448 449 Byte2 B2 = s.get_i8_2(); 450 if (B2.x != 1 || B2.y != 2) { 451 return false; 452 } 453 B2.x = 2; 454 B2.y = 3; 455 s.set_i8_2(B2); 456 457 Byte3 B3 = s.get_i8_3(); 458 if (B3.x != 1 || B3.y != 2 || B3.z != 3) { 459 return false; 460 } 461 B3.x = 2; 462 B3.y = 3; 463 B3.z = 4; 464 s.set_i8_3(B3); 465 466 Byte4 B4 = s.get_i8_4(); 467 if (B4.x != 1 || B4.y != 2 || B4.z != 3 || B4.w != 4) { 468 return false; 469 } 470 B4.x = 2; 471 B4.y = 3; 472 B4.z = 4; 473 B4.w = 5; 474 s.set_i8_4(B4); 475 476 Short2 S2 = s.get_u8_2(); 477 if (S2.x != 1 || S2.y != 2) { 478 return false; 479 } 480 S2.x = 2; 481 S2.y = 3; 482 s.set_u8_2(S2); 483 484 Short3 S3 = s.get_u8_3(); 485 if (S3.x != 1 || S3.y != 2 || S3.z != 3) { 486 return false; 487 } 488 S3.x = 2; 489 S3.y = 3; 490 S3.z = 4; 491 s.set_u8_3(S3); 492 493 Short4 S4 = s.get_u8_4(); 494 if (S4.x != 1 || S4.y != 2 || S4.z != 3 || S4.w != 4) { 495 return false; 496 } 497 S4.x = 2; 498 S4.y = 3; 499 S4.z = 4; 500 S4.w = 5; 501 s.set_u8_4(S4); 502 503 S2 = s.get_i16_2(); 504 if (S2.x != 1 || S2.y != 2) { 505 return false; 506 } 507 S2.x = 2; 508 S2.y = 3; 509 s.set_i16_2(S2); 510 511 S3 = s.get_i16_3(); 512 if (S3.x != 1 || S3.y != 2 || S3.z != 3) { 513 return false; 514 } 515 S3.x = 2; 516 S3.y = 3; 517 S3.z = 4; 518 s.set_i16_3(S3); 519 520 S4 = s.get_i16_4(); 521 if (S4.x != 1 || S4.y != 2 || S4.z != 3 || S4.w != 4) { 522 return false; 523 } 524 S4.x = 2; 525 S4.y = 3; 526 S4.z = 4; 527 S4.w = 5; 528 s.set_i16_4(S4); 529 530 Int2 I2 = s.get_u16_2(); 531 if (I2.x != 1 || I2.y != 2) { 532 return false; 533 } 534 I2.x = 2; 535 I2.y = 3; 536 s.set_u16_2(I2); 537 538 Int3 I3 = s.get_u16_3(); 539 if (I3.x != 1 || I3.y != 2 || I3.z != 3) { 540 return false; 541 } 542 I3.x = 2; 543 I3.y = 3; 544 I3.z = 4; 545 s.set_u16_3(I3); 546 547 Int4 I4 = s.get_u16_4(); 548 if (I4.x != 1 || I4.y != 2 || I4.z != 3 || I4.w != 4) { 549 return false; 550 } 551 I4.x = 2; 552 I4.y = 3; 553 I4.z = 4; 554 I4.w = 5; 555 s.set_u16_4(I4); 556 557 I2 = s.get_i32_2(); 558 if (I2.x != 1 || I2.y != 2) { 559 return false; 560 } 561 I2.x = 2; 562 I2.y = 3; 563 s.set_i32_2(I2); 564 565 I3 = s.get_i32_3(); 566 if (I3.x != 1 || I3.y != 2 || I3.z != 3) { 567 return false; 568 } 569 I3.x = 2; 570 I3.y = 3; 571 I3.z = 4; 572 s.set_i32_3(I3); 573 574 I4 = s.get_i32_4(); 575 if (I4.x != 1 || I4.y != 2 || I4.z != 3 || I4.w != 4) { 576 return false; 577 } 578 I4.x = 2; 579 I4.y = 3; 580 I4.z = 4; 581 I4.w = 5; 582 s.set_i32_4(I4); 583 584 Long2 L2 = s.get_u32_2(); 585 if (L2.x != 1 || L2.y != 2) { 586 return false; 587 } 588 L2.x = 2; 589 L2.y = 3; 590 s.set_u32_2(L2); 591 592 Long3 L3 = s.get_u32_3(); 593 if (L3.x != 1 || L3.y != 2 || L3.z != 3) { 594 return false; 595 } 596 L3.x = 2; 597 L3.y = 3; 598 L3.z = 4; 599 s.set_u32_3(L3); 600 601 Long4 L4 = s.get_u32_4(); 602 if (L4.x != 1 || L4.y != 2 || L4.z != 3 || L4.w != 4) { 603 return false; 604 } 605 L4.x = 2; 606 L4.y = 3; 607 L4.z = 4; 608 L4.w = 5; 609 s.set_u32_4(L4); 610 611 L2 = s.get_i64_2(); 612 if (L2.x != 1 || L2.y != 2) { 613 return false; 614 } 615 L2.x = 2; 616 L2.y = 3; 617 s.set_i64_2(L2); 618 619 L3 = s.get_i64_3(); 620 if (L3.x != 1 || L3.y != 2 || L3.z != 3) { 621 return false; 622 } 623 L3.x = 2; 624 L3.y = 3; 625 L3.z = 4; 626 s.set_i64_3(L3); 627 628 L4 = s.get_i64_4(); 629 if (L4.x != 1 || L4.y != 2 || L4.z != 3 || L4.w != 4) { 630 return false; 631 } 632 L4.x = 2; 633 L4.y = 3; 634 L4.z = 4; 635 L4.w = 5; 636 s.set_i64_4(L4); 637 638 L2 = s.get_u64_2(); 639 if (L2.x != 1 || L2.y != 2) { 640 return false; 641 } 642 L2.x = 2; 643 L2.y = 3; 644 s.set_u64_2(L2); 645 646 L3 = s.get_u64_3(); 647 if (L3.x != 1 || L3.y != 2 || L3.z != 3) { 648 return false; 649 } 650 L3.x = 2; 651 L3.y = 3; 652 L3.z = 4; 653 s.set_u64_3(L3); 654 655 L4 = s.get_u64_4(); 656 if (L4.x != 1 || L4.y != 2 || L4.z != 3 || L4.w != 4) { 657 return false; 658 } 659 L4.x = 2; 660 L4.y = 3; 661 L4.z = 4; 662 L4.w = 5; 663 s.set_u64_4(L4); 664 665 return true; 666 } 667 testVector()668 public void testVector() { 669 ScriptC_vector s = new ScriptC_vector(mRS); 670 if (!initializeVector(s)) { 671 fail("Failed to init vector components"); 672 } else { 673 s.invoke_vector_test(); 674 mRS.finish(); 675 waitForMessage(); 676 } 677 checkForErrors(); 678 } 679 testMatrix()680 public void testMatrix() { 681 ScriptC_MatrixTest s = new ScriptC_MatrixTest(mRS); 682 s.invoke_matrixTests(); 683 mRS.finish(); 684 waitForMessage(); 685 checkForErrors(); 686 } 687 initializeStructObject(ScriptC_struct_object s)688 private boolean initializeStructObject(ScriptC_struct_object s) { 689 ScriptField_objects_rs.Item i = new ScriptField_objects_rs.Item(); 690 i.e = Element.FONT(mRS); 691 i.i = 1; 692 s.set_myStruct(i); 693 return true; 694 } 695 testStructObject()696 public void testStructObject() { 697 ScriptC_struct_object s = 698 new ScriptC_struct_object(mRS); 699 if (!initializeStructObject(s)) { 700 fail("Failed to init structure with RS objects"); 701 } else { 702 s.invoke_struct_object_test(); 703 mRS.finish(); 704 waitForMessage(); 705 } 706 checkForErrors(); 707 } 708 709 /** 710 * Test utility functions. 711 */ testUtilityFunctions()712 public void testUtilityFunctions() { 713 ScriptC_utils t = new ScriptC_utils(mRS); 714 t.invoke_test(); 715 waitForMessage(); 716 checkForErrors(); 717 } 718 setUpAllocation(Allocation a, int val)719 void setUpAllocation(Allocation a, int val) { 720 Type t = a.getType(); 721 int x = t.getX(); 722 723 int[] arr = new int[x]; 724 for (int i = 0; i < x; i++) { 725 arr[i] = val; 726 } 727 a.copyFrom(arr); 728 } 729 checkAllocation(Allocation a, int val)730 void checkAllocation(Allocation a, int val) { 731 Type t = a.getType(); 732 int x = t.getX(); 733 734 int[] arr = new int[x]; 735 a.copyTo(arr); 736 for (int i = 0; i < x; i++) { 737 assertTrue(arr[i] == val); 738 } 739 } 740 741 /** 742 * Test support for reflected forEach() as well as validation of parameters. 743 */ testForEach()744 public void testForEach() { 745 ScriptC_negate s = new ScriptC_negate(mRS); 746 747 int x = 7; 748 Type t = new Type.Builder(mRS, Element.I32(mRS)).setX(x).create(); 749 Allocation in = Allocation.createTyped(mRS, t); 750 Allocation out = Allocation.createTyped(mRS, t); 751 752 int val = 5; 753 setUpAllocation(in, val); 754 s.forEach_root(in, out); 755 checkAllocation(out, -val); 756 757 Type badT = new Type.Builder(mRS, Element.I32(mRS)).setX(x-1).create(); 758 Allocation badOut = Allocation.createTyped(mRS, badT); 759 try { 760 s.forEach_root(in, badOut); 761 fail("should throw RSRuntimeException"); 762 } catch (RSRuntimeException e) { 763 } 764 checkForErrors(); 765 } 766 767 /** 768 * Test script instancing. 769 */ testInstance()770 public void testInstance() { 771 ScriptC_instance instance_1 = new ScriptC_instance(mRS); 772 ScriptC_instance instance_2 = new ScriptC_instance(mRS); 773 774 Type t = new Type.Builder(mRS, Element.I32(mRS)).setX(1).create(); 775 Allocation ai1 = Allocation.createTyped(mRS, t); 776 Allocation ai2 = Allocation.createTyped(mRS, t); 777 778 instance_1.set_i(1); 779 instance_2.set_i(2); 780 instance_1.set_ai(ai1); 781 instance_2.set_ai(ai2); 782 783 // We now check to ensure that the global is not being shared across 784 // our separate script instances. Our invoke here merely sets the 785 // instanced allocation with the instanced global variable's value. 786 // If globals are being shared (i.e. not instancing scripts), then 787 // both instanced allocations will have the same resulting value 788 // (depending on the order in which the invokes complete). 789 instance_1.invoke_instance_test(); 790 instance_2.invoke_instance_test(); 791 792 int i1[] = new int[1]; 793 int i2[] = new int[1]; 794 795 ai1.copyTo(i1); 796 ai2.copyTo(i2); 797 798 // 3-step check ensures that a fortunate race condition wouldn't let us 799 // pass accidentally. 800 assertEquals(2, i2[0]); 801 assertEquals(1, i1[0]); 802 assertEquals(2, i2[0]); 803 804 checkForErrors(); 805 } 806 } 807