Skip to content

Commit

Permalink
test std::span<const std::complex<float>>
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Jan 9, 2023
1 parent a4a2844 commit f5e367d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion modules/c++/mem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set(MODULE_NAME mem)
coda_add_module(
${MODULE_NAME}
VERSION 1.0
DEPS sys-c++ gsl-c++ std-c++)
DEPS sys-c++ gsl-c++ coda_oss-c++ std-c++)

coda_add_tests(
MODULE_NAME ${MODULE_NAME}
Expand Down
58 changes: 58 additions & 0 deletions modules/c++/mem/include/mem/ComplexView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* =========================================================================
* This file is part of mem-c++
* =========================================================================
*
* (C) Copyright 2013 - 2014, MDA Information Systems LLC
* (C) Copyright 2023, Maxar Technologies, Inc.
*
* mem-c++ is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; If not,
* see <http://www.gnu.org/licenses/>.
*
*/

#ifndef CODA_OSS_mem_ComplexView_h_INCLUDED_
#define CODA_OSS_mem_ComplexView_h_INCLUDED_
#pragma once

#include <cstddef>
#include <vector>
#include <memory>
#include <complex>

#include "coda_oss/span.h"

namespace mem
{
/*!
* \class ComplexView
* \brief This class provides a read-only view onto a collection of complex numbers.
* For the simple case, it's std::span<const std::complex<T>>. However, sometimes
* the data is in two parallel arrays:
* const float* reals;
* const float* imags;
* This class sets things up so that clients can always use the same
* access routines, regardless of how the underlying data is actually stored.
*/
template <typename T>
struct ComplexView
{
ComplexView() = default;
~ComplexView() = default;
ComplexView(const ComplexView&) = default;
ComplexView& operator=(const ComplexView&) = default;
ComplexView(ComplexView&&) = default;
ComplexView& operator=(ComplexView&&) = default;
};

#endif // CODA_OSS_mem_ComplexView_h_INCLUDED_
22 changes: 22 additions & 0 deletions modules/c++/mem/unittests/test_vector_pointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
*
*/

#include <vector>
#include <complex>
#include <std/span>

#include <mem/VectorOfPointers.h>

#include "TestCase.h"
Expand Down Expand Up @@ -112,10 +116,28 @@ TEST_CASE(testVecOfSharedPointers)
{
std::vector<std::shared_ptr<int>> myVec2 = std_vector_shared_ptr_int(); // copy
}
}

TEST_CASE(testSpanCxFloat)
{
const std::vector<std::complex<float>> data{{1, 2}, {3, 4}, {5, 6}, {7, 8}};
const std::span<const std::complex<float>> view(data.data(), data.size());

TEST_ASSERT_EQ(data.size(), view.size());

TEST_ASSERT_EQ(view[0].real(), 1);
TEST_ASSERT_EQ(view[0].imag(), 2);
TEST_ASSERT_EQ(view[1].real(), 3);
TEST_ASSERT_EQ(view[1].imag(), 4);
TEST_ASSERT_EQ(view[2].real(), 5);
TEST_ASSERT_EQ(view[2].imag(), 6);
TEST_ASSERT_EQ(view[3].real(), 7);
TEST_ASSERT_EQ(view[3].imag(), 8);
}

TEST_MAIN(
TEST_CHECK(testVecOfRawPointers);
TEST_CHECK(testVecOfSharedPointers);

TEST_CHECK(testSpanCxFloat);
)
2 changes: 1 addition & 1 deletion modules/c++/mem/wscript
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NAME = 'mem'
VERSION = '1.0'
MODULE_DEPS = 'sys gsl std'
MODULE_DEPS = 'sys gsl coda_oss std'
TEST_DEPS = 'cli std'
UNITTEST_DEPS = 'std'

Expand Down

0 comments on commit f5e367d

Please sign in to comment.