Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 186 lines (167 sloc) 6.919 kb
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
#include <map>
#include <set>
#include <memory>
#include <cctype>
#include <atomic>
#include <vector>
#include <string>
#include <cstdint>
#include <cstring>
#include <sstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <stdexcept>

#include "test.hpp"

#include "cppa/atom.hpp"
#include "cppa/announce.hpp"
#include "cppa/any_tuple.hpp"
#include "cppa/serializer.hpp"
#include "cppa/deserializer.hpp"
#include "cppa/uniform_type_info.hpp"
#include "cppa/detail/types_array.hpp"
#include "cppa/network/message_header.hpp"

#include "cppa/util/callable_trait.hpp"

using std::cout;
using std::endl;

namespace {

struct foo {
    int value;
    explicit foo(int val = 0) : value(val) { }
};

inline bool operator==(const foo& lhs, const foo& rhs) {
    return lhs.value == rhs.value;
}

} // namespace <anonymous>

using namespace cppa;

int main() {
    CPPA_TEST(test__uniform_type);
    bool announce1 = announce<foo>(&foo::value);
    bool announce2 = announce<foo>(&foo::value);
    bool announce3 = announce<foo>(&foo::value);
    bool announce4 = announce<foo>(&foo::value);
    {
        //bar.create_object();
        object obj1 = uniform_typeid<foo>()->create();
        object obj2(obj1);
        CPPA_CHECK(obj1 == obj2);
        get_ref<foo>(obj1).value = 42;
        CPPA_CHECK(obj1 != obj2);
        CPPA_CHECK_EQUAL(get<foo>(obj1).value, 42);
        CPPA_CHECK_EQUAL(get<foo>(obj2).value, 0);
    }
    auto int_val = [](bool val) { return val ? 1 : 0; };
    int successful_announces = int_val(announce1)
                               + int_val(announce2)
                               + int_val(announce3)
                               + int_val(announce4);
    CPPA_CHECK_EQUAL(successful_announces, 1);
    // these types (and only those) are present if
    // the uniform_type_info implementation is correct
    std::set<std::string> expected = {
        "bool",
        "@_::foo", // <anonymous namespace>::foo
        "@i8", "@i16", "@i32", "@i64", // signed integer names
        "@u8", "@u16", "@u32", "@u64", // unsigned integer names
        "@str", "@u16str", "@u32str", // strings
        "@strmap", // string containers
        "float", "double", "long double", // floating points
        "@0", // cppa::util::void_type
        // default announced cppa types
        "@atom", // cppa::atom_value
        "@<>", // cppa::any_tuple
        "@hdr", // cppa::detail::addressed_message
        "@actor", // cppa::actor_ptr
        "@group", // cppa::group_ptr
        "@channel", // cppa::channel_ptr
        "@process_info", // cppa::intrusive_ptr<cppa::process_information>
        "cppa::util::duration"
    };
    // holds the type names we see at runtime
    std::set<std::string> found;
    // fetch all available type names
    auto types = uniform_type_info::instances();
    for (auto tinfo : types) {
        found.insert(tinfo->name());
    }
    // compare the two sets
    CPPA_CHECK_EQUAL(expected.size(), found.size());
    bool expected_equals_found = false;
    if (expected.size() == found.size()) {
        expected_equals_found = std::equal(found.begin(),
                                           found.end(),
                                           expected.begin());
        CPPA_CHECK(expected_equals_found);
    }
    if (!expected_equals_found) {
        cout << "found:" << endl;
        for (const std::string& tname : found) {
            cout << " - " << tname << endl;
        }
        cout << "expected: " << endl;
        for (const std::string& tname : expected) {
            cout << " - " << tname << endl;
        }
    }

    // check if static types are identical to runtime types
    auto& sarr = detail::static_types_array<
                    std::int8_t, std::int16_t, std::int32_t, std::int64_t,
                    std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t,
                    std::string, std::u16string, std::u32string,
                    float, double,
                    atom_value, any_tuple, network::message_header,
                    actor_ptr, group_ptr,
                    channel_ptr, intrusive_ptr<process_information>
                 >::arr;

    std::vector<const uniform_type_info*> rarr{
        uniform_typeid<std::int8_t>(),
        uniform_typeid<std::int16_t>(),
        uniform_typeid<std::int32_t>(),
        uniform_typeid<std::int64_t>(),
        uniform_typeid<std::uint8_t>(),
        uniform_typeid<std::uint16_t>(),
        uniform_typeid<std::uint32_t>(),
        uniform_typeid<std::uint64_t>(),
        uniform_typeid<std::string>(),
        uniform_typeid<std::u16string>(),
        uniform_typeid<std::u32string>(),
        uniform_typeid<float>(),
        uniform_typeid<double>(),
        uniform_typeid<atom_value>(),
        uniform_typeid<any_tuple>(),
        uniform_typeid<network::message_header>(),
        uniform_typeid<actor_ptr>(),
        uniform_typeid<group_ptr>(),
        uniform_typeid<channel_ptr>(),
        uniform_typeid<intrusive_ptr<process_information> >()
    };

    CPPA_CHECK_EQUAL(true, sarr.is_pure());

    for (size_t i = 0; i < sarr.size; ++i) {
        CPPA_CHECK_EQUAL(sarr[i]->name(), rarr[i]->name());
        CPPA_CHECK(sarr[i] == rarr[i]);
    }

    auto& arr0 = detail::static_types_array<atom_value, std::uint32_t>::arr;
    CPPA_CHECK(arr0.is_pure());
    CPPA_CHECK(arr0[0] == uniform_typeid<atom_value>());
    CPPA_CHECK(arr0[0] == uniform_type_info::from("@atom"));
    CPPA_CHECK(arr0[1] == uniform_typeid<std::uint32_t>());
    CPPA_CHECK(arr0[1] == uniform_type_info::from("@u32"));
    CPPA_CHECK(uniform_type_info::from("@u32") == uniform_typeid<std::uint32_t>());

    auto& arr1 = detail::static_types_array<std::string, std::int8_t>::arr;
    CPPA_CHECK(arr1[0] == uniform_typeid<std::string>());
    CPPA_CHECK(arr1[0] == uniform_type_info::from("@str"));
    CPPA_CHECK(arr1[1] == uniform_typeid<std::int8_t>());
    CPPA_CHECK(arr1[1] == uniform_type_info::from("@i8"));

    auto& arr2 = detail::static_types_array<std::uint8_t, std::int8_t>::arr;
    CPPA_CHECK(arr2[0] == uniform_typeid<std::uint8_t>());
    CPPA_CHECK(arr2[0] == uniform_type_info::from("@u8"));
    CPPA_CHECK(arr2[1] == uniform_typeid<std::int8_t>());
    CPPA_CHECK(arr2[1] == uniform_type_info::from("@i8"));

    auto& arr3 = detail::static_types_array<atom_value, std::uint16_t>::arr;
    CPPA_CHECK(arr3[0] == uniform_typeid<atom_value>());
    CPPA_CHECK(arr3[0] == uniform_type_info::from("@atom"));
    CPPA_CHECK(arr3[1] == uniform_typeid<std::uint16_t>());
    CPPA_CHECK(arr3[1] == uniform_type_info::from("@u16"));
    CPPA_CHECK(uniform_type_info::from("@u16") == uniform_typeid<std::uint16_t>());

    return CPPA_TEST_RESULT;
}
Something went wrong with that request. Please try again.