File: test_xin_memory_history_manager.cpp

package info (click to toggle)
xeus 5.2.6-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,764 kB
  • sloc: cpp: 7,406; makefile: 157; python: 25
file content (147 lines) | stat: -rw-r--r-- 5,974 bytes parent folder | download | duplicates (3)
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
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht    *
*                                                                          *
* Distributed under the terms of the BSD 3-Clause License.                 *
*                                                                          *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include "doctest/doctest.h"

#include <vector>
#include <array>
#include <string>

#include "nlohmann/json.hpp"

#include "xeus/xhistory_manager.hpp"

namespace nl = nlohmann;

namespace xeus
{
    using history_manager_ptr = std::unique_ptr<xhistory_manager>;
    using history_type = std::vector<std::array<std::string, 3>>;

    TEST_SUITE("xin_memory_history_manager"){
    TEST_CASE("get_tail")
    {
        history_manager_ptr hist = xeus::make_in_memory_history_manager();
        hist->store_inputs(0, 1, "print(3)");
        hist->store_inputs(0, 2, "a = 3");
        hist->store_inputs(0, 3, "print(a)");
        hist->store_inputs(0, 4, "a");

        nl::json tail1 = hist->get_tail(1000, true, false);
        REQUIRE_EQ(tail1["status"], "ok");
        auto history1 = tail1["history"].get<history_type>();
        REQUIRE_EQ(history1.size(), std::size_t(4));
        REQUIRE_EQ(history1[0][1], "1");
        REQUIRE_EQ(history1[0][2], "print(3)");
        REQUIRE_EQ(history1[1][1], "2");
        REQUIRE_EQ(history1[1][2], "a = 3");
        REQUIRE_EQ(history1[2][1], "3");
        REQUIRE_EQ(history1[2][2], "print(a)");
        REQUIRE_EQ(history1[3][1], "4");
        REQUIRE_EQ(history1[3][2], "a");

        nl::json tail2 = hist->get_tail(2, true, false);
        REQUIRE_EQ(tail2["status"], "ok");
        auto history2 = tail2["history"].get<history_type>();
        REQUIRE_EQ(history2.size(), std::size_t(2));
        REQUIRE_EQ(history2[0][1], "3");
        REQUIRE_EQ(history2[0][2], "print(a)");
        REQUIRE_EQ(history2[1][1], "4");
        REQUIRE_EQ(history2[1][2], "a");
    }

    TEST_CASE("get_range")
    {
        history_manager_ptr hist = xeus::make_in_memory_history_manager();
        hist->store_inputs(0, 1, "print(3)");
        hist->store_inputs(0, 2, "a = 3");
        hist->store_inputs(0, 3, "print(a)");
        hist->store_inputs(0, 4, "a");

        nl::json range1 = hist->get_range(0, 1, 2, true, false);
        REQUIRE_EQ(range1["status"], "ok");
        auto history1 = range1["history"].get<history_type>();
        REQUIRE_EQ(history1.size(), std::size_t(1));
        REQUIRE_EQ(history1[0][1], "2");
        REQUIRE_EQ(history1[0][2], "a = 3");

        nl::json range2 = hist->get_range(0, 1, 3, true, false);
        REQUIRE_EQ(range2["status"], "ok");
        auto history2 = range2["history"].get<history_type>();
        REQUIRE_EQ(history2.size(), std::size_t(2));
        REQUIRE_EQ(history2[0][1], "2");
        REQUIRE_EQ(history2[0][2], "a = 3");
        REQUIRE_EQ(history2[1][1], "3");
        REQUIRE_EQ(history2[1][2], "print(a)");

        nl::json range3 = hist->get_range(0, 1000, 2, true, false);
        REQUIRE_EQ(range3["status"], "error");
    }

    TEST_CASE("search")
    {
        history_manager_ptr hist = xeus::make_in_memory_history_manager();
        hist->store_inputs(0, 1, "print(36)");
        hist->store_inputs(0, 2, "a = 3");
        hist->store_inputs(0, 3, "print(a)");
        hist->store_inputs(0, 4, "a");

        nl::json search1 = hist->search("print*", true, false, 10, false);
        REQUIRE_EQ(search1["status"], "ok");
        auto history1 = search1["history"].get<history_type>();
        REQUIRE_EQ(history1.size(), std::size_t(2));
        REQUIRE_EQ(history1[0][1], "1");
        REQUIRE_EQ(history1[0][2], "print(36)");
        REQUIRE_EQ(history1[1][1], "3");
        REQUIRE_EQ(history1[1][2], "print(a)");

        nl::json search2 = hist->search("print(*)", true, false, 10, false);
        REQUIRE_EQ(search2["status"], "ok");
        auto history2 = search2["history"].get<history_type>();
        REQUIRE_EQ(history2.size(), std::size_t(2));
        REQUIRE_EQ(history2[0][1], "1");
        REQUIRE_EQ(history2[0][2], "print(36)");
        REQUIRE_EQ(history2[1][1], "3");
        REQUIRE_EQ(history2[1][2], "print(a)");

        nl::json search3 = hist->search("print(?)", true, false, 10, false);
        REQUIRE_EQ(search3["status"], "ok");
        auto history3 = search3["history"].get<history_type>();
        REQUIRE_EQ(history3.size(), std::size_t(1));
        REQUIRE_EQ(history3[0][1], "3");
        REQUIRE_EQ(history3[0][2], "print(a)");

        nl::json search4 = hist->search("print*", true, false, 1, false);
        REQUIRE_EQ(search4["status"], "ok");
        auto history4 = search4["history"].get<history_type>();
        REQUIRE_EQ(history4.size(), std::size_t(1));
        REQUIRE_EQ(history4[0][1], "3");
        REQUIRE_EQ(history4[0][2], "print(a)");

        hist->store_inputs(0, 3, "print(a)");
        nl::json search5 = hist->search("print*", true, false, 10, false);
        REQUIRE_EQ(search5["status"], "ok");
        auto history5 = search5["history"].get<history_type>();
        REQUIRE_EQ(history5.size(), std::size_t(3));
        REQUIRE_EQ(history2[0][1], "1");
        REQUIRE_EQ(history2[0][2], "print(36)");
        REQUIRE_EQ(history5[1][1], "3");
        REQUIRE_EQ(history5[1][2], "print(a)");
        REQUIRE_EQ(history5[2][1], "3");
        REQUIRE_EQ(history5[2][2], "print(a)");

        nl::json search6 = hist->search("print*", true, false, 10, true);
        REQUIRE_EQ(search6["status"], "ok");
        auto history6 = search6["history"].get<history_type>();
        REQUIRE_EQ(history2[0][1], "1");
        REQUIRE_EQ(history2[0][2], "print(36)");
        REQUIRE_EQ(history5[1][1], "3");
        REQUIRE_EQ(history5[1][2], "print(a)");
    }
    }
}