warp-lang 1.0.2__py3-none-manylinux2014_x86_64.whl → 1.2.0__py3-none-manylinux2014_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of warp-lang might be problematic. Click here for more details.

Files changed (356) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.so +0 -0
  4. warp/bin/warp.so +0 -0
  5. warp/build.py +88 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3693 -3354
  8. warp/codegen.py +2925 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +49 -45
  11. warp/context.py +5409 -5102
  12. warp/dlpack.py +442 -442
  13. warp/examples/__init__.py +16 -16
  14. warp/examples/assets/bear.usd +0 -0
  15. warp/examples/assets/bunny.usd +0 -0
  16. warp/examples/assets/cartpole.urdf +110 -110
  17. warp/examples/assets/crazyflie.usd +0 -0
  18. warp/examples/assets/cube.usd +0 -0
  19. warp/examples/assets/nv_ant.xml +92 -92
  20. warp/examples/assets/nv_humanoid.xml +183 -183
  21. warp/examples/assets/quadruped.urdf +267 -267
  22. warp/examples/assets/rocks.nvdb +0 -0
  23. warp/examples/assets/rocks.usd +0 -0
  24. warp/examples/assets/sphere.usd +0 -0
  25. warp/examples/benchmarks/benchmark_api.py +381 -383
  26. warp/examples/benchmarks/benchmark_cloth.py +278 -277
  27. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
  28. warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
  29. warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
  30. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
  31. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
  32. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
  33. warp/examples/benchmarks/benchmark_cloth_warp.py +145 -146
  34. warp/examples/benchmarks/benchmark_launches.py +293 -295
  35. warp/examples/browse.py +29 -29
  36. warp/examples/core/example_dem.py +232 -219
  37. warp/examples/core/example_fluid.py +291 -267
  38. warp/examples/core/example_graph_capture.py +142 -126
  39. warp/examples/core/example_marching_cubes.py +186 -174
  40. warp/examples/core/example_mesh.py +172 -155
  41. warp/examples/core/example_mesh_intersect.py +203 -193
  42. warp/examples/core/example_nvdb.py +174 -170
  43. warp/examples/core/example_raycast.py +103 -90
  44. warp/examples/core/example_raymarch.py +197 -178
  45. warp/examples/core/example_render_opengl.py +183 -141
  46. warp/examples/core/example_sph.py +403 -387
  47. warp/examples/core/example_torch.py +219 -181
  48. warp/examples/core/example_wave.py +261 -248
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +432 -389
  51. warp/examples/fem/example_burgers.py +262 -0
  52. warp/examples/fem/example_convection_diffusion.py +180 -168
  53. warp/examples/fem/example_convection_diffusion_dg.py +217 -209
  54. warp/examples/fem/example_deformed_geometry.py +175 -159
  55. warp/examples/fem/example_diffusion.py +199 -173
  56. warp/examples/fem/example_diffusion_3d.py +178 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +219 -214
  58. warp/examples/fem/example_mixed_elasticity.py +242 -222
  59. warp/examples/fem/example_navier_stokes.py +257 -243
  60. warp/examples/fem/example_stokes.py +218 -192
  61. warp/examples/fem/example_stokes_transfer.py +263 -249
  62. warp/examples/fem/mesh_utils.py +133 -109
  63. warp/examples/fem/plot_utils.py +292 -287
  64. warp/examples/optim/example_bounce.py +258 -246
  65. warp/examples/optim/example_cloth_throw.py +220 -209
  66. warp/examples/optim/example_diffray.py +564 -536
  67. warp/examples/optim/example_drone.py +862 -835
  68. warp/examples/optim/example_inverse_kinematics.py +174 -168
  69. warp/examples/optim/example_inverse_kinematics_torch.py +183 -169
  70. warp/examples/optim/example_spring_cage.py +237 -231
  71. warp/examples/optim/example_trajectory.py +221 -199
  72. warp/examples/optim/example_walker.py +304 -293
  73. warp/examples/sim/example_cartpole.py +137 -129
  74. warp/examples/sim/example_cloth.py +194 -186
  75. warp/examples/sim/example_granular.py +122 -111
  76. warp/examples/sim/example_granular_collision_sdf.py +195 -186
  77. warp/examples/sim/example_jacobian_ik.py +234 -214
  78. warp/examples/sim/example_particle_chain.py +116 -105
  79. warp/examples/sim/example_quadruped.py +191 -180
  80. warp/examples/sim/example_rigid_chain.py +195 -187
  81. warp/examples/sim/example_rigid_contact.py +187 -177
  82. warp/examples/sim/example_rigid_force.py +125 -125
  83. warp/examples/sim/example_rigid_gyroscopic.py +107 -95
  84. warp/examples/sim/example_rigid_soft_contact.py +132 -122
  85. warp/examples/sim/example_soft_body.py +188 -177
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +61 -27
  88. warp/fem/cache.py +403 -388
  89. warp/fem/dirichlet.py +178 -179
  90. warp/fem/domain.py +262 -263
  91. warp/fem/field/__init__.py +100 -101
  92. warp/fem/field/field.py +148 -149
  93. warp/fem/field/nodal_field.py +298 -299
  94. warp/fem/field/restriction.py +22 -21
  95. warp/fem/field/test.py +180 -181
  96. warp/fem/field/trial.py +183 -183
  97. warp/fem/geometry/__init__.py +16 -19
  98. warp/fem/geometry/closest_point.py +69 -70
  99. warp/fem/geometry/deformed_geometry.py +270 -271
  100. warp/fem/geometry/element.py +748 -744
  101. warp/fem/geometry/geometry.py +184 -186
  102. warp/fem/geometry/grid_2d.py +380 -373
  103. warp/fem/geometry/grid_3d.py +437 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/nanogrid.py +455 -0
  106. warp/fem/geometry/partition.py +374 -376
  107. warp/fem/geometry/quadmesh_2d.py +532 -532
  108. warp/fem/geometry/tetmesh.py +840 -840
  109. warp/fem/geometry/trimesh_2d.py +577 -577
  110. warp/fem/integrate.py +1684 -1615
  111. warp/fem/operator.py +190 -191
  112. warp/fem/polynomial.py +214 -213
  113. warp/fem/quadrature/__init__.py +2 -2
  114. warp/fem/quadrature/pic_quadrature.py +243 -245
  115. warp/fem/quadrature/quadrature.py +295 -294
  116. warp/fem/space/__init__.py +179 -292
  117. warp/fem/space/basis_space.py +522 -489
  118. warp/fem/space/collocated_function_space.py +100 -105
  119. warp/fem/space/dof_mapper.py +236 -236
  120. warp/fem/space/function_space.py +148 -145
  121. warp/fem/space/grid_2d_function_space.py +148 -267
  122. warp/fem/space/grid_3d_function_space.py +167 -306
  123. warp/fem/space/hexmesh_function_space.py +253 -352
  124. warp/fem/space/nanogrid_function_space.py +202 -0
  125. warp/fem/space/partition.py +350 -350
  126. warp/fem/space/quadmesh_2d_function_space.py +261 -369
  127. warp/fem/space/restriction.py +161 -160
  128. warp/fem/space/shape/__init__.py +90 -15
  129. warp/fem/space/shape/cube_shape_function.py +728 -738
  130. warp/fem/space/shape/shape_function.py +102 -103
  131. warp/fem/space/shape/square_shape_function.py +611 -611
  132. warp/fem/space/shape/tet_shape_function.py +565 -567
  133. warp/fem/space/shape/triangle_shape_function.py +429 -429
  134. warp/fem/space/tetmesh_function_space.py +224 -292
  135. warp/fem/space/topology.py +297 -295
  136. warp/fem/space/trimesh_2d_function_space.py +153 -221
  137. warp/fem/types.py +77 -77
  138. warp/fem/utils.py +495 -495
  139. warp/jax.py +166 -141
  140. warp/jax_experimental.py +341 -339
  141. warp/native/array.h +1081 -1025
  142. warp/native/builtin.h +1603 -1560
  143. warp/native/bvh.cpp +402 -398
  144. warp/native/bvh.cu +533 -525
  145. warp/native/bvh.h +430 -429
  146. warp/native/clang/clang.cpp +496 -464
  147. warp/native/crt.cpp +42 -32
  148. warp/native/crt.h +352 -335
  149. warp/native/cuda_crt.h +1049 -1049
  150. warp/native/cuda_util.cpp +549 -540
  151. warp/native/cuda_util.h +288 -203
  152. warp/native/cutlass_gemm.cpp +34 -34
  153. warp/native/cutlass_gemm.cu +372 -372
  154. warp/native/error.cpp +66 -66
  155. warp/native/error.h +27 -27
  156. warp/native/exports.h +187 -0
  157. warp/native/fabric.h +228 -228
  158. warp/native/hashgrid.cpp +301 -278
  159. warp/native/hashgrid.cu +78 -77
  160. warp/native/hashgrid.h +227 -227
  161. warp/native/initializer_array.h +32 -32
  162. warp/native/intersect.h +1204 -1204
  163. warp/native/intersect_adj.h +365 -365
  164. warp/native/intersect_tri.h +322 -322
  165. warp/native/marching.cpp +2 -2
  166. warp/native/marching.cu +497 -497
  167. warp/native/marching.h +2 -2
  168. warp/native/mat.h +1545 -1498
  169. warp/native/matnn.h +333 -333
  170. warp/native/mesh.cpp +203 -203
  171. warp/native/mesh.cu +292 -293
  172. warp/native/mesh.h +1887 -1887
  173. warp/native/nanovdb/GridHandle.h +366 -0
  174. warp/native/nanovdb/HostBuffer.h +590 -0
  175. warp/native/nanovdb/NanoVDB.h +6624 -4782
  176. warp/native/nanovdb/PNanoVDB.h +3390 -2553
  177. warp/native/noise.h +850 -850
  178. warp/native/quat.h +1112 -1085
  179. warp/native/rand.h +303 -299
  180. warp/native/range.h +108 -108
  181. warp/native/reduce.cpp +156 -156
  182. warp/native/reduce.cu +348 -348
  183. warp/native/runlength_encode.cpp +61 -61
  184. warp/native/runlength_encode.cu +46 -46
  185. warp/native/scan.cpp +30 -30
  186. warp/native/scan.cu +36 -36
  187. warp/native/scan.h +7 -7
  188. warp/native/solid_angle.h +442 -442
  189. warp/native/sort.cpp +94 -94
  190. warp/native/sort.cu +97 -97
  191. warp/native/sort.h +14 -14
  192. warp/native/sparse.cpp +337 -337
  193. warp/native/sparse.cu +544 -544
  194. warp/native/spatial.h +630 -630
  195. warp/native/svd.h +562 -562
  196. warp/native/temp_buffer.h +30 -30
  197. warp/native/vec.h +1177 -1133
  198. warp/native/volume.cpp +529 -297
  199. warp/native/volume.cu +58 -32
  200. warp/native/volume.h +960 -538
  201. warp/native/volume_builder.cu +446 -425
  202. warp/native/volume_builder.h +34 -19
  203. warp/native/volume_impl.h +61 -0
  204. warp/native/warp.cpp +1057 -1052
  205. warp/native/warp.cu +2949 -2828
  206. warp/native/warp.h +321 -305
  207. warp/optim/__init__.py +9 -9
  208. warp/optim/adam.py +120 -120
  209. warp/optim/linear.py +1104 -939
  210. warp/optim/sgd.py +104 -92
  211. warp/render/__init__.py +10 -10
  212. warp/render/render_opengl.py +3356 -3204
  213. warp/render/render_usd.py +768 -749
  214. warp/render/utils.py +152 -150
  215. warp/sim/__init__.py +52 -59
  216. warp/sim/articulation.py +685 -685
  217. warp/sim/collide.py +1594 -1590
  218. warp/sim/import_mjcf.py +489 -481
  219. warp/sim/import_snu.py +220 -221
  220. warp/sim/import_urdf.py +536 -516
  221. warp/sim/import_usd.py +887 -881
  222. warp/sim/inertia.py +316 -317
  223. warp/sim/integrator.py +234 -233
  224. warp/sim/integrator_euler.py +1956 -1956
  225. warp/sim/integrator_featherstone.py +1917 -1991
  226. warp/sim/integrator_xpbd.py +3288 -3312
  227. warp/sim/model.py +4473 -4314
  228. warp/sim/particles.py +113 -112
  229. warp/sim/render.py +417 -403
  230. warp/sim/utils.py +413 -410
  231. warp/sparse.py +1289 -1227
  232. warp/stubs.py +2192 -2469
  233. warp/tape.py +1162 -225
  234. warp/tests/__init__.py +1 -1
  235. warp/tests/__main__.py +4 -4
  236. warp/tests/assets/test_index_grid.nvdb +0 -0
  237. warp/tests/assets/torus.usda +105 -105
  238. warp/tests/aux_test_class_kernel.py +26 -26
  239. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  240. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  241. warp/tests/aux_test_dependent.py +20 -22
  242. warp/tests/aux_test_grad_customs.py +21 -23
  243. warp/tests/aux_test_reference.py +9 -11
  244. warp/tests/aux_test_reference_reference.py +8 -10
  245. warp/tests/aux_test_square.py +15 -17
  246. warp/tests/aux_test_unresolved_func.py +14 -14
  247. warp/tests/aux_test_unresolved_symbol.py +14 -14
  248. warp/tests/disabled_kinematics.py +237 -239
  249. warp/tests/run_coverage_serial.py +31 -31
  250. warp/tests/test_adam.py +155 -157
  251. warp/tests/test_arithmetic.py +1088 -1124
  252. warp/tests/test_array.py +2415 -2326
  253. warp/tests/test_array_reduce.py +148 -150
  254. warp/tests/test_async.py +666 -656
  255. warp/tests/test_atomic.py +139 -141
  256. warp/tests/test_bool.py +212 -149
  257. warp/tests/test_builtins_resolution.py +1290 -1292
  258. warp/tests/test_bvh.py +162 -171
  259. warp/tests/test_closest_point_edge_edge.py +227 -228
  260. warp/tests/test_codegen.py +562 -553
  261. warp/tests/test_compile_consts.py +217 -101
  262. warp/tests/test_conditional.py +244 -246
  263. warp/tests/test_copy.py +230 -215
  264. warp/tests/test_ctypes.py +630 -632
  265. warp/tests/test_dense.py +65 -67
  266. warp/tests/test_devices.py +89 -98
  267. warp/tests/test_dlpack.py +528 -529
  268. warp/tests/test_examples.py +403 -378
  269. warp/tests/test_fabricarray.py +952 -955
  270. warp/tests/test_fast_math.py +60 -54
  271. warp/tests/test_fem.py +1298 -1278
  272. warp/tests/test_fp16.py +128 -130
  273. warp/tests/test_func.py +336 -337
  274. warp/tests/test_generics.py +596 -571
  275. warp/tests/test_grad.py +885 -640
  276. warp/tests/test_grad_customs.py +331 -336
  277. warp/tests/test_hash_grid.py +208 -164
  278. warp/tests/test_import.py +37 -39
  279. warp/tests/test_indexedarray.py +1132 -1134
  280. warp/tests/test_intersect.py +65 -67
  281. warp/tests/test_jax.py +305 -307
  282. warp/tests/test_large.py +169 -164
  283. warp/tests/test_launch.py +352 -354
  284. warp/tests/test_lerp.py +217 -261
  285. warp/tests/test_linear_solvers.py +189 -171
  286. warp/tests/test_lvalue.py +419 -493
  287. warp/tests/test_marching_cubes.py +63 -65
  288. warp/tests/test_mat.py +1799 -1827
  289. warp/tests/test_mat_lite.py +113 -115
  290. warp/tests/test_mat_scalar_ops.py +2905 -2889
  291. warp/tests/test_math.py +124 -193
  292. warp/tests/test_matmul.py +498 -499
  293. warp/tests/test_matmul_lite.py +408 -410
  294. warp/tests/test_mempool.py +186 -190
  295. warp/tests/test_mesh.py +281 -324
  296. warp/tests/test_mesh_query_aabb.py +226 -241
  297. warp/tests/test_mesh_query_point.py +690 -702
  298. warp/tests/test_mesh_query_ray.py +290 -303
  299. warp/tests/test_mlp.py +274 -276
  300. warp/tests/test_model.py +108 -110
  301. warp/tests/test_module_hashing.py +111 -0
  302. warp/tests/test_modules_lite.py +36 -39
  303. warp/tests/test_multigpu.py +161 -163
  304. warp/tests/test_noise.py +244 -248
  305. warp/tests/test_operators.py +248 -250
  306. warp/tests/test_options.py +121 -125
  307. warp/tests/test_peer.py +131 -137
  308. warp/tests/test_pinned.py +76 -78
  309. warp/tests/test_print.py +52 -54
  310. warp/tests/test_quat.py +2084 -2086
  311. warp/tests/test_rand.py +324 -288
  312. warp/tests/test_reload.py +207 -217
  313. warp/tests/test_rounding.py +177 -179
  314. warp/tests/test_runlength_encode.py +188 -190
  315. warp/tests/test_sim_grad.py +241 -0
  316. warp/tests/test_sim_kinematics.py +89 -97
  317. warp/tests/test_smoothstep.py +166 -168
  318. warp/tests/test_snippet.py +303 -266
  319. warp/tests/test_sparse.py +466 -460
  320. warp/tests/test_spatial.py +2146 -2148
  321. warp/tests/test_special_values.py +362 -0
  322. warp/tests/test_streams.py +484 -473
  323. warp/tests/test_struct.py +708 -675
  324. warp/tests/test_tape.py +171 -148
  325. warp/tests/test_torch.py +741 -743
  326. warp/tests/test_transient_module.py +85 -87
  327. warp/tests/test_types.py +554 -659
  328. warp/tests/test_utils.py +488 -499
  329. warp/tests/test_vec.py +1262 -1268
  330. warp/tests/test_vec_lite.py +71 -73
  331. warp/tests/test_vec_scalar_ops.py +2097 -2099
  332. warp/tests/test_verify_fp.py +92 -94
  333. warp/tests/test_volume.py +961 -736
  334. warp/tests/test_volume_write.py +338 -265
  335. warp/tests/unittest_serial.py +38 -37
  336. warp/tests/unittest_suites.py +367 -359
  337. warp/tests/unittest_utils.py +434 -578
  338. warp/tests/unused_test_misc.py +69 -71
  339. warp/tests/walkthrough_debug.py +85 -85
  340. warp/thirdparty/appdirs.py +598 -598
  341. warp/thirdparty/dlpack.py +143 -143
  342. warp/thirdparty/unittest_parallel.py +563 -561
  343. warp/torch.py +321 -295
  344. warp/types.py +4941 -4450
  345. warp/utils.py +1008 -821
  346. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/LICENSE.md +126 -126
  347. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/METADATA +365 -400
  348. warp_lang-1.2.0.dist-info/RECORD +359 -0
  349. warp/examples/assets/cube.usda +0 -42
  350. warp/examples/assets/sphere.usda +0 -56
  351. warp/examples/assets/torus.usda +0 -105
  352. warp/examples/fem/example_convection_diffusion_dg0.py +0 -194
  353. warp/native/nanovdb/PNanoVDBWrite.h +0 -295
  354. warp_lang-1.0.2.dist-info/RECORD +0 -352
  355. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/WHEEL +0 -0
  356. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/top_level.txt +0 -0
warp/tests/test_types.py CHANGED
@@ -1,659 +1,554 @@
1
- # Copyright (c) 2023 NVIDIA CORPORATION. All rights reserved.
2
- # NVIDIA CORPORATION and its licensors retain all intellectual property
3
- # and proprietary rights in and to this software, related documentation
4
- # and any modifications thereto. Any use, reproduction, disclosure or
5
- # distribution of this software and related documentation without an express
6
- # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
-
8
- import unittest
9
-
10
- from warp.tests.unittest_utils import *
11
-
12
-
13
- wp.init()
14
-
15
-
16
- def test_bool(test, device):
17
- value = wp.bool(False)
18
- test.assertIsInstance(bool(value), bool)
19
- test.assertIsInstance(int(value), int)
20
- test.assertIsInstance(float(value), float)
21
- test.assertEqual(bool(value), False)
22
- test.assertEqual(int(value), 0)
23
- test.assertEqual(float(value), 0.0)
24
- try:
25
- ctypes.c_bool(value)
26
- except Exception:
27
- test.fail()
28
-
29
- value = wp.bool(True)
30
- test.assertIsInstance(bool(value), bool)
31
- test.assertIsInstance(int(value), int)
32
- test.assertIsInstance(float(value), float)
33
- test.assertEqual(bool(value), True)
34
- test.assertEqual(int(value), 1)
35
- test.assertEqual(float(value), 1.0)
36
- try:
37
- ctypes.c_bool(value)
38
- except Exception:
39
- test.fail()
40
-
41
- value = wp.bool(0.0)
42
- test.assertIsInstance(bool(value), bool)
43
- test.assertIsInstance(int(value), int)
44
- test.assertIsInstance(float(value), float)
45
- test.assertEqual(bool(value), False)
46
- test.assertEqual(int(value), 0)
47
- test.assertEqual(float(value), 0.0)
48
- try:
49
- ctypes.c_bool(value)
50
- except Exception:
51
- test.fail()
52
-
53
- value = wp.bool(123)
54
- test.assertIsInstance(bool(value), bool)
55
- test.assertIsInstance(int(value), int)
56
- test.assertIsInstance(float(value), float)
57
- test.assertEqual(bool(value), True)
58
- test.assertEqual(int(value), 1)
59
- test.assertEqual(float(value), 1.0)
60
- try:
61
- ctypes.c_bool(value)
62
- except Exception:
63
- test.fail()
64
-
65
-
66
- def test_integers(test, device, dtype):
67
- value = dtype(0)
68
- test.assertIsInstance(bool(value), bool)
69
- test.assertIsInstance(int(value), int)
70
- test.assertIsInstance(float(value), float)
71
- test.assertEqual(bool(value), False)
72
- test.assertEqual(int(value), 0)
73
- test.assertEqual(float(value), 0.0)
74
- try:
75
- ctypes.c_bool(value)
76
- ctypes.c_int(value)
77
- ctypes.c_float(value)
78
- except Exception:
79
- test.fail()
80
-
81
- value = dtype(123)
82
- test.assertIsInstance(bool(value), bool)
83
- test.assertIsInstance(int(value), int)
84
- test.assertIsInstance(float(value), float)
85
- test.assertEqual(bool(value), True)
86
- test.assertEqual(int(value), 123)
87
- test.assertEqual(float(value), 123.0)
88
- try:
89
- ctypes.c_bool(value)
90
- ctypes.c_int(value)
91
- ctypes.c_float(value)
92
- except Exception:
93
- test.fail()
94
-
95
-
96
- def test_floats(test, device, dtype):
97
- value = dtype(0.0)
98
- test.assertIsInstance(bool(value), bool)
99
- test.assertIsInstance(int(value), int)
100
- test.assertIsInstance(float(value), float)
101
- test.assertEqual(bool(value), False)
102
- test.assertEqual(int(value), 0)
103
- test.assertEqual(float(value), 0.0)
104
- try:
105
- ctypes.c_bool(value)
106
- ctypes.c_float(value)
107
- except Exception:
108
- test.fail()
109
-
110
- value = dtype(1.25)
111
- test.assertIsInstance(bool(value), bool)
112
- test.assertIsInstance(int(value), int)
113
- test.assertIsInstance(float(value), float)
114
- test.assertEqual(bool(value), True)
115
- test.assertEqual(int(value), 1)
116
- test.assertEqual(float(value), 1.25)
117
- try:
118
- ctypes.c_bool(value)
119
- ctypes.c_float(value)
120
- except Exception:
121
- test.fail()
122
-
123
-
124
- def test_constant(test, device):
125
- const = wp.constant(123)
126
- test.assertEqual(const, 123)
127
-
128
- const = wp.constant(1.25)
129
- test.assertEqual(const, 1.25)
130
-
131
- const = wp.constant(True)
132
- test.assertEqual(const, True)
133
-
134
- const = wp.constant(wp.float16(1.25))
135
- test.assertEqual(const.value, 1.25)
136
-
137
- const = wp.constant(wp.int16(123))
138
- test.assertEqual(const.value, 123)
139
-
140
- const = wp.constant(wp.vec3i(1, 2, 3))
141
- test.assertEqual(const, wp.vec3i(1, 2, 3))
142
-
143
-
144
- def test_constant_error_invalid_type(test, device):
145
- with test.assertRaisesRegex(
146
- RuntimeError,
147
- r"Invalid constant type: <class 'tuple'>$",
148
- ):
149
- wp.constant((1, 2, 3))
150
-
151
-
152
- def test_vector(test, device, dtype):
153
- def make_scalar(x):
154
- # Cast to the correct integer type to simulate wrapping.
155
- if dtype in wp.types.int_types:
156
- return dtype._type_(x).value
157
-
158
- return x
159
-
160
- def make_vec(*args):
161
- if dtype in wp.types.int_types:
162
- # Cast to the correct integer type to simulate wrapping.
163
- return tuple(dtype._type_(x).value for x in args)
164
-
165
- return args
166
-
167
- vec3_cls = wp.vec(3, dtype)
168
- vec4_cls = wp.vec(4, dtype)
169
-
170
- v = vec4_cls(1, 2, 3, 4)
171
- test.assertEqual(v[0], make_scalar(1))
172
- test.assertEqual(v.x, make_scalar(1))
173
- test.assertEqual(v.y, make_scalar(2))
174
- test.assertEqual(v.z, make_scalar(3))
175
- test.assertEqual(v.w, make_scalar(4))
176
- test.assertSequenceEqual(v[0:2], make_vec(1, 2))
177
- test.assertSequenceEqual(v, make_vec(1, 2, 3, 4))
178
-
179
- v[0] = -1
180
- test.assertEqual(v[0], make_scalar(-1))
181
- test.assertEqual(v.x, make_scalar(-1))
182
- test.assertEqual(v.y, make_scalar(2))
183
- test.assertEqual(v.z, make_scalar(3))
184
- test.assertEqual(v.w, make_scalar(4))
185
- test.assertSequenceEqual(v[0:2], make_vec(-1, 2))
186
- test.assertSequenceEqual(v, make_vec(-1, 2, 3, 4))
187
-
188
- v[1:3] = (-2, -3)
189
- test.assertEqual(v[0], make_scalar(-1))
190
- test.assertEqual(v.x, make_scalar(-1))
191
- test.assertEqual(v.y, make_scalar(-2))
192
- test.assertEqual(v.z, make_scalar(-3))
193
- test.assertEqual(v.w, make_scalar(4))
194
- test.assertSequenceEqual(v[0:2], make_vec(-1, -2))
195
- test.assertSequenceEqual(v, make_vec(-1, -2, -3, 4))
196
-
197
- v.x = 1
198
- test.assertEqual(v[0], make_scalar(1))
199
- test.assertEqual(v.x, make_scalar(1))
200
- test.assertEqual(v.y, make_scalar(-2))
201
- test.assertEqual(v.z, make_scalar(-3))
202
- test.assertEqual(v.w, make_scalar(4))
203
- test.assertSequenceEqual(v[0:2], make_vec(1, -2))
204
- test.assertSequenceEqual(v, make_vec(1, -2, -3, 4))
205
-
206
- v = vec3_cls(2, 4, 6)
207
- test.assertSequenceEqual(+v, make_vec(2, 4, 6))
208
- test.assertSequenceEqual(-v, make_vec(-2, -4, -6))
209
- test.assertSequenceEqual(v + vec3_cls(1, 1, 1), make_vec(3, 5, 7))
210
- test.assertSequenceEqual(v - vec3_cls(1, 1, 1), make_vec(1, 3, 5))
211
- test.assertSequenceEqual(v * dtype(2), make_vec(4, 8, 12))
212
- test.assertSequenceEqual(dtype(2) * v, make_vec(4, 8, 12))
213
- test.assertSequenceEqual(v / dtype(2), make_vec(1, 2, 3))
214
- test.assertSequenceEqual(dtype(12) / v, make_vec(6, 3, 2))
215
-
216
- test.assertTrue(v != vec3_cls(1, 2, 3))
217
- test.assertEqual(str(v), "[{}]".format(", ".join(str(x) for x in v)))
218
-
219
- # Check added purely for coverage reasons but is this really a desired
220
- # behaviour? Not allowing to define new attributes using systems like
221
- # `__slots__` could help improving memory usage.
222
- v.foo = 123
223
- test.assertEqual(v.foo, 123)
224
-
225
-
226
- def test_vector_assign(test, device):
227
- v = wp.vec3s()
228
- v[0] = 1
229
- v[1] = wp.int8(2)
230
- v[2] = np.int8(3)
231
- test.assertEqual(v, (1, 2, 3))
232
-
233
- v = wp.vec3h()
234
- v[0] = 1.0
235
- v[1] = wp.float16(2.0)
236
- v[2] = np.float16(3.0)
237
- test.assertEqual(v, (1.0, 2.0, 3.0))
238
-
239
-
240
- def test_vector_error_invalid_arg_count(test, device):
241
- with test.assertRaisesRegex(
242
- ValueError,
243
- r"Invalid number of arguments in vector constructor, expected 3 elements, got 2$",
244
- ):
245
- wp.vec3(1, 2)
246
-
247
-
248
- def test_vector_error_invalid_ptr(test, device):
249
- with test.assertRaisesRegex(
250
- RuntimeError,
251
- r"NULL pointer exception",
252
- ):
253
- wp.vec3.from_ptr(0)
254
-
255
-
256
- def test_vector_error_invalid_get_item_key(test, device):
257
- v = wp.vec3(1, 2, 3)
258
-
259
- with test.assertRaisesRegex(
260
- KeyError,
261
- r"Invalid key None, expected int or slice",
262
- ):
263
- v[None]
264
-
265
-
266
- def test_vector_error_invalid_set_item_key(test, device):
267
- v = wp.vec3(1, 2, 3)
268
- with test.assertRaisesRegex(
269
- KeyError,
270
- r"Invalid key None, expected int or slice",
271
- ):
272
- v[None] = 0
273
-
274
-
275
- def test_vector_error_invalid_set_item_value(test, device):
276
- v1 = wp.vec3i(1, 2, 3)
277
- v2 = wp.vec3h(1, 2, 3)
278
-
279
- with test.assertRaisesRegex(
280
- TypeError,
281
- r"Expected to assign a `int32` value but got `str` instead",
282
- ):
283
- v1[0] = "123.0"
284
-
285
- with test.assertRaisesRegex(
286
- TypeError,
287
- r"Expected to assign a slice from a sequence of values but got `int` instead",
288
- ):
289
- v1[:] = 123
290
-
291
- with test.assertRaisesRegex(
292
- TypeError,
293
- r"Expected to assign a slice from a sequence of `int32` values but got `vec3i` instead",
294
- ):
295
- v1[:1] = (v1,)
296
-
297
- with test.assertRaisesRegex(
298
- ValueError,
299
- r"Can only assign sequence of same size",
300
- ):
301
- v1[:1] = (1, 2)
302
-
303
- with test.assertRaisesRegex(
304
- TypeError,
305
- r"Expected to assign a slice from a sequence of `float16` values but got `vec3h` instead",
306
- ):
307
- v2[:1] = (v2,)
308
-
309
-
310
- def test_matrix(test, device):
311
- for dtype in tuple(wp.types.float_types) + (float,):
312
-
313
- def make_scalar(x):
314
- # Cast to the correct integer type to simulate wrapping.
315
- if dtype in wp.types.int_types:
316
- return dtype._type_(x).value
317
-
318
- return x
319
-
320
- def make_vec(*args):
321
- if dtype in wp.types.int_types:
322
- # Cast to the correct integer type to simulate wrapping.
323
- return tuple(dtype._type_(x).value for x in args)
324
-
325
- return args
326
-
327
- def make_mat(*args):
328
- if dtype in wp.types.int_types:
329
- # Cast to the correct integer type to simulate wrapping.
330
- return tuple(tuple(dtype._type_(x).value for x in row) for row in args)
331
-
332
- return args
333
-
334
- mat22_cls = wp.mat((2, 2), dtype)
335
- mat33_cls = wp.mat((3, 3), dtype)
336
- vec2_cls = wp.vec(2, dtype)
337
-
338
- m = mat33_cls(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
339
- test.assertEqual(m[0][0], make_scalar(1))
340
- test.assertEqual(m[0][1], make_scalar(2))
341
- test.assertEqual(m[0][2], make_scalar(3))
342
- test.assertEqual(m[1][0], make_scalar(4))
343
- test.assertEqual(m[1][1], make_scalar(5))
344
- test.assertEqual(m[1][2], make_scalar(6))
345
- test.assertEqual(m[2][0], make_scalar(7))
346
- test.assertEqual(m[2][1], make_scalar(8))
347
- test.assertEqual(m[2][2], make_scalar(9))
348
- test.assertEqual(m[0, 0], make_scalar(1))
349
- test.assertEqual(m[0, 1], make_scalar(2))
350
- test.assertEqual(m[0, 2], make_scalar(3))
351
- test.assertEqual(m[1, 0], make_scalar(4))
352
- test.assertEqual(m[1, 1], make_scalar(5))
353
- test.assertEqual(m[1, 2], make_scalar(6))
354
- test.assertEqual(m[2, 0], make_scalar(7))
355
- test.assertEqual(m[2, 1], make_scalar(8))
356
- test.assertEqual(m[2, 2], make_scalar(9))
357
- test.assertSequenceEqual(m[0], make_vec(1, 2, 3))
358
- test.assertSequenceEqual(m[1], make_vec(4, 5, 6))
359
- test.assertSequenceEqual(m[2], make_vec(7, 8, 9))
360
- test.assertSequenceEqual(m[0][1:3], make_vec(2, 3))
361
- test.assertSequenceEqual(m[1][0:2], make_vec(4, 5))
362
- test.assertSequenceEqual(m[2][0:3], make_vec(7, 8, 9))
363
- # test.assertSequenceEqual(m[0, 1:3], make_vec(2, 3))
364
- # test.assertSequenceEqual(m[1, 0:2], make_vec(4, 5))
365
- # test.assertSequenceEqual(m[2, 0:3], make_vec(7, 8, 9))
366
- test.assertSequenceEqual(m, make_mat((1, 2, 3), (4, 5, 6), (7, 8, 9)))
367
-
368
- m[1, 0] = -4
369
- test.assertEqual(m[0][0], make_scalar(1))
370
- test.assertEqual(m[0][1], make_scalar(2))
371
- test.assertEqual(m[0][2], make_scalar(3))
372
- test.assertEqual(m[1][0], make_scalar(-4))
373
- test.assertEqual(m[1][1], make_scalar(5))
374
- test.assertEqual(m[1][2], make_scalar(6))
375
- test.assertEqual(m[2][0], make_scalar(7))
376
- test.assertEqual(m[2][1], make_scalar(8))
377
- test.assertEqual(m[2][2], make_scalar(9))
378
- test.assertEqual(m[0, 0], make_scalar(1))
379
- test.assertEqual(m[0, 1], make_scalar(2))
380
- test.assertEqual(m[0, 2], make_scalar(3))
381
- test.assertEqual(m[1, 0], make_scalar(-4))
382
- test.assertEqual(m[1, 1], make_scalar(5))
383
- test.assertEqual(m[1, 2], make_scalar(6))
384
- test.assertEqual(m[2, 0], make_scalar(7))
385
- test.assertEqual(m[2, 1], make_scalar(8))
386
- test.assertEqual(m[2, 2], make_scalar(9))
387
- test.assertSequenceEqual(m[0], make_vec(1, 2, 3))
388
- test.assertSequenceEqual(m[1], make_vec(-4, 5, 6))
389
- test.assertSequenceEqual(m[2], make_vec(7, 8, 9))
390
- test.assertSequenceEqual(m[0][1:3], make_vec(2, 3))
391
- test.assertSequenceEqual(m[1][0:2], make_vec(-4, 5))
392
- test.assertSequenceEqual(m[2][0:3], make_vec(7, 8, 9))
393
- # test.assertSequenceEqual(m[0, 1:3], make_vec(2, 3))
394
- # test.assertSequenceEqual(m[1, 0:2], make_vec(-4, 5))
395
- # test.assertSequenceEqual(m[2, 0:3], make_vec(7, 8, 9))
396
- test.assertSequenceEqual(m, make_mat((1, 2, 3), (-4, 5, 6), (7, 8, 9)))
397
-
398
- m[2] = (-7, 8, -9)
399
- test.assertEqual(m[0][0], make_scalar(1))
400
- test.assertEqual(m[0][1], make_scalar(2))
401
- test.assertEqual(m[0][2], make_scalar(3))
402
- test.assertEqual(m[1][0], make_scalar(-4))
403
- test.assertEqual(m[1][1], make_scalar(5))
404
- test.assertEqual(m[1][2], make_scalar(6))
405
- test.assertEqual(m[2][0], make_scalar(-7))
406
- test.assertEqual(m[2][1], make_scalar(8))
407
- test.assertEqual(m[2][2], make_scalar(-9))
408
- test.assertEqual(m[0, 0], make_scalar(1))
409
- test.assertEqual(m[0, 1], make_scalar(2))
410
- test.assertEqual(m[0, 2], make_scalar(3))
411
- test.assertEqual(m[1, 0], make_scalar(-4))
412
- test.assertEqual(m[1, 1], make_scalar(5))
413
- test.assertEqual(m[1, 2], make_scalar(6))
414
- test.assertEqual(m[2, 0], make_scalar(-7))
415
- test.assertEqual(m[2, 1], make_scalar(8))
416
- test.assertEqual(m[2, 2], make_scalar(-9))
417
- test.assertSequenceEqual(m[0], make_vec(1, 2, 3))
418
- test.assertSequenceEqual(m[1], make_vec(-4, 5, 6))
419
- test.assertSequenceEqual(m[2], make_vec(-7, 8, -9))
420
- test.assertSequenceEqual(m[0][1:3], make_vec(2, 3))
421
- test.assertSequenceEqual(m[1][0:2], make_vec(-4, 5))
422
- test.assertSequenceEqual(m[2][0:3], make_vec(-7, 8, -9))
423
- # test.assertSequenceEqual(m[0, 1:3], make_vec(2, 3))
424
- # test.assertSequenceEqual(m[1, 0:2], make_vec(-4, 5))
425
- # test.assertSequenceEqual(m[2, 0:3], make_vec(-7, 8, -9))
426
- test.assertSequenceEqual(m, make_mat((1, 2, 3), (-4, 5, 6), (-7, 8, -9)))
427
-
428
- m = mat22_cls(2, 4, 6, 8)
429
- test.assertSequenceEqual(+m, make_mat((2, 4), (6, 8)))
430
- test.assertSequenceEqual(-m, make_mat((-2, -4), (-6, -8)))
431
- test.assertSequenceEqual(m + mat22_cls(1, 1, 1, 1), make_mat((3, 5), (7, 9)))
432
- test.assertSequenceEqual(m - mat22_cls(1, 1, 1, 1), make_mat((1, 3), (5, 7)))
433
- test.assertSequenceEqual(m * dtype(2), make_mat((4, 8), (12, 16)))
434
- test.assertSequenceEqual(dtype(2) * m, make_mat((4, 8), (12, 16)))
435
- test.assertSequenceEqual(m / dtype(2), make_mat((1, 2), (3, 4)))
436
- test.assertSequenceEqual(dtype(24) / m, make_mat((12, 6), (4, 3)))
437
-
438
- test.assertSequenceEqual(m * vec2_cls(1, 2), make_vec(10, 22))
439
- test.assertSequenceEqual(m @ vec2_cls(1, 2), make_vec(10, 22))
440
- test.assertSequenceEqual(vec2_cls(1, 2) * m, make_vec(14, 20))
441
- test.assertSequenceEqual(vec2_cls(1, 2) @ m, make_vec(14, 20))
442
-
443
- test.assertTrue(m != mat22_cls(1, 2, 3, 4))
444
- test.assertEqual(
445
- str(m),
446
- "[{}]".format(",\n ".join("[{}]".format(", ".join(str(y) for y in m[x])) for x in range(m._shape_[0]))),
447
- )
448
-
449
- # Check added purely for coverage reasons but is this really a desired
450
- # behaviour? Not allowing to define new attributes using systems like
451
- # `__slots__` could help improving memory usage.
452
- m.foo = 123
453
- test.assertEqual(m.foo, 123)
454
-
455
-
456
- def test_matrix_error_invalid_arg_count(test, device):
457
- with test.assertRaisesRegex(
458
- ValueError,
459
- r"Invalid number of arguments in matrix constructor, expected 4 elements, got 3$",
460
- ):
461
- wp.mat22(1, 2, 3)
462
-
463
-
464
- def test_matrix_error_invalid_row_count(test, device):
465
- with test.assertRaisesRegex(
466
- TypeError,
467
- r"Invalid argument in matrix constructor, expected row of length 2, got \(1, 2, 3\)$",
468
- ):
469
- wp.mat22((1, 2, 3), (3, 4, 5))
470
-
471
-
472
- def test_matrix_error_invalid_ptr(test, device):
473
- with test.assertRaisesRegex(
474
- RuntimeError,
475
- r"NULL pointer exception",
476
- ):
477
- wp.mat22.from_ptr(0)
478
-
479
-
480
- def test_matrix_error_invalid_set_row_index(test, device):
481
- m = wp.mat22(1, 2, 3, 4)
482
- with test.assertRaisesRegex(
483
- IndexError,
484
- r"Invalid row index$",
485
- ):
486
- m.set_row(2, (0, 0))
487
-
488
-
489
- def test_matrix_error_invalid_get_item_key(test, device):
490
- m = wp.mat22(1, 2, 3, 4)
491
-
492
- with test.assertRaisesRegex(
493
- KeyError,
494
- r"Invalid key None, expected int or pair of ints",
495
- ):
496
- m[None]
497
-
498
-
499
- def test_matrix_error_invalid_get_item_key_length(test, device):
500
- m = wp.mat22(1, 2, 3, 4)
501
-
502
- with test.assertRaisesRegex(
503
- KeyError,
504
- r"Invalid key, expected one or two indices, got 3",
505
- ):
506
- m[0, 1, 2]
507
-
508
-
509
- def test_matrix_error_invalid_set_item_key(test, device):
510
- m = wp.mat22(1, 2, 3, 4)
511
- with test.assertRaisesRegex(
512
- KeyError,
513
- r"Invalid key None, expected int or pair of ints",
514
- ):
515
- m[None] = 0
516
-
517
-
518
- def test_matrix_error_invalid_set_item_key_length(test, device):
519
- m = wp.mat22(1, 2, 3, 4)
520
-
521
- with test.assertRaisesRegex(
522
- KeyError,
523
- r"Invalid key, expected one or two indices, got 3",
524
- ):
525
- m[0, 1, 2] = (0, 0)
526
-
527
-
528
- def test_matrix_error_invalid_set_item_value(test, device):
529
- m = wp.mat22h(1, 2, 3, 4)
530
-
531
- with test.assertRaisesRegex(
532
- TypeError,
533
- r"Expected to assign a `float16` value but got `str` instead",
534
- ):
535
- m[0, 0] = "123.0"
536
-
537
- with test.assertRaisesRegex(
538
- TypeError,
539
- r"Expected to assign a `float16` value but got `str` instead",
540
- ):
541
- m[0][0] = "123.0"
542
-
543
- with test.assertRaisesRegex(
544
- TypeError,
545
- r"Expected to assign a slice from a sequence of values but got `int` instead",
546
- ):
547
- m[0] = 123
548
-
549
- with test.assertRaisesRegex(
550
- TypeError,
551
- r"Expected to assign a slice from a sequence of `float16` values but got `mat22h` instead",
552
- ):
553
- m[0] = (m,)
554
-
555
- with test.assertRaisesRegex(
556
- KeyError,
557
- r"Slices are not supported when indexing matrices using the `m\[start:end\]` notation",
558
- ):
559
- m[:] = 123
560
-
561
- with test.assertRaisesRegex(
562
- KeyError,
563
- r"Slices are not supported when indexing matrices using the `m\[i, j\]` notation",
564
- ):
565
- m[0, :1] = (123,)
566
-
567
- with test.assertRaisesRegex(
568
- ValueError,
569
- r"Can only assign sequence of same size",
570
- ):
571
- m[0][:1] = (1, 2)
572
-
573
-
574
- def test_dtype_from_numpy(test, device):
575
- import numpy as np
576
-
577
- def test_conversions(np_type, warp_type):
578
- test.assertEqual(wp.dtype_from_numpy(np_type), warp_type)
579
- test.assertEqual(wp.dtype_from_numpy(np.dtype(np_type)), warp_type)
580
-
581
- test_conversions(np.float16, wp.float16)
582
- test_conversions(np.float32, wp.float32)
583
- test_conversions(np.float64, wp.float64)
584
- test_conversions(np.int8, wp.int8)
585
- test_conversions(np.int16, wp.int16)
586
- test_conversions(np.int32, wp.int32)
587
- test_conversions(np.int64, wp.int64)
588
- test_conversions(np.uint8, wp.uint8)
589
- test_conversions(np.uint16, wp.uint16)
590
- test_conversions(np.uint32, wp.uint32)
591
- test_conversions(np.uint64, wp.uint64)
592
- test_conversions(np.bool_, wp.bool)
593
- test_conversions(np.byte, wp.int8)
594
- test_conversions(np.ubyte, wp.uint8)
595
-
596
-
597
- def test_dtype_to_numpy(test, device):
598
- import numpy as np
599
-
600
- def test_conversions(warp_type, np_type):
601
- test.assertEqual(wp.dtype_to_numpy(warp_type), np_type)
602
-
603
- test_conversions(wp.float16, np.float16)
604
- test_conversions(wp.float32, np.float32)
605
- test_conversions(wp.float64, np.float64)
606
- test_conversions(wp.int8, np.int8)
607
- test_conversions(wp.int16, np.int16)
608
- test_conversions(wp.int32, np.int32)
609
- test_conversions(wp.int64, np.int64)
610
- test_conversions(wp.uint8, np.uint8)
611
- test_conversions(wp.uint16, np.uint16)
612
- test_conversions(wp.uint32, np.uint32)
613
- test_conversions(wp.uint64, np.uint64)
614
- test_conversions(wp.bool, np.bool_)
615
-
616
-
617
- devices = [x for x in get_test_devices() if x.is_cpu]
618
-
619
-
620
- class TestTypes(unittest.TestCase):
621
- pass
622
-
623
-
624
- add_function_test(TestTypes, "test_bool", test_bool, devices=devices)
625
-
626
- for dtype in wp.types.int_types:
627
- add_function_test(TestTypes, f"test_integers_{dtype.__name__}", test_integers, devices=devices, dtype=dtype)
628
-
629
- for dtype in wp.types.float_types:
630
- add_function_test(TestTypes, f"test_floats_{dtype.__name__}", test_floats, devices=devices, dtype=dtype)
631
-
632
- add_function_test(TestTypes, "test_constant_error_invalid_type", test_constant_error_invalid_type, devices=devices)
633
-
634
- for dtype in tuple(wp.types.scalar_types) + (int, float):
635
- add_function_test(TestTypes, f"test_vector_{dtype.__name__}", test_vector, devices=devices, dtype=dtype)
636
-
637
- add_function_test(TestTypes, "test_vector_assign", test_vector_assign, devices=devices)
638
- add_function_test(TestTypes, "test_vector_error_invalid_arg_count", test_vector_error_invalid_arg_count, devices=devices)
639
- add_function_test(TestTypes, "test_vector_error_invalid_ptr", test_vector_error_invalid_ptr, devices=devices)
640
- add_function_test(TestTypes, "test_vector_error_invalid_get_item_key", test_vector_error_invalid_get_item_key, devices=devices)
641
- add_function_test(TestTypes, "test_vector_error_invalid_set_item_key", test_vector_error_invalid_set_item_key, devices=devices)
642
- add_function_test(TestTypes, "test_vector_error_invalid_set_item_value", test_vector_error_invalid_set_item_value, devices=devices)
643
- add_function_test(TestTypes, "test_matrix", test_matrix, devices=devices)
644
- add_function_test(TestTypes, "test_matrix_error_invalid_arg_count", test_matrix_error_invalid_arg_count, devices=devices)
645
- add_function_test(TestTypes, "test_matrix_error_invalid_row_count", test_matrix_error_invalid_row_count, devices=devices)
646
- add_function_test(TestTypes, "test_matrix_error_invalid_ptr", test_matrix_error_invalid_ptr, devices=devices)
647
- add_function_test(TestTypes, "test_matrix_error_invalid_set_row_index", test_matrix_error_invalid_set_row_index, devices=devices)
648
- add_function_test(TestTypes, "test_matrix_error_invalid_get_item_key", test_matrix_error_invalid_get_item_key, devices=devices)
649
- add_function_test(TestTypes, "test_matrix_error_invalid_get_item_key_length", test_matrix_error_invalid_get_item_key_length, devices=devices)
650
- add_function_test(TestTypes, "test_matrix_error_invalid_set_item_key", test_matrix_error_invalid_set_item_key, devices=devices)
651
- add_function_test(TestTypes, "test_matrix_error_invalid_set_item_key_length", test_matrix_error_invalid_set_item_key_length, devices=devices)
652
- add_function_test(TestTypes, "test_matrix_error_invalid_set_item_value", test_matrix_error_invalid_set_item_value, devices=devices)
653
-
654
- add_function_test(TestTypes, "test_dtype_from_numpy", test_dtype_from_numpy, devices=None)
655
- add_function_test(TestTypes, "test_dtype_to_numpy", test_dtype_to_numpy, devices=None)
656
-
657
- if __name__ == "__main__":
658
- wp.build.clear_kernel_cache()
659
- unittest.main(verbosity=2)
1
+ # Copyright (c) 2023 NVIDIA CORPORATION. All rights reserved.
2
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
3
+ # and proprietary rights in and to this software, related documentation
4
+ # and any modifications thereto. Any use, reproduction, disclosure or
5
+ # distribution of this software and related documentation without an express
6
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
+
8
+ import unittest
9
+
10
+ from warp.tests.unittest_utils import *
11
+
12
+
13
+ def test_integers(test, device, dtype):
14
+ value = dtype(0)
15
+ test.assertIsInstance(bool(value), bool)
16
+ test.assertIsInstance(int(value), int)
17
+ test.assertIsInstance(float(value), float)
18
+ test.assertEqual(bool(value), False)
19
+ test.assertEqual(int(value), 0)
20
+ test.assertEqual(float(value), 0.0)
21
+ try:
22
+ ctypes.c_bool(value)
23
+ ctypes.c_int(value)
24
+ ctypes.c_float(value)
25
+ except Exception:
26
+ test.fail()
27
+
28
+ value = dtype(123)
29
+ test.assertIsInstance(bool(value), bool)
30
+ test.assertIsInstance(int(value), int)
31
+ test.assertIsInstance(float(value), float)
32
+ test.assertEqual(bool(value), True)
33
+ test.assertEqual(int(value), 123)
34
+ test.assertEqual(float(value), 123.0)
35
+ try:
36
+ ctypes.c_bool(value)
37
+ ctypes.c_int(value)
38
+ ctypes.c_float(value)
39
+ except Exception:
40
+ test.fail()
41
+
42
+
43
+ def test_floats(test, device, dtype):
44
+ value = dtype(0.0)
45
+ test.assertIsInstance(bool(value), bool)
46
+ test.assertIsInstance(int(value), int)
47
+ test.assertIsInstance(float(value), float)
48
+ test.assertEqual(bool(value), False)
49
+ test.assertEqual(int(value), 0)
50
+ test.assertEqual(float(value), 0.0)
51
+ try:
52
+ ctypes.c_bool(value)
53
+ ctypes.c_float(value)
54
+ except Exception:
55
+ test.fail()
56
+
57
+ value = dtype(1.25)
58
+ test.assertIsInstance(bool(value), bool)
59
+ test.assertIsInstance(int(value), int)
60
+ test.assertIsInstance(float(value), float)
61
+ test.assertEqual(bool(value), True)
62
+ test.assertEqual(int(value), 1)
63
+ test.assertEqual(float(value), 1.25)
64
+ try:
65
+ ctypes.c_bool(value)
66
+ ctypes.c_float(value)
67
+ except Exception:
68
+ test.fail()
69
+
70
+
71
+ def test_vector(test, device, dtype):
72
+ def make_scalar(x):
73
+ # Cast to the correct integer type to simulate wrapping.
74
+ if dtype in wp.types.int_types:
75
+ return dtype._type_(x).value
76
+
77
+ return x
78
+
79
+ def make_vec(*args):
80
+ if dtype in wp.types.int_types:
81
+ # Cast to the correct integer type to simulate wrapping.
82
+ return tuple(dtype._type_(x).value for x in args)
83
+
84
+ return args
85
+
86
+ vec3_cls = wp.vec(3, dtype)
87
+ vec4_cls = wp.vec(4, dtype)
88
+
89
+ v = vec4_cls(1, 2, 3, 4)
90
+ test.assertEqual(v[0], make_scalar(1))
91
+ test.assertEqual(v.x, make_scalar(1))
92
+ test.assertEqual(v.y, make_scalar(2))
93
+ test.assertEqual(v.z, make_scalar(3))
94
+ test.assertEqual(v.w, make_scalar(4))
95
+ test.assertSequenceEqual(v[0:2], make_vec(1, 2))
96
+ test.assertSequenceEqual(v, make_vec(1, 2, 3, 4))
97
+
98
+ v[0] = -1
99
+ test.assertEqual(v[0], make_scalar(-1))
100
+ test.assertEqual(v.x, make_scalar(-1))
101
+ test.assertEqual(v.y, make_scalar(2))
102
+ test.assertEqual(v.z, make_scalar(3))
103
+ test.assertEqual(v.w, make_scalar(4))
104
+ test.assertSequenceEqual(v[0:2], make_vec(-1, 2))
105
+ test.assertSequenceEqual(v, make_vec(-1, 2, 3, 4))
106
+
107
+ v[1:3] = (-2, -3)
108
+ test.assertEqual(v[0], make_scalar(-1))
109
+ test.assertEqual(v.x, make_scalar(-1))
110
+ test.assertEqual(v.y, make_scalar(-2))
111
+ test.assertEqual(v.z, make_scalar(-3))
112
+ test.assertEqual(v.w, make_scalar(4))
113
+ test.assertSequenceEqual(v[0:2], make_vec(-1, -2))
114
+ test.assertSequenceEqual(v, make_vec(-1, -2, -3, 4))
115
+
116
+ v.x = 1
117
+ test.assertEqual(v[0], make_scalar(1))
118
+ test.assertEqual(v.x, make_scalar(1))
119
+ test.assertEqual(v.y, make_scalar(-2))
120
+ test.assertEqual(v.z, make_scalar(-3))
121
+ test.assertEqual(v.w, make_scalar(4))
122
+ test.assertSequenceEqual(v[0:2], make_vec(1, -2))
123
+ test.assertSequenceEqual(v, make_vec(1, -2, -3, 4))
124
+
125
+ v = vec3_cls(2, 4, 6)
126
+ test.assertSequenceEqual(+v, make_vec(2, 4, 6))
127
+ test.assertSequenceEqual(-v, make_vec(-2, -4, -6))
128
+ test.assertSequenceEqual(v + vec3_cls(1, 1, 1), make_vec(3, 5, 7))
129
+ test.assertSequenceEqual(v - vec3_cls(1, 1, 1), make_vec(1, 3, 5))
130
+ test.assertSequenceEqual(v * dtype(2), make_vec(4, 8, 12))
131
+ test.assertSequenceEqual(dtype(2) * v, make_vec(4, 8, 12))
132
+ test.assertSequenceEqual(v / dtype(2), make_vec(1, 2, 3))
133
+ test.assertSequenceEqual(dtype(12) / v, make_vec(6, 3, 2))
134
+
135
+ test.assertTrue(v != vec3_cls(1, 2, 3))
136
+ test.assertEqual(str(v), "[{}]".format(", ".join(str(x) for x in v)))
137
+
138
+ # Check added purely for coverage reasons but is this really a desired
139
+ # behaviour? Not allowing to define new attributes using systems like
140
+ # `__slots__` could help improving memory usage.
141
+ v.foo = 123
142
+ test.assertEqual(v.foo, 123)
143
+
144
+
145
+ devices = [x for x in get_test_devices() if x.is_cpu]
146
+
147
+
148
+ class TestTypes(unittest.TestCase):
149
+ def test_bool(self):
150
+ value = wp.bool(False)
151
+ self.assertIsInstance(bool(value), bool)
152
+ self.assertIsInstance(int(value), int)
153
+ self.assertIsInstance(float(value), float)
154
+ self.assertEqual(bool(value), False)
155
+ self.assertEqual(int(value), 0)
156
+ self.assertEqual(float(value), 0.0)
157
+ try:
158
+ ctypes.c_bool(value)
159
+ except Exception:
160
+ self.fail()
161
+
162
+ value = wp.bool(True)
163
+ self.assertIsInstance(bool(value), bool)
164
+ self.assertIsInstance(int(value), int)
165
+ self.assertIsInstance(float(value), float)
166
+ self.assertEqual(bool(value), True)
167
+ self.assertEqual(int(value), 1)
168
+ self.assertEqual(float(value), 1.0)
169
+ try:
170
+ ctypes.c_bool(value)
171
+ except Exception:
172
+ self.fail()
173
+
174
+ value = wp.bool(0.0)
175
+ self.assertIsInstance(bool(value), bool)
176
+ self.assertIsInstance(int(value), int)
177
+ self.assertIsInstance(float(value), float)
178
+ self.assertEqual(bool(value), False)
179
+ self.assertEqual(int(value), 0)
180
+ self.assertEqual(float(value), 0.0)
181
+ try:
182
+ ctypes.c_bool(value)
183
+ except Exception:
184
+ self.fail()
185
+
186
+ value = wp.bool(123)
187
+ self.assertIsInstance(bool(value), bool)
188
+ self.assertIsInstance(int(value), int)
189
+ self.assertIsInstance(float(value), float)
190
+ self.assertEqual(bool(value), True)
191
+ self.assertEqual(int(value), 1)
192
+ self.assertEqual(float(value), 1.0)
193
+ try:
194
+ ctypes.c_bool(value)
195
+ except Exception:
196
+ self.fail()
197
+
198
+ def test_constant(self):
199
+ const = wp.constant(123)
200
+ self.assertEqual(const, 123)
201
+
202
+ const = wp.constant(1.25)
203
+ self.assertEqual(const, 1.25)
204
+
205
+ const = wp.constant(True)
206
+ self.assertEqual(const, True)
207
+
208
+ const = wp.constant(wp.float16(1.25))
209
+ self.assertEqual(const.value, 1.25)
210
+
211
+ const = wp.constant(wp.int16(123))
212
+ self.assertEqual(const.value, 123)
213
+
214
+ const = wp.constant(wp.vec3i(1, 2, 3))
215
+ self.assertEqual(const, wp.vec3i(1, 2, 3))
216
+
217
+ def test_constant_error_invalid_type(self):
218
+ with self.assertRaisesRegex(RuntimeError, r"Invalid constant type: <class 'tuple'>$"):
219
+ wp.constant((1, 2, 3))
220
+
221
+ def test_vector_assign(self):
222
+ v = wp.vec3s()
223
+ v[0] = 1
224
+ v[1] = wp.int8(2)
225
+ v[2] = np.int8(3)
226
+ self.assertEqual(v, (1, 2, 3))
227
+
228
+ v = wp.vec3h()
229
+ v[0] = 1.0
230
+ v[1] = wp.float16(2.0)
231
+ v[2] = np.float16(3.0)
232
+ self.assertEqual(v, (1.0, 2.0, 3.0))
233
+
234
+ def test_vector_error_invalid_arg_count(self):
235
+ with self.assertRaisesRegex(
236
+ ValueError, r"Invalid number of arguments in vector constructor, expected 3 elements, got 2$"
237
+ ):
238
+ wp.vec3(1, 2)
239
+
240
+ def test_vector_error_invalid_ptr(self):
241
+ with self.assertRaisesRegex(RuntimeError, r"NULL pointer exception"):
242
+ wp.vec3.from_ptr(0)
243
+
244
+ def test_vector_error_invalid_get_item_key(self):
245
+ v = wp.vec3(1, 2, 3)
246
+
247
+ with self.assertRaisesRegex(KeyError, r"Invalid key None, expected int or slice"):
248
+ v[None]
249
+
250
+ def test_vector_error_invalid_set_item_key(self):
251
+ v = wp.vec3(1, 2, 3)
252
+ with self.assertRaisesRegex(KeyError, r"Invalid key None, expected int or slice"):
253
+ v[None] = 0
254
+
255
+ def test_vector_error_invalid_set_item_value(self):
256
+ v1 = wp.vec3i(1, 2, 3)
257
+ v2 = wp.vec3h(1, 2, 3)
258
+
259
+ with self.assertRaisesRegex(TypeError, r"Expected to assign a `int32` value but got `str` instead"):
260
+ v1[0] = "123.0"
261
+
262
+ with self.assertRaisesRegex(
263
+ TypeError, r"Expected to assign a slice from a sequence of values but got `int` instead"
264
+ ):
265
+ v1[:] = 123
266
+
267
+ with self.assertRaisesRegex(
268
+ TypeError, r"Expected to assign a slice from a sequence of `int32` values but got `vec3i` instead"
269
+ ):
270
+ v1[:1] = (v1,)
271
+
272
+ with self.assertRaisesRegex(ValueError, r"Can only assign sequence of same size"):
273
+ v1[:1] = (1, 2)
274
+
275
+ with self.assertRaisesRegex(
276
+ TypeError, r"Expected to assign a slice from a sequence of `float16` values but got `vec3h` instead"
277
+ ):
278
+ v2[:1] = (v2,)
279
+
280
+ def test_matrix(self):
281
+ for dtype in tuple(wp.types.float_types) + (float,):
282
+
283
+ def make_scalar(x, dtype=dtype):
284
+ # Cast to the correct integer type to simulate wrapping.
285
+ if dtype in wp.types.int_types:
286
+ return dtype._type_(x).value
287
+
288
+ return x
289
+
290
+ def make_vec(*args, dtype=dtype):
291
+ if dtype in wp.types.int_types:
292
+ # Cast to the correct integer type to simulate wrapping.
293
+ return tuple(dtype._type_(x).value for x in args)
294
+
295
+ return args
296
+
297
+ def make_mat(*args, dtype=dtype):
298
+ if dtype in wp.types.int_types:
299
+ # Cast to the correct integer type to simulate wrapping.
300
+ return tuple(tuple(dtype._type_(x).value for x in row) for row in args)
301
+
302
+ return args
303
+
304
+ mat22_cls = wp.mat((2, 2), dtype)
305
+ mat33_cls = wp.mat((3, 3), dtype)
306
+ vec2_cls = wp.vec(2, dtype)
307
+
308
+ m = mat33_cls(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
309
+ self.assertEqual(m[0][0], make_scalar(1))
310
+ self.assertEqual(m[0][1], make_scalar(2))
311
+ self.assertEqual(m[0][2], make_scalar(3))
312
+ self.assertEqual(m[1][0], make_scalar(4))
313
+ self.assertEqual(m[1][1], make_scalar(5))
314
+ self.assertEqual(m[1][2], make_scalar(6))
315
+ self.assertEqual(m[2][0], make_scalar(7))
316
+ self.assertEqual(m[2][1], make_scalar(8))
317
+ self.assertEqual(m[2][2], make_scalar(9))
318
+ self.assertEqual(m[0, 0], make_scalar(1))
319
+ self.assertEqual(m[0, 1], make_scalar(2))
320
+ self.assertEqual(m[0, 2], make_scalar(3))
321
+ self.assertEqual(m[1, 0], make_scalar(4))
322
+ self.assertEqual(m[1, 1], make_scalar(5))
323
+ self.assertEqual(m[1, 2], make_scalar(6))
324
+ self.assertEqual(m[2, 0], make_scalar(7))
325
+ self.assertEqual(m[2, 1], make_scalar(8))
326
+ self.assertEqual(m[2, 2], make_scalar(9))
327
+ self.assertSequenceEqual(m[0], make_vec(1, 2, 3))
328
+ self.assertSequenceEqual(m[1], make_vec(4, 5, 6))
329
+ self.assertSequenceEqual(m[2], make_vec(7, 8, 9))
330
+ self.assertSequenceEqual(m[0][1:3], make_vec(2, 3))
331
+ self.assertSequenceEqual(m[1][0:2], make_vec(4, 5))
332
+ self.assertSequenceEqual(m[2][0:3], make_vec(7, 8, 9))
333
+ # self.assertSequenceEqual(m[0, 1:3], make_vec(2, 3))
334
+ # self.assertSequenceEqual(m[1, 0:2], make_vec(4, 5))
335
+ # self.assertSequenceEqual(m[2, 0:3], make_vec(7, 8, 9))
336
+ self.assertSequenceEqual(m, make_mat((1, 2, 3), (4, 5, 6), (7, 8, 9)))
337
+
338
+ m[1, 0] = -4
339
+ self.assertEqual(m[0][0], make_scalar(1))
340
+ self.assertEqual(m[0][1], make_scalar(2))
341
+ self.assertEqual(m[0][2], make_scalar(3))
342
+ self.assertEqual(m[1][0], make_scalar(-4))
343
+ self.assertEqual(m[1][1], make_scalar(5))
344
+ self.assertEqual(m[1][2], make_scalar(6))
345
+ self.assertEqual(m[2][0], make_scalar(7))
346
+ self.assertEqual(m[2][1], make_scalar(8))
347
+ self.assertEqual(m[2][2], make_scalar(9))
348
+ self.assertEqual(m[0, 0], make_scalar(1))
349
+ self.assertEqual(m[0, 1], make_scalar(2))
350
+ self.assertEqual(m[0, 2], make_scalar(3))
351
+ self.assertEqual(m[1, 0], make_scalar(-4))
352
+ self.assertEqual(m[1, 1], make_scalar(5))
353
+ self.assertEqual(m[1, 2], make_scalar(6))
354
+ self.assertEqual(m[2, 0], make_scalar(7))
355
+ self.assertEqual(m[2, 1], make_scalar(8))
356
+ self.assertEqual(m[2, 2], make_scalar(9))
357
+ self.assertSequenceEqual(m[0], make_vec(1, 2, 3))
358
+ self.assertSequenceEqual(m[1], make_vec(-4, 5, 6))
359
+ self.assertSequenceEqual(m[2], make_vec(7, 8, 9))
360
+ self.assertSequenceEqual(m[0][1:3], make_vec(2, 3))
361
+ self.assertSequenceEqual(m[1][0:2], make_vec(-4, 5))
362
+ self.assertSequenceEqual(m[2][0:3], make_vec(7, 8, 9))
363
+ # self.assertSequenceEqual(m[0, 1:3], make_vec(2, 3))
364
+ # self.assertSequenceEqual(m[1, 0:2], make_vec(-4, 5))
365
+ # self.assertSequenceEqual(m[2, 0:3], make_vec(7, 8, 9))
366
+ self.assertSequenceEqual(m, make_mat((1, 2, 3), (-4, 5, 6), (7, 8, 9)))
367
+
368
+ m[2] = (-7, 8, -9)
369
+ self.assertEqual(m[0][0], make_scalar(1))
370
+ self.assertEqual(m[0][1], make_scalar(2))
371
+ self.assertEqual(m[0][2], make_scalar(3))
372
+ self.assertEqual(m[1][0], make_scalar(-4))
373
+ self.assertEqual(m[1][1], make_scalar(5))
374
+ self.assertEqual(m[1][2], make_scalar(6))
375
+ self.assertEqual(m[2][0], make_scalar(-7))
376
+ self.assertEqual(m[2][1], make_scalar(8))
377
+ self.assertEqual(m[2][2], make_scalar(-9))
378
+ self.assertEqual(m[0, 0], make_scalar(1))
379
+ self.assertEqual(m[0, 1], make_scalar(2))
380
+ self.assertEqual(m[0, 2], make_scalar(3))
381
+ self.assertEqual(m[1, 0], make_scalar(-4))
382
+ self.assertEqual(m[1, 1], make_scalar(5))
383
+ self.assertEqual(m[1, 2], make_scalar(6))
384
+ self.assertEqual(m[2, 0], make_scalar(-7))
385
+ self.assertEqual(m[2, 1], make_scalar(8))
386
+ self.assertEqual(m[2, 2], make_scalar(-9))
387
+ self.assertSequenceEqual(m[0], make_vec(1, 2, 3))
388
+ self.assertSequenceEqual(m[1], make_vec(-4, 5, 6))
389
+ self.assertSequenceEqual(m[2], make_vec(-7, 8, -9))
390
+ self.assertSequenceEqual(m[0][1:3], make_vec(2, 3))
391
+ self.assertSequenceEqual(m[1][0:2], make_vec(-4, 5))
392
+ self.assertSequenceEqual(m[2][0:3], make_vec(-7, 8, -9))
393
+ # self.assertSequenceEqual(m[0, 1:3], make_vec(2, 3))
394
+ # self.assertSequenceEqual(m[1, 0:2], make_vec(-4, 5))
395
+ # self.assertSequenceEqual(m[2, 0:3], make_vec(-7, 8, -9))
396
+ self.assertSequenceEqual(m, make_mat((1, 2, 3), (-4, 5, 6), (-7, 8, -9)))
397
+
398
+ m = mat22_cls(2, 4, 6, 8)
399
+ self.assertSequenceEqual(+m, make_mat((2, 4), (6, 8)))
400
+ self.assertSequenceEqual(-m, make_mat((-2, -4), (-6, -8)))
401
+ self.assertSequenceEqual(m + mat22_cls(1, 1, 1, 1), make_mat((3, 5), (7, 9)))
402
+ self.assertSequenceEqual(m - mat22_cls(1, 1, 1, 1), make_mat((1, 3), (5, 7)))
403
+ self.assertSequenceEqual(m * dtype(2), make_mat((4, 8), (12, 16)))
404
+ self.assertSequenceEqual(dtype(2) * m, make_mat((4, 8), (12, 16)))
405
+ self.assertSequenceEqual(m / dtype(2), make_mat((1, 2), (3, 4)))
406
+ self.assertSequenceEqual(dtype(24) / m, make_mat((12, 6), (4, 3)))
407
+
408
+ self.assertSequenceEqual(m * vec2_cls(1, 2), make_vec(10, 22))
409
+ self.assertSequenceEqual(m @ vec2_cls(1, 2), make_vec(10, 22))
410
+ self.assertSequenceEqual(vec2_cls(1, 2) * m, make_vec(14, 20))
411
+ self.assertSequenceEqual(vec2_cls(1, 2) @ m, make_vec(14, 20))
412
+
413
+ self.assertTrue(m != mat22_cls(1, 2, 3, 4))
414
+ self.assertEqual(
415
+ str(m),
416
+ "[{}]".format(",\n ".join("[{}]".format(", ".join(str(y) for y in m[x])) for x in range(m._shape_[0]))),
417
+ )
418
+
419
+ # Check added purely for coverage reasons but is this really a desired
420
+ # behaviour? Not allowing to define new attributes using systems like
421
+ # `__slots__` could help improving memory usage.
422
+ m.foo = 123
423
+ self.assertEqual(m.foo, 123)
424
+
425
+ def test_matrix_error_invalid_arg_count(self):
426
+ with self.assertRaisesRegex(
427
+ ValueError, r"Invalid number of arguments in matrix constructor, expected 4 elements, got 3$"
428
+ ):
429
+ wp.mat22(1, 2, 3)
430
+
431
+ def test_matrix_error_invalid_row_count(self):
432
+ with self.assertRaisesRegex(
433
+ TypeError, r"Invalid argument in matrix constructor, expected row of length 2, got \(1, 2, 3\)$"
434
+ ):
435
+ wp.mat22((1, 2, 3), (3, 4, 5))
436
+
437
+ def test_matrix_error_invalid_ptr(self):
438
+ with self.assertRaisesRegex(RuntimeError, r"NULL pointer exception"):
439
+ wp.mat22.from_ptr(0)
440
+
441
+ def test_matrix_error_invalid_set_row_index(self):
442
+ m = wp.mat22(1, 2, 3, 4)
443
+ with self.assertRaisesRegex(IndexError, r"Invalid row index$"):
444
+ m.set_row(2, (0, 0))
445
+
446
+ def test_matrix_error_invalid_get_item_key(self):
447
+ m = wp.mat22(1, 2, 3, 4)
448
+
449
+ with self.assertRaisesRegex(KeyError, r"Invalid key None, expected int or pair of ints"):
450
+ m[None]
451
+
452
+ def test_matrix_error_invalid_get_item_key_length(self):
453
+ m = wp.mat22(1, 2, 3, 4)
454
+
455
+ with self.assertRaisesRegex(KeyError, r"Invalid key, expected one or two indices, got 3"):
456
+ m[0, 1, 2]
457
+
458
+ def test_matrix_error_invalid_set_item_key(self):
459
+ m = wp.mat22(1, 2, 3, 4)
460
+ with self.assertRaisesRegex(KeyError, r"Invalid key None, expected int or pair of ints"):
461
+ m[None] = 0
462
+
463
+ def test_matrix_error_invalid_set_item_key_length(self):
464
+ m = wp.mat22(1, 2, 3, 4)
465
+
466
+ with self.assertRaisesRegex(KeyError, r"Invalid key, expected one or two indices, got 3"):
467
+ m[0, 1, 2] = (0, 0)
468
+
469
+ def test_matrix_error_invalid_set_item_value(self):
470
+ m = wp.mat22h(1, 2, 3, 4)
471
+
472
+ with self.assertRaisesRegex(TypeError, r"Expected to assign a `float16` value but got `str` instead"):
473
+ m[0, 0] = "123.0"
474
+
475
+ with self.assertRaisesRegex(TypeError, r"Expected to assign a `float16` value but got `str` instead"):
476
+ m[0][0] = "123.0"
477
+
478
+ with self.assertRaisesRegex(
479
+ TypeError, r"Expected to assign a slice from a sequence of values but got `int` instead"
480
+ ):
481
+ m[0] = 123
482
+
483
+ with self.assertRaisesRegex(
484
+ TypeError, r"Expected to assign a slice from a sequence of `float16` values but got `mat22h` instead"
485
+ ):
486
+ m[0] = (m,)
487
+
488
+ with self.assertRaisesRegex(
489
+ KeyError, r"Slices are not supported when indexing matrices using the `m\[start:end\]` notation"
490
+ ):
491
+ m[:] = 123
492
+
493
+ with self.assertRaisesRegex(
494
+ KeyError, r"Slices are not supported when indexing matrices using the `m\[i, j\]` notation"
495
+ ):
496
+ m[0, :1] = (123,)
497
+
498
+ with self.assertRaisesRegex(ValueError, r"Can only assign sequence of same size"):
499
+ m[0][:1] = (1, 2)
500
+
501
+ def test_dtype_from_numpy(self):
502
+ import numpy as np
503
+
504
+ def test_conversions(np_type, warp_type):
505
+ self.assertEqual(wp.dtype_from_numpy(np_type), warp_type)
506
+ self.assertEqual(wp.dtype_from_numpy(np.dtype(np_type)), warp_type)
507
+
508
+ test_conversions(np.float16, wp.float16)
509
+ test_conversions(np.float32, wp.float32)
510
+ test_conversions(np.float64, wp.float64)
511
+ test_conversions(np.int8, wp.int8)
512
+ test_conversions(np.int16, wp.int16)
513
+ test_conversions(np.int32, wp.int32)
514
+ test_conversions(np.int64, wp.int64)
515
+ test_conversions(np.uint8, wp.uint8)
516
+ test_conversions(np.uint16, wp.uint16)
517
+ test_conversions(np.uint32, wp.uint32)
518
+ test_conversions(np.uint64, wp.uint64)
519
+ test_conversions(np.bool_, wp.bool)
520
+ test_conversions(np.byte, wp.int8)
521
+ test_conversions(np.ubyte, wp.uint8)
522
+
523
+ def test_dtype_to_numpy(self):
524
+ import numpy as np
525
+
526
+ def test_conversions(warp_type, np_type):
527
+ self.assertEqual(wp.dtype_to_numpy(warp_type), np_type)
528
+
529
+ test_conversions(wp.float16, np.float16)
530
+ test_conversions(wp.float32, np.float32)
531
+ test_conversions(wp.float64, np.float64)
532
+ test_conversions(wp.int8, np.int8)
533
+ test_conversions(wp.int16, np.int16)
534
+ test_conversions(wp.int32, np.int32)
535
+ test_conversions(wp.int64, np.int64)
536
+ test_conversions(wp.uint8, np.uint8)
537
+ test_conversions(wp.uint16, np.uint16)
538
+ test_conversions(wp.uint32, np.uint32)
539
+ test_conversions(wp.uint64, np.uint64)
540
+ test_conversions(wp.bool, np.bool_)
541
+
542
+
543
+ for dtype in wp.types.int_types:
544
+ add_function_test(TestTypes, f"test_integers_{dtype.__name__}", test_integers, devices=devices, dtype=dtype)
545
+
546
+ for dtype in wp.types.float_types:
547
+ add_function_test(TestTypes, f"test_floats_{dtype.__name__}", test_floats, devices=devices, dtype=dtype)
548
+
549
+ for dtype in tuple(wp.types.scalar_types) + (int, float):
550
+ add_function_test(TestTypes, f"test_vector_{dtype.__name__}", test_vector, devices=devices, dtype=dtype)
551
+
552
+ if __name__ == "__main__":
553
+ wp.build.clear_kernel_cache()
554
+ unittest.main(verbosity=2)