<commit_before>/*
 @ <NAME>
*/

#include "stdafx.h"

#include "gtest\gtest.h"
#include "kbase\memory\singleton.h"

#include <atomic>
#include <thread>
#include <vector>

namespace {

struct DataPack {
    int i;
    double d;
};

std::atomic<DataPack*> g_data_pack_ptr {nullptr};

class SingletonUser {
public:
    static SingletonUser* Instance()
    {
        return kbase::Singleton<SingletonUser>::instance();
    }

private:
    friend kbase::DefaultSingletonTraits<SingletonUser>;
    SingletonUser() {}
    ~SingletonUser() {}

public:
    int n;
};

}   // namespace

TEST(SingletonTest, Valid)
{
    kbase::AtExitManager exit_manager;
    auto i = kbase::Singleton<int>::instance();
    *i = 0x12345678;
}

TEST(SingletonTest, Uniqueness)
{
    kbase::AtExitManager exit_manager;
    std::vector<std::thread> vth;
    for (int i = 0; i < 100; ++i) {
        vth.emplace_back([](int id) {
            auto ptr = kbase::Singleton<DataPack>::instance();
            auto g_ptr = g_data_pack_ptr.load();
            if (!g_ptr) {
                std::cout << "thread " << id << " obtains singleton object first\n";
                g_data_pack_ptr.store(ptr);
            } else {
                EXPECT_EQ(ptr, g_ptr);
            }
        }, i);
        vth.back().join();
    }
}<commit_msg>add LeakyTraits test case for singleton unittest<commit_after>/*
 @ <NAME>
*/

#include "stdafx.h"

#include "gtest\gtest.h"
#include "kbase\memory\singleton.h"

#include <atomic>
#include <thread>
#include <vector>

namespace {

struct DataPack {
    int i;
    double d;
};

std::atomic<DataPack*> g_data_pack_ptr {nullptr};

class SingletonUser {
public:
    static SingletonUser* Instance()
    {
        return kbase::Singleton<SingletonUser>::instance();
    }

private:
    friend kbase::DefaultSingletonTraits<SingletonUser>;
    SingletonUser() {}
    ~SingletonUser() {}

public:
    int n;
};

}   // namespace

TEST(SingletonTest, Valid)
{
    kbase::AtExitManager exit_manager;
    auto i = kbase::Singleton<int>::instance();
    *i = 0x12345678;
}

TEST(SingletonTest, Leaky)
{
    int* i = kbase::Singleton<int, kbase::LeakySingletonTraits<int>>::instance();
    *i = 0x12345678;
    delete i;
}

TEST(SingletonTest, Uniqueness)
{
    kbase::AtExitManager exit_manager;
    std::vector<std::thread> vth;
    for (int i = 0; i < 100; ++i) {
        vth.emplace_back([](int id) {
            auto ptr = kbase::Singleton<DataPack>::instance();
            auto g_ptr = g_data_pack_ptr.load();
            if (!g_ptr) {
                std::cout << "thread " << id << " obtains singleton object first\n";
                g_data_pack_ptr.store(ptr);
            } else {
                EXPECT_EQ(ptr, g_ptr);
            }
        }, i);
        vth.back().join();
    }
}
{'fraction_non_alphanumeric': 0.14845434938892882, 'fraction_numerical': 0.012580877066858375, 'mean_word_length': 2.7305630026809653, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2545148', 'n_tokens_mistral': 996, 'n_tokens_neox': 960, 'n_words': 188}