<commit_before>// Copyright (c) 2019 by <NAME>. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "iceoryx_utils/cxx/unique_ptr.hpp"
#include "test.hpp"

#include <iostream>

using namespace ::testing;

struct Position {
    double_t x = 0.0;
    double_t y = 0.0;
    double_t z = 0.0;
};

class UniquePtrTest : public Test {

public:
    UniquePtrTest()
    {

    }

    void SetUp()
    {
    }

    void TearDown()
    {
    }

};

TEST_F(UniquePtrTest, CanBeConstructedWithUndefinedBlob)
{
    auto deleter = [](Position* const p){delete p;};
    uint8_t* buf = new uint8_t[sizeof(Position)];
    auto ptr = iox::cxx::unique_ptr<Position>(buf, deleter);

    ptr->x = 10.0;
    ptr->y = 77.77;
    ptr->z = 50.50;

    EXPECT_EQ(10.0, ptr->x);
    EXPECT_EQ(77.77, ptr->y);
    EXPECT_EQ(50.50, ptr->z);

}

TEST_F(UniquePtrTest, CanBeResetToPointToUndefinedBlob)
{

    auto deleter = [](Position* const p){delete p;};
    auto ptr = iox::cxx::unique_ptr<Position>(deleter);

    uint8_t* buf = new uint8_t[sizeof(Position)];
    ptr.reset(reinterpret_cast<Position*>(buf));

    ptr->x = 10.0;
    ptr->y = 77.77;
    ptr->z = 50.50;

    EXPECT_EQ(10.0, ptr->x);
    EXPECT_EQ(77.77, ptr->y);
    EXPECT_EQ(50.50, ptr->z);

}<commit_msg>iox-#218 Add test for deleter.<commit_after>// Copyright (c) 2019 by <NAME> GmbH. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "iceoryx_utils/cxx/unique_ptr.hpp"
#include "test.hpp"

#include <iostream>

using namespace ::testing;

struct Position {
    double_t x = 0.0;
    double_t y = 0.0;
    double_t z = 0.0;
};

class UniquePtrTest : public Test {

public:
    UniquePtrTest()
    {

    }

    void SetUp()
    {
    }

    void TearDown()
    {
    }

};

TEST_F(UniquePtrTest, CanBeConstructedWithUndefinedBlob)
{
    auto deleter = [](Position* const p){delete p;};
    uint8_t* buf = new uint8_t[sizeof(Position)];
    auto ptr = iox::cxx::unique_ptr<Position>(buf, deleter);

    ptr->x = 10.0;
    ptr->y = 77.77;
    ptr->z = 50.50;

    EXPECT_EQ(10.0, ptr->x);
    EXPECT_EQ(77.77, ptr->y);
    EXPECT_EQ(50.50, ptr->z);

}

TEST_F(UniquePtrTest, CanBeResetToPointToUndefinedBlob)
{

    auto deleter = [](Position* const p){delete p;};
    auto ptr = iox::cxx::unique_ptr<Position>(deleter);

    uint8_t* buf = new uint8_t[sizeof(Position)];
    ptr.reset(reinterpret_cast<Position*>(buf));

    ptr->x = 10.0;
    ptr->y = 77.77;
    ptr->z = 50.50;

    EXPECT_EQ(10.0, ptr->x);
    EXPECT_EQ(77.77, ptr->y);
    EXPECT_EQ(50.50, ptr->z);

}

TEST_F(UniquePtrTest, DeleterIsCalledWhenPtrGoesOutOfScope)
{
    bool deleterCalled = false;
    auto deleter = [&deleterCalled](Position* const p){
        deleterCalled = true;
        delete p;
    };

    {
        auto object = new Position();
        auto ptr = iox::cxx::unique_ptr<Position>(object, deleter);
    }

    ASSERT_EQ(true, deleterCalled);
}
{'fraction_non_alphanumeric': 0.13751263902932254, 'fraction_numerical': 0.03210313447927199, 'mean_word_length': 3.08359133126935, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2648074', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1360, 'n_words': 375}