1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2019 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#include "corestore_test.h"
19
20#include <platform/corestore.h>
21
22#include <atomic>
23#include <iostream>
24
25TEST_F(CoreStoreTest, test) {
26    CoreStore<std::atomic<uint32_t>> corestore;
27
28    std::cout << corestore.size() << std::endl;
29
30    size_t count = 0;
31    for (auto& e : corestore) {
32        EXPECT_EQ(0u, e.load());
33        count++;
34    }
35    EXPECT_EQ(corestore.size(), count);
36    corestore.get()++;
37    // Can't guarantee the next get would hit the same element... so check all
38    count = 0;
39    for (auto& e : corestore) {
40        if (e.load() != 0) {
41            count++;
42        }
43    }
44
45    // Expected on core slot to be non-zero
46    EXPECT_EQ(1u, count);
47}
48
49unsigned ArrayTest::cpuCount;
50unsigned ArrayTest::cpuIndex;
51
52size_t ArrayTest::getCpuIndex(size_t c) {
53    auto index = folly::AccessSpreader<ManualTag>::current(c);
54    return index;
55}
56
57int ArrayTest::testingGetCpu(unsigned* cpu,
58                             unsigned* node,
59                             void* /* unused */) {
60    if (cpu) {
61        *cpu = ArrayTest::cpuIndex;
62    }
63
64    if (node) {
65        *node = ArrayTest::cpuIndex;
66    }
67    // Return value not checked
68    return 0;
69}
70
71TEST_F(ArrayTest, testCoreArraySize) {
72    // Check we have the correct number of elements in the coreArray
73    for (int i = 0; i < 200; ++i) {
74        cpuCount = i;
75        corestore = std::make_unique<
76                CoreStore<uint8_t, &getCpuCount, &getCpuIndex>>();
77        EXPECT_EQ(getCpuCount(), corestore->size());
78    }
79}
80
81TEST_F(ArrayTest, testCpuGTCoreArraySize) {
82    cpuCount = 4;
83    corestore =
84            std::make_unique<CoreStore<uint8_t, &getCpuCount, &getCpuIndex>>();
85    for (int i = 0; i < 200; ++i) {
86        cpuIndex = i;
87        EXPECT_EQ(0, corestore->get());
88    }
89}
90