File: conformance_concurrent_lru_cache.cpp

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (130 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download | duplicates (6)
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
/*
    Copyright (c) 2005-2021 Intel Corporation

    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.
*/

#if __INTEL_COMPILER && _MSC_VER
#pragma warning(disable : 2586) // decorated name length exceeded, name was truncated
#endif

#define TBB_PREVIEW_CONCURRENT_LRU_CACHE 1

#include "common/test.h"
#include "oneapi/tbb/concurrent_lru_cache.h"
#include <common/concurrent_lru_cache_common.h>

//! \file conformance_concurrent_lru_cache.cpp
//! \brief Test for [preview] functionality

//-----------------------------------------------------------------------------
// Concurrent LRU Cache Tests: Cache Test Cases
//-----------------------------------------------------------------------------

//! \brief \ref interface \ref requirement
TEST_CASE("basic test for creation and use") {
    using preset = concurrent_lru_cache_presets::preset_default<int, int>;

    auto callback = [](int key) -> int { return key; };
    std::size_t number_of_lru_history_items = 8;

    preset preset_object{callback, number_of_lru_history_items};
    preset::cache_type& cache = preset_object.cache;

    int dummy_key = 0;
    preset::handle_type h = cache[dummy_key];
    int value = h.value();
    (void)value;
}

//! \brief \ref interface \ref requirement
TEST_CASE("basic test for std::move") {
    using preset = concurrent_lru_cache_presets::preset1;
    preset preset_object{};
    preset::cache_type& cache = preset_object.cache;

    // retain handle object to keep an item in the cache
    // if it is still active without aging
    preset::handle_type sheep = cache["sheep"];
    preset::handle_type horse = cache["horse"];
    preset::handle_type bull = cache["bull"];

    // store handler objects in vector
    std::vector<preset::handle_type> animals;
    animals.reserve(5);
    animals.emplace_back(std::move(sheep));
    animals.emplace_back(std::move(horse));
    animals[0] = std::move(bull);

    // after resize() vec will be full of default constructed handlers
    // with null pointers on item in cache and on cache which item belongs to
    animals.resize(10);
}

//! \brief \ref interface \ref requirement
TEST_CASE("basic test for to bool conversion") {
    using concurrent_lru_cache_presets::preset1;

    preset1 preset_instance{};
    preset1::cache_type& cache = preset_instance.cache;


    preset1::handle_type handle;
    preset1::handle_type foobar = preset1::handle_type();

    handle = cache["handle"];

    preset1::handle_type foo = cache["bar"];

    preset1::handle_type handle1(std::move(handle));

    handle = std::move(handle1);

    REQUIRE_MESSAGE(
        !preset1::handle_type(),
        "user-defined to-bool conversion does not work");
    REQUIRE_MESSAGE(
        handle,
        "user-defined to-bool conversion does not work");

    handle = preset1::handle_type();
}

//! \brief \ref requirement
TEST_CASE("basic test for cache hit") {
    using preset = concurrent_lru_cache_presets::preset_call_count<__LINE__>;
    preset preset_object{};
    preset::cache_type& cache = preset_object.cache;

    int dummy_key = 0;
    cache[dummy_key];
    cache[dummy_key];

    REQUIRE_MESSAGE(
        preset::counter_type::calls == 1,
        "value function should be called only on a cache miss");
}

//! \brief \ref requirement
TEST_CASE("basic test for unused objects") {
    using preset = concurrent_lru_cache_presets::preset_instance_count;
    preset preset_object{};

    for (std::size_t i = 0; i < preset_object.number_of_lru_history_items; ++i)
        preset_object.cache[i];

    REQUIRE_MESSAGE(
        preset_object.source.instances_count() == preset_object.number_of_lru_history_items+1,
        "cache should respect number of stored unused objects to number passed in constructor");
}