File: tutensor.cpp

package info (click to toggle)
wsclean 3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,968 kB
  • sloc: cpp: 85,742; python: 3,526; sh: 245; makefile: 21
file content (63 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download | duplicates (5)
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
#include <algorithm>
#include <cstddef>
#include <memory>
#include <complex>

#include <aocommon/xt/utensor.h>

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(utensor)

BOOST_AUTO_TEST_CASE(create_complex) {
  // This allocator wraps an std::allocator. After allocating memory, it fills
  // the allocated memory with fixed non-zero values.
  class InitializingAllocator {
   public:
    using value_type = std::complex<float>;

    value_type* allocate(std::size_t n) {
      value_type* result = allocator_.allocate(n);
      std::fill_n(result, n, value_type{42.0f, 43.0f});
      return result;
    }

    void deallocate(value_type* p, std::size_t n) {
      allocator_.deallocate(p, n);
    }

    bool operator==(const InitializingAllocator& other) const {
      return allocator_ == other.allocator_;
    }

    bool operator!=(const InitializingAllocator& other) const {
      return allocator_ != other.allocator_;
    }

   private:
    std::allocator<value_type> allocator_;
  };

  const std::size_t kTensorSize = 3;
  const std::array<std::size_t, 1> kShape{kTensorSize};
  xt::xtensor<std::complex<float>, 1, XTENSOR_DEFAULT_LAYOUT,
              InitializingAllocator>
      initialized(kShape);
  aocommon::xt::UTensor<std::complex<float>, 1, XTENSOR_DEFAULT_LAYOUT,
                        InitializingAllocator>
      uninitialized(kShape);

  for (std::size_t i = 0; i < kTensorSize; ++i) {
    // The allocator filled the allocated space with {42.0f, 43.0f}.

    // The regular 'initialized' xtensor overwrites the contents.
    BOOST_CHECK_EQUAL(initialized(i).real(), 0.0f);
    BOOST_CHECK_EQUAL(initialized(i).imag(), 0.0f);

    // The 'uninitialized' UTensor should not have overwritten the contents.
    BOOST_CHECK_EQUAL(uninitialized(i).real(), 42.0f);
    BOOST_CHECK_EQUAL(uninitialized(i).imag(), 43.0f);
  }
}

BOOST_AUTO_TEST_SUITE_END()