File: complex_test_common.h

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (658 lines) | stat: -rw-r--r-- 20,546 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#include <c10/macros/Macros.h>
#include <c10/util/complex.h>
#include <c10/util/hash.h>
#include <gtest/gtest.h>
#include <sstream>
#include <tuple>
#include <type_traits>
#include <unordered_map>

#if (defined(__CUDACC__) || defined(__HIPCC__))
#define MAYBE_GLOBAL __global__
#else
#define MAYBE_GLOBAL
#endif

#define PI 3.141592653589793238463

namespace memory {

MAYBE_GLOBAL void test_size() {
  static_assert(sizeof(c10::complex<float>) == 2 * sizeof(float), "");
  static_assert(sizeof(c10::complex<double>) == 2 * sizeof(double), "");
}

MAYBE_GLOBAL void test_align() {
  static_assert(alignof(c10::complex<float>) == 2 * sizeof(float), "");
  static_assert(alignof(c10::complex<double>) == 2 * sizeof(double), "");
}

MAYBE_GLOBAL void test_pod() {
  static_assert(std::is_standard_layout<c10::complex<float>>::value, "");
  static_assert(std::is_standard_layout<c10::complex<double>>::value, "");
}

TEST(TestMemory, ReinterpretCast) {
  {
    std::complex<float> z(1, 2);
    c10::complex<float> zz = *reinterpret_cast<c10::complex<float>*>(&z);
    ASSERT_EQ(zz.real(), float(1));
    ASSERT_EQ(zz.imag(), float(2));
  }

  {
    c10::complex<float> z(3, 4);
    std::complex<float> zz = *reinterpret_cast<std::complex<float>*>(&z);
    ASSERT_EQ(zz.real(), float(3));
    ASSERT_EQ(zz.imag(), float(4));
  }

  {
    std::complex<double> z(1, 2);
    c10::complex<double> zz = *reinterpret_cast<c10::complex<double>*>(&z);
    ASSERT_EQ(zz.real(), double(1));
    ASSERT_EQ(zz.imag(), double(2));
  }

  {
    c10::complex<double> z(3, 4);
    std::complex<double> zz = *reinterpret_cast<std::complex<double>*>(&z);
    ASSERT_EQ(zz.real(), double(3));
    ASSERT_EQ(zz.imag(), double(4));
  }
}

#if defined(__CUDACC__) || defined(__HIPCC__)
TEST(TestMemory, ThrustReinterpretCast) {
  {
    thrust::complex<float> z(1, 2);
    c10::complex<float> zz = *reinterpret_cast<c10::complex<float>*>(&z);
    ASSERT_EQ(zz.real(), float(1));
    ASSERT_EQ(zz.imag(), float(2));
  }

  {
    c10::complex<float> z(3, 4);
    thrust::complex<float> zz = *reinterpret_cast<thrust::complex<float>*>(&z);
    ASSERT_EQ(zz.real(), float(3));
    ASSERT_EQ(zz.imag(), float(4));
  }

  {
    thrust::complex<double> z(1, 2);
    c10::complex<double> zz = *reinterpret_cast<c10::complex<double>*>(&z);
    ASSERT_EQ(zz.real(), double(1));
    ASSERT_EQ(zz.imag(), double(2));
  }

  {
    c10::complex<double> z(3, 4);
    thrust::complex<double> zz =
        *reinterpret_cast<thrust::complex<double>*>(&z);
    ASSERT_EQ(zz.real(), double(3));
    ASSERT_EQ(zz.imag(), double(4));
  }
}
#endif

} // namespace memory

namespace constructors {

template <typename scalar_t>
C10_HOST_DEVICE void test_construct_from_scalar() {
  constexpr scalar_t num1 = scalar_t(1.23);
  constexpr scalar_t num2 = scalar_t(4.56);
  constexpr scalar_t zero = scalar_t();
  static_assert(c10::complex<scalar_t>(num1, num2).real() == num1, "");
  static_assert(c10::complex<scalar_t>(num1, num2).imag() == num2, "");
  static_assert(c10::complex<scalar_t>(num1).real() == num1, "");
  static_assert(c10::complex<scalar_t>(num1).imag() == zero, "");
  static_assert(c10::complex<scalar_t>().real() == zero, "");
  static_assert(c10::complex<scalar_t>().imag() == zero, "");
}

template <typename scalar_t, typename other_t>
C10_HOST_DEVICE void test_construct_from_other() {
  constexpr other_t num1 = other_t(1.23);
  constexpr other_t num2 = other_t(4.56);
  constexpr scalar_t num3 = scalar_t(num1);
  constexpr scalar_t num4 = scalar_t(num2);
  static_assert(
      c10::complex<scalar_t>(c10::complex<other_t>(num1, num2)).real() == num3,
      "");
  static_assert(
      c10::complex<scalar_t>(c10::complex<other_t>(num1, num2)).imag() == num4,
      "");
}

MAYBE_GLOBAL void test_convert_constructors() {
  test_construct_from_scalar<float>();
  test_construct_from_scalar<double>();

  static_assert(
      std::is_convertible<c10::complex<float>, c10::complex<float>>::value, "");
  static_assert(
      !std::is_convertible<c10::complex<double>, c10::complex<float>>::value,
      "");
  static_assert(
      std::is_convertible<c10::complex<float>, c10::complex<double>>::value,
      "");
  static_assert(
      std::is_convertible<c10::complex<double>, c10::complex<double>>::value,
      "");

  static_assert(
      std::is_constructible<c10::complex<float>, c10::complex<float>>::value,
      "");
  static_assert(
      std::is_constructible<c10::complex<double>, c10::complex<float>>::value,
      "");
  static_assert(
      std::is_constructible<c10::complex<float>, c10::complex<double>>::value,
      "");
  static_assert(
      std::is_constructible<c10::complex<double>, c10::complex<double>>::value,
      "");

  test_construct_from_other<float, float>();
  test_construct_from_other<float, double>();
  test_construct_from_other<double, float>();
  test_construct_from_other<double, double>();
}

template <typename scalar_t>
C10_HOST_DEVICE void test_construct_from_std() {
  constexpr scalar_t num1 = scalar_t(1.23);
  constexpr scalar_t num2 = scalar_t(4.56);
  static_assert(
      c10::complex<scalar_t>(std::complex<scalar_t>(num1, num2)).real() == num1,
      "");
  static_assert(
      c10::complex<scalar_t>(std::complex<scalar_t>(num1, num2)).imag() == num2,
      "");
}

MAYBE_GLOBAL void test_std_conversion() {
  test_construct_from_std<float>();
  test_construct_from_std<double>();
}

#if defined(__CUDACC__) || defined(__HIPCC__)
template <typename scalar_t>
void test_construct_from_thrust() {
  constexpr scalar_t num1 = scalar_t(1.23);
  constexpr scalar_t num2 = scalar_t(4.56);
  ASSERT_EQ(
      c10::complex<scalar_t>(thrust::complex<scalar_t>(num1, num2)).real(),
      num1);
  ASSERT_EQ(
      c10::complex<scalar_t>(thrust::complex<scalar_t>(num1, num2)).imag(),
      num2);
}

TEST(TestConstructors, FromThrust) {
  test_construct_from_thrust<float>();
  test_construct_from_thrust<double>();
}
#endif

TEST(TestConstructors, UnorderedMap) {
  std::unordered_map<
      c10::complex<double>,
      c10::complex<double>,
      c10::hash<c10::complex<double>>>
      m;
  auto key1 = c10::complex<double>(2.5, 3);
  auto key2 = c10::complex<double>(2, 0);
  auto val1 = c10::complex<double>(2, -3.2);
  auto val2 = c10::complex<double>(0, -3);
  m[key1] = val1;
  m[key2] = val2;
  ASSERT_EQ(m[key1], val1);
  ASSERT_EQ(m[key2], val2);
}

} // namespace constructors

namespace assignment {

template <typename scalar_t>
constexpr c10::complex<scalar_t> one() {
  c10::complex<scalar_t> result(3, 4);
  result = scalar_t(1);
  return result;
}

MAYBE_GLOBAL void test_assign_real() {
  static_assert(one<float>().real() == float(1), "");
  static_assert(one<float>().imag() == float(), "");
  static_assert(one<double>().real() == double(1), "");
  static_assert(one<double>().imag() == double(), "");
}

constexpr std::tuple<c10::complex<double>, c10::complex<float>> one_two() {
  constexpr c10::complex<float> src(1, 2);
  c10::complex<double> ret0;
  c10::complex<float> ret1;
  ret0 = ret1 = src;
  return std::make_tuple(ret0, ret1);
}

MAYBE_GLOBAL void test_assign_other() {
  constexpr auto tup = one_two();
  static_assert(std::get<c10::complex<double>>(tup).real() == double(1), "");
  static_assert(std::get<c10::complex<double>>(tup).imag() == double(2), "");
  static_assert(std::get<c10::complex<float>>(tup).real() == float(1), "");
  static_assert(std::get<c10::complex<float>>(tup).imag() == float(2), "");
}

constexpr std::tuple<c10::complex<double>, c10::complex<float>> one_two_std() {
  constexpr std::complex<float> src(1, 1);
  c10::complex<double> ret0;
  c10::complex<float> ret1;
  ret0 = ret1 = src;
  return std::make_tuple(ret0, ret1);
}

MAYBE_GLOBAL void test_assign_std() {
  constexpr auto tup = one_two();
  static_assert(std::get<c10::complex<double>>(tup).real() == double(1), "");
  static_assert(std::get<c10::complex<double>>(tup).imag() == double(2), "");
  static_assert(std::get<c10::complex<float>>(tup).real() == float(1), "");
  static_assert(std::get<c10::complex<float>>(tup).imag() == float(2), "");
}

#if defined(__CUDACC__) || defined(__HIPCC__)
C10_HOST_DEVICE std::tuple<c10::complex<double>, c10::complex<float>>
one_two_thrust() {
  thrust::complex<float> src(1, 2);
  c10::complex<double> ret0;
  c10::complex<float> ret1;
  ret0 = ret1 = src;
  return std::make_tuple(ret0, ret1);
}

TEST(TestAssignment, FromThrust) {
  auto tup = one_two_thrust();
  ASSERT_EQ(std::get<c10::complex<double>>(tup).real(), double(1));
  ASSERT_EQ(std::get<c10::complex<double>>(tup).imag(), double(2));
  ASSERT_EQ(std::get<c10::complex<float>>(tup).real(), float(1));
  ASSERT_EQ(std::get<c10::complex<float>>(tup).imag(), float(2));
}
#endif

} // namespace assignment

namespace literals {

MAYBE_GLOBAL void test_complex_literals() {
  using namespace c10::complex_literals;
  static_assert(std::is_same<decltype(0.5_if), c10::complex<float>>::value, "");
  static_assert((0.5_if).real() == float(), "");
  static_assert((0.5_if).imag() == float(0.5), "");
  static_assert(
      std::is_same<decltype(0.5_id), c10::complex<double>>::value, "");
  static_assert((0.5_id).real() == float(), "");
  static_assert((0.5_id).imag() == float(0.5), "");

  static_assert(std::is_same<decltype(1_if), c10::complex<float>>::value, "");
  static_assert((1_if).real() == float(), "");
  static_assert((1_if).imag() == float(1), "");
  static_assert(std::is_same<decltype(1_id), c10::complex<double>>::value, "");
  static_assert((1_id).real() == double(), "");
  static_assert((1_id).imag() == double(1), "");
}

} // namespace literals

namespace real_imag {

template <typename scalar_t>
constexpr c10::complex<scalar_t> zero_one() {
  c10::complex<scalar_t> result;
  result.imag(scalar_t(1));
  return result;
}

template <typename scalar_t>
constexpr c10::complex<scalar_t> one_zero() {
  c10::complex<scalar_t> result;
  result.real(scalar_t(1));
  return result;
}

MAYBE_GLOBAL void test_real_imag_modify() {
  static_assert(zero_one<float>().real() == float(0), "");
  static_assert(zero_one<float>().imag() == float(1), "");
  static_assert(zero_one<double>().real() == double(0), "");
  static_assert(zero_one<double>().imag() == double(1), "");

  static_assert(one_zero<float>().real() == float(1), "");
  static_assert(one_zero<float>().imag() == float(0), "");
  static_assert(one_zero<double>().real() == double(1), "");
  static_assert(one_zero<double>().imag() == double(0), "");
}

} // namespace real_imag

namespace arithmetic_assign {

template <typename scalar_t>
constexpr c10::complex<scalar_t> p(scalar_t value) {
  c10::complex<scalar_t> result(scalar_t(2), scalar_t(2));
  result += value;
  return result;
}

template <typename scalar_t>
constexpr c10::complex<scalar_t> m(scalar_t value) {
  c10::complex<scalar_t> result(scalar_t(2), scalar_t(2));
  result -= value;
  return result;
}

template <typename scalar_t>
constexpr c10::complex<scalar_t> t(scalar_t value) {
  c10::complex<scalar_t> result(scalar_t(2), scalar_t(2));
  result *= value;
  return result;
}

template <typename scalar_t>
constexpr c10::complex<scalar_t> d(scalar_t value) {
  c10::complex<scalar_t> result(scalar_t(2), scalar_t(2));
  result /= value;
  return result;
}

template <typename scalar_t>
C10_HOST_DEVICE void test_arithmetic_assign_scalar() {
  constexpr c10::complex<scalar_t> x = p(scalar_t(1));
  static_assert(x.real() == scalar_t(3), "");
  static_assert(x.imag() == scalar_t(2), "");
  constexpr c10::complex<scalar_t> y = m(scalar_t(1));
  static_assert(y.real() == scalar_t(1), "");
  static_assert(y.imag() == scalar_t(2), "");
  constexpr c10::complex<scalar_t> z = t(scalar_t(2));
  static_assert(z.real() == scalar_t(4), "");
  static_assert(z.imag() == scalar_t(4), "");
  constexpr c10::complex<scalar_t> t = d(scalar_t(2));
  static_assert(t.real() == scalar_t(1), "");
  static_assert(t.imag() == scalar_t(1), "");
}

template <typename scalar_t, typename rhs_t>
constexpr c10::complex<scalar_t> p(
    scalar_t real,
    scalar_t imag,
    c10::complex<rhs_t> rhs) {
  c10::complex<scalar_t> result(real, imag);
  result += rhs;
  return result;
}

template <typename scalar_t, typename rhs_t>
constexpr c10::complex<scalar_t> m(
    scalar_t real,
    scalar_t imag,
    c10::complex<rhs_t> rhs) {
  c10::complex<scalar_t> result(real, imag);
  result -= rhs;
  return result;
}

template <typename scalar_t, typename rhs_t>
constexpr c10::complex<scalar_t> t(
    scalar_t real,
    scalar_t imag,
    c10::complex<rhs_t> rhs) {
  c10::complex<scalar_t> result(real, imag);
  result *= rhs;
  return result;
}

template <typename scalar_t, typename rhs_t>
constexpr c10::complex<scalar_t> d(
    scalar_t real,
    scalar_t imag,
    c10::complex<rhs_t> rhs) {
  c10::complex<scalar_t> result(real, imag);
  result /= rhs;
  return result;
}

template <typename scalar_t>
C10_HOST_DEVICE void test_arithmetic_assign_complex() {
  using namespace c10::complex_literals;
  constexpr c10::complex<scalar_t> x2 = p(scalar_t(2), scalar_t(2), 1.0_if);
  static_assert(x2.real() == scalar_t(2), "");
  static_assert(x2.imag() == scalar_t(3), "");
  constexpr c10::complex<scalar_t> x3 = p(scalar_t(2), scalar_t(2), 1.0_id);
  static_assert(x3.real() == scalar_t(2), "");

  // this test is skipped due to a bug in constexpr evaluation
  // in nvcc. This bug has already been fixed since CUDA 11.2
#if !defined(__CUDACC__) || (defined(CUDA_VERSION) && CUDA_VERSION >= 11020)
  static_assert(x3.imag() == scalar_t(3), "");
#endif

  constexpr c10::complex<scalar_t> y2 = m(scalar_t(2), scalar_t(2), 1.0_if);
  static_assert(y2.real() == scalar_t(2), "");
  static_assert(y2.imag() == scalar_t(1), "");
  constexpr c10::complex<scalar_t> y3 = m(scalar_t(2), scalar_t(2), 1.0_id);
  static_assert(y3.real() == scalar_t(2), "");

  // this test is skipped due to a bug in constexpr evaluation
  // in nvcc. This bug has already been fixed since CUDA 11.2
#if !defined(__CUDACC__) || (defined(CUDA_VERSION) && CUDA_VERSION >= 11020)
  static_assert(y3.imag() == scalar_t(1), "");
#endif

  constexpr c10::complex<scalar_t> z2 = t(scalar_t(1), scalar_t(-2), 1.0_if);
  static_assert(z2.real() == scalar_t(2), "");
  static_assert(z2.imag() == scalar_t(1), "");
  constexpr c10::complex<scalar_t> z3 = t(scalar_t(1), scalar_t(-2), 1.0_id);
  static_assert(z3.real() == scalar_t(2), "");
  static_assert(z3.imag() == scalar_t(1), "");

  constexpr c10::complex<scalar_t> t2 = d(scalar_t(-1), scalar_t(2), 1.0_if);
  static_assert(t2.real() == scalar_t(2), "");
  static_assert(t2.imag() == scalar_t(1), "");
  constexpr c10::complex<scalar_t> t3 = d(scalar_t(-1), scalar_t(2), 1.0_id);
  static_assert(t3.real() == scalar_t(2), "");
  static_assert(t3.imag() == scalar_t(1), "");
}

MAYBE_GLOBAL void test_arithmetic_assign() {
  test_arithmetic_assign_scalar<float>();
  test_arithmetic_assign_scalar<double>();
  test_arithmetic_assign_complex<float>();
  test_arithmetic_assign_complex<double>();
}

} // namespace arithmetic_assign

namespace arithmetic {

template <typename scalar_t>
C10_HOST_DEVICE void test_arithmetic_() {
  static_assert(
      c10::complex<scalar_t>(1, 2) == +c10::complex<scalar_t>(1, 2), "");
  static_assert(
      c10::complex<scalar_t>(-1, -2) == -c10::complex<scalar_t>(1, 2), "");

  static_assert(
      c10::complex<scalar_t>(1, 2) + c10::complex<scalar_t>(3, 4) ==
          c10::complex<scalar_t>(4, 6),
      "");
  static_assert(
      c10::complex<scalar_t>(1, 2) + scalar_t(3) ==
          c10::complex<scalar_t>(4, 2),
      "");
  static_assert(
      scalar_t(3) + c10::complex<scalar_t>(1, 2) ==
          c10::complex<scalar_t>(4, 2),
      "");

  static_assert(
      c10::complex<scalar_t>(1, 2) - c10::complex<scalar_t>(3, 4) ==
          c10::complex<scalar_t>(-2, -2),
      "");
  static_assert(
      c10::complex<scalar_t>(1, 2) - scalar_t(3) ==
          c10::complex<scalar_t>(-2, 2),
      "");
  static_assert(
      scalar_t(3) - c10::complex<scalar_t>(1, 2) ==
          c10::complex<scalar_t>(2, -2),
      "");

  static_assert(
      c10::complex<scalar_t>(1, 2) * c10::complex<scalar_t>(3, 4) ==
          c10::complex<scalar_t>(-5, 10),
      "");
  static_assert(
      c10::complex<scalar_t>(1, 2) * scalar_t(3) ==
          c10::complex<scalar_t>(3, 6),
      "");
  static_assert(
      scalar_t(3) * c10::complex<scalar_t>(1, 2) ==
          c10::complex<scalar_t>(3, 6),
      "");

  static_assert(
      c10::complex<scalar_t>(-5, 10) / c10::complex<scalar_t>(3, 4) ==
          c10::complex<scalar_t>(1, 2),
      "");
  static_assert(
      c10::complex<scalar_t>(5, 10) / scalar_t(5) ==
          c10::complex<scalar_t>(1, 2),
      "");
  static_assert(
      scalar_t(25) / c10::complex<scalar_t>(3, 4) ==
          c10::complex<scalar_t>(3, -4),
      "");
}

MAYBE_GLOBAL void test_arithmetic() {
  test_arithmetic_<float>();
  test_arithmetic_<double>();
}

template <typename T, typename int_t>
void test_binary_ops_for_int_type_(T real, T img, int_t num) {
  c10::complex<T> c(real, img);
  ASSERT_EQ(c + num, c10::complex<T>(real + num, img));
  ASSERT_EQ(num + c, c10::complex<T>(num + real, img));
  ASSERT_EQ(c - num, c10::complex<T>(real - num, img));
  ASSERT_EQ(num - c, c10::complex<T>(num - real, -img));
  ASSERT_EQ(c * num, c10::complex<T>(real * num, img * num));
  ASSERT_EQ(num * c, c10::complex<T>(num * real, num * img));
  ASSERT_EQ(c / num, c10::complex<T>(real / num, img / num));
  ASSERT_EQ(
      num / c,
      c10::complex<T>(num * real / std::norm(c), -num * img / std::norm(c)));
}

template <typename T>
void test_binary_ops_for_all_int_types_(T real, T img, int8_t i) {
  test_binary_ops_for_int_type_<T, int8_t>(real, img, i);
  test_binary_ops_for_int_type_<T, int16_t>(real, img, i);
  test_binary_ops_for_int_type_<T, int32_t>(real, img, i);
  test_binary_ops_for_int_type_<T, int64_t>(real, img, i);
}

TEST(TestArithmeticIntScalar, All) {
  test_binary_ops_for_all_int_types_<float>(1.0, 0.1, 1);
  test_binary_ops_for_all_int_types_<double>(-1.3, -0.2, -2);
}

} // namespace arithmetic

namespace equality {

template <typename scalar_t>
C10_HOST_DEVICE void test_equality_() {
  static_assert(
      c10::complex<scalar_t>(1, 2) == c10::complex<scalar_t>(1, 2), "");
  static_assert(c10::complex<scalar_t>(1, 0) == scalar_t(1), "");
  static_assert(scalar_t(1) == c10::complex<scalar_t>(1, 0), "");
  static_assert(
      c10::complex<scalar_t>(1, 2) != c10::complex<scalar_t>(3, 4), "");
  static_assert(c10::complex<scalar_t>(1, 2) != scalar_t(1), "");
  static_assert(scalar_t(1) != c10::complex<scalar_t>(1, 2), "");
}

MAYBE_GLOBAL void test_equality() {
  test_equality_<float>();
  test_equality_<double>();
}

} // namespace equality

namespace io {

template <typename scalar_t>
void test_io_() {
  std::stringstream ss;
  c10::complex<scalar_t> a(1, 2);
  ss << a;
  ASSERT_EQ(ss.str(), "(1,2)");
  ss.str("(3,4)");
  ss >> a;
  ASSERT_TRUE(a == c10::complex<scalar_t>(3, 4));
}

TEST(TestIO, All) {
  test_io_<float>();
  test_io_<double>();
}

} // namespace io

namespace test_std {

template <typename scalar_t>
C10_HOST_DEVICE void test_callable_() {
  static_assert(std::real(c10::complex<scalar_t>(1, 2)) == scalar_t(1), "");
  static_assert(std::imag(c10::complex<scalar_t>(1, 2)) == scalar_t(2), "");
  std::abs(c10::complex<scalar_t>(1, 2));
  std::arg(c10::complex<scalar_t>(1, 2));
  static_assert(std::norm(c10::complex<scalar_t>(3, 4)) == scalar_t(25), "");
  static_assert(
      std::conj(c10::complex<scalar_t>(3, 4)) == c10::complex<scalar_t>(3, -4),
      "");
  c10::polar(float(1), float(PI / 2));
  c10::polar(double(1), double(PI / 2));
}

MAYBE_GLOBAL void test_callable() {
  test_callable_<float>();
  test_callable_<double>();
}

template <typename scalar_t>
void test_values_() {
  ASSERT_EQ(std::abs(c10::complex<scalar_t>(3, 4)), scalar_t(5));
  ASSERT_LT(std::abs(std::arg(c10::complex<scalar_t>(0, 1)) - PI / 2), 1e-6);
  ASSERT_LT(
      std::abs(
          c10::polar(scalar_t(1), scalar_t(PI / 2)) -
          c10::complex<scalar_t>(0, 1)),
      1e-6);
}

TEST(TestStd, BasicFunctions) {
  test_values_<float>();
  test_values_<double>();
  // CSQRT edge cases: checks for overflows which are likely to occur
  // if square root is computed using polar form
  ASSERT_LT(
      std::abs(std::sqrt(c10::complex<float>(-1e20, -4988429.2)).real()), 3e-4);
  ASSERT_LT(
      std::abs(std::sqrt(c10::complex<double>(-1e60, -4988429.2)).real()),
      3e-4);
}

} // namespace test_std