/cosmosis/datablock/c_datablock_multidim_complex_array_test.c

https://bitbucket.org/joezuntz/cosmosis · C · 92 lines · 82 code · 1 blank · 9 comment · 49 complexity · c38447da6d5024f31eeab96df712ec9a MD5 · raw file

  1. #include "c_datablock.h"
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <float.h>
  5. #include <complex.h>
  6. #include <stdio.h>
  7. int main()
  8. {
  9. /* Array of two elements, each is an array of 3 complexes. */
  10. double complex e_2_3[2][3] = { {1.25 - 0.5 * _Complex_I, 2.0 + 3.5 * _Complex_I, -0.5},
  11. {3.5 * _Complex_I, -5.0 + 1.0e-6 * _Complex_I, 5.5 }
  12. };
  13. /* Array of three elements, each is an array of 2 complexes. */
  14. double complex e_3_2[3][2] = { {1.25 - 0.5 * _Complex_I, 2.0 + 3.5 * _Complex_I},
  15. { -0.5, 3.5 * _Complex_I},
  16. { -5.0 + 1.0e-6 * _Complex_I, 5.5}
  17. };
  18. /* First we have some tests that verify our compiler is treating
  19. multidimensional arrays according to the rules upon which we rely.
  20. */
  21. int sza = sizeof(e_2_3) / sizeof(double complex);
  22. assert(sza == 2 * 3);
  23. int szb = sizeof(e_3_2) / sizeof(double complex);
  24. assert(szb == 2 * 3);
  25. /* Make sure the two arrays give the same layout in memory */
  26. double complex* x = &(e_2_3[0][0]);
  27. double complex* y = &(e_3_2[0][0]);
  28. for (int i = 0; i != sza; ++i) { assert(x[i] == y[i]); }
  29. /* Insert a two-dimensional array into a datablock. */
  30. c_datablock* s = make_c_datablock();
  31. assert(s);
  32. const int expected_ndims = 2;
  33. int expected_extents[] = {2, 3};
  34. assert(c_datablock_put_complex_array(s, "a", "e23",
  35. (double complex*)e_2_3,
  36. expected_ndims,
  37. expected_extents) == DBS_SUCCESS);
  38. assert(c_datablock_put_complex_array(s, "a", "e23a",
  39. &(e_2_3[0][0]),
  40. expected_ndims,
  41. expected_extents) == DBS_SUCCESS);
  42. assert(c_datablock_has_value(s, "a", "e23") == true);
  43. assert(c_datablock_has_value(s, "a", "e23a"));
  44. /* Get what we just inserted */
  45. const int ndims = 2;
  46. int extents[ndims];
  47. assert(c_datablock_get_complex_array_shape(s, "a", "e23",
  48. ndims, extents) == DBS_SUCCESS);
  49. assert(extents[0] == 2);
  50. assert(extents[1] == 3);
  51. double complex xx[extents[0]][extents[1]];
  52. assert(c_datablock_get_complex_array(s, "a", "e23",
  53. (double complex*)xx, ndims, extents) == DBS_SUCCESS);
  54. for (int i = 0; i != 2; ++i)
  55. for (int j = 0; j != 3; ++j)
  56. { assert(xx[i][j] == e_2_3[i][j]); }
  57. assert(c_datablock_get_complex_array(s, "a", "e23", &(xx[0][0]), ndims, extents) == DBS_SUCCESS);
  58. for (int i = 0; i != 2; ++i)
  59. for (int j = 0; j != 3; ++j)
  60. { assert(xx[i][j] == e_2_3[i][j]); }
  61. // Test a 4-dimensional array.
  62. double complex a4d[4][3][2][5];
  63. for (int i = 0; i != 4; ++i)
  64. for (int j = 0; j != 3; ++j)
  65. for (int k = 0; k != 2; ++k)
  66. for (int l = 0; l != 5; ++l)
  67. { a4d[i][j][k][l] = (i + 1.5 * _Complex_I) * (j + 2.5) * (k + 3.5) * (l + 4.5 * _Complex_I); }
  68. assert(! c_datablock_has_value(s, "a", "a4d"));
  69. int a4d_extents[] = {4, 3, 2, 5};
  70. assert(c_datablock_put_complex_array(s, "a", "a4d", (double complex*)a4d, 4, a4d_extents)
  71. == DBS_SUCCESS);
  72. int out_extents[4];
  73. assert(c_datablock_get_complex_array_shape(s, "a", "a4d", 4, &out_extents[0])
  74. == DBS_SUCCESS);
  75. for (int i = 0; i != 4; ++i) { assert(out_extents[i] == a4d_extents[i]); }
  76. double complex out4d[out_extents[0]][out_extents[1]][out_extents[2]][out_extents[3]];
  77. assert(c_datablock_get_complex_array(s, "a", "a4d", &out4d[0][0][0][0], 3, out_extents) == DBS_NDIM_MISMATCH);
  78. assert(c_datablock_get_complex_array(s, "a", "a4d", &out4d[0][0][0][0], 5, out_extents) == DBS_NDIM_MISMATCH);
  79. int wrong_extents[] = {4, 3, 2, 4};
  80. assert(c_datablock_get_complex_array(s, "a", "a4d", &out4d[0][0][0][0], 4, wrong_extents) == DBS_EXTENTS_MISMATCH);
  81. assert(c_datablock_get_complex_array(s, "a", "a4d", &out4d[0][0][0][0], 4, out_extents) == DBS_SUCCESS);
  82. for (int i = 0; i != 4; ++i)
  83. for (int j = 0; j != 3; ++j)
  84. for (int k = 0; k != 2; ++k)
  85. for (int l = 0; l != 5; ++l)
  86. {
  87. assert(out4d[i][j][k][l] == (i + 1.5 * _Complex_I) * (j + 2.5) * (k + 3.5) * (l + 4.5 * _Complex_I));
  88. printf("out[%d][%d][%d][%d] = %f + %f I\n", i, j, k, l, creal(out4d[i][j][k][l]), cimag(out4d[i][j][k][l]));
  89. }
  90. destroy_c_datablock(s);
  91. }