AsmGrader 0.0.0
Loading...
Searching...
No Matches
test_context.hpp
Go to the documentation of this file.
1#pragma once
2
11#include <asmgrader/logging.hpp>
16
17#include <fmt/base.h>
18#include <fmt/format.h>
19
20#include <array>
21#include <cstddef>
22#include <cstdint>
23#include <functional>
24#include <string>
25#include <string_view>
26#include <utility>
27#include <vector>
28
29#include <sys/user.h>
30
31namespace asmgrader {
32
33class TestBase;
34
40{
41public:
42 explicit TestContext(TestBase& test, Program program,
43 std::function<void(const RequirementResult&)> on_requirement = common::noop) noexcept;
44
45 bool require(bool condition, std::string msg,
47 bool require(bool condition, RequirementResult::DebugInfo debug_info = RequirementResult::DebugInfo{});
48
52
54
56 std::string_view get_name() const;
57
63 void restart_program();
64
65 // Low-level exececutor of an arbitrary syscall
66 Result<SyscallRecord> exec_syscall(u64 sys_nr, std::array<std::uint64_t, 6> args);
67
69 std::string get_stdout();
70
72 std::string get_full_stdout();
73
76 std::size_t flush_stdin();
77
79 const std::vector<SyscallRecord>& get_syscall_records() const;
80
83
85 void send_stdin(const std::string& input);
86
89 template <typename T>
90 AsmSymbol<T> find_symbol(std::string_view name);
91
93 template <std::size_t NumBytes>
95
97 template <typename Func>
98 AsmFunction<Func> find_function(std::string name);
99
101 RunResult run();
102
103private:
104 TestBase* associated_test_;
105 Program prog_;
106
108 TestResult result_;
109
110 // ########### Callbacks
111
113 std::function<void(const RequirementResult&)> on_requirement_;
114};
115
116template <std::size_t NumBytes>
118 AsmBuffer<NumBytes> buffer{prog_};
119
120 return buffer;
121}
122
123template <typename T>
125 static_assert(MemoryReadSupported<T>, "Specified type does not have supported memory read operation! See docs.");
126
127 auto symbol = prog_.get_symtab().find(name);
128
129 if (!symbol) {
130 LOG_DEBUG("Could not resolve symbol {:?}", name);
131 return {prog_, std::string{name}, ErrorKind::UnresolvedSymbol};
132 }
133
134 return {prog_, std::string{name}, symbol->address};
135}
136
137template <typename Func>
139 auto loc = prog_.get_symtab().find(name);
140
141 if (!loc) {
142 return {prog_, std::move(name), ErrorKind::UnresolvedSymbol};
143 }
144
145 return {prog_, std::move(name), loc->address};
146}
147
148} // namespace asmgrader
Definition asm_buffer.hpp:23
Definition asm_function.hpp:18
Definition asm_symbol.hpp:21
std::variant wrapper for a partial implementation of C++23's expected type
Definition expected.hpp:34
Definition program.hpp:31
SymbolTable & get_symtab()
Definition program.cpp:75
Definition run_result.hpp:6
std::optional< Symbol > find(std::string_view name) const
Definition symbol_table.cpp:21
Base class primarily for a user-written test.
Definition test_base.hpp:17
User-facing API for use within an assignment test case for: Interacting with or querying data for the...
Definition test_context.hpp:40
TestResult finalize()
Obtain the final test results Run after the test is complete. Note: has no ill effects if run before ...
Definition test_context.cpp:52
TestContext(TestBase &test, Program program, std::function< void(const RequirementResult &)> on_requirement=common::noop) noexcept
Definition test_context.cpp:41
std::string get_full_stdout()
Get all stdout from since the beginning of the test invokation.
Definition test_context.cpp:89
RegistersState get_registers() const
Get the current register state of the program.
Definition test_context.cpp:176
std::string get_stdout()
Get any new stdout from the program since the last call to this function.
Definition test_context.cpp:85
AsmSymbol< T > find_symbol(std::string_view name)
Find a named symbol in the associated program.
Definition test_context.hpp:124
void send_stdin(const std::string &input)
Get any new stdout from the program since the last call to this function.
Definition test_context.cpp:93
AsmFunction< Func > find_function(std::string name)
Find a named function in the associated program.
Definition test_context.hpp:138
Result< SyscallRecord > exec_syscall(u64 sys_nr, std::array< std::uint64_t, 6 > args)
Definition test_context.cpp:105
void restart_program()
Restarts the entire program.
Definition test_context.cpp:101
AsmBuffer< NumBytes > create_buffer()
Create a buffer of NumBytes
Definition test_context.hpp:117
std::size_t flush_stdin()
Flushes any reamaining unread data in the stdin buffer Returns: number of bytes flushed,...
Definition test_context.cpp:113
std::string_view get_name() const
Test name getter.
Definition test_context.cpp:81
const std::vector< SyscallRecord > & get_syscall_records() const
Obtain a list of the syscalls that have been executed so far.
Definition test_context.cpp:109
RunResult run()
Run the program normally from _start
Definition test_context.cpp:97
bool require(bool condition, std::string msg, RequirementResult::DebugInfo debug_info=RequirementResult::DebugInfo{})
Definition test_context.cpp:72
Definition concepts.hpp:23
Defines data classes to store result data for the current run session.
#define LOG_DEBUG(...)
Definition logging.hpp:44
constexpr auto noop
Usefule as the default argument to function callbacks.
Definition functional.hpp:6
Definition asm_buffer.hpp:19
All header-only as this is relatively low level and we want operations to be fast (inlinable)
Definition registers_state.hpp:252
Definition grading_session.hpp:77
Definition grading_session.hpp:72
Definition grading_session.hpp:93