#include "gate/tests.hpp"
#include "gate/console.hpp"
#include "gate/debugging.h"
using namespace gate;
GATEXX_TEST_UNIT(Console)
{
GATEXX_TEST_REQUIRE_EQUAL(Console.empty(), false);
GATEXX_TEST_REQUIRE_NOT_EQUAL(Console.c_impl(), NULL);
{
GATEXX_TEST_CHECK_NOTHROW(Console.awaitChar(0));
char_32_t receivedChar = 0;
bool_t readSucceeded = false;
try
{
readSucceeded = Console.readChar(receivedChar, 0);
}
catch (...)
{
readSucceeded = false;
}
if (readSucceeded)
{
GATEXX_TEST_CHECK(receivedChar == 0);
GATE_DEBUG_TRACE_VALUE(receivedChar);
}
char buffer[32];<--- The scope of the variable 'buffer' can be reduced. [+]The scope of the variable 'buffer' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:<--- Shadowed declaration
void f(int x)<--- Shadowed declaration
{<--- Shadowed declaration
int i = 0;<--- Shadowed declaration
if (x) {<--- Shadowed declaration
// it's safe to move 'int i = 0;' here<--- Shadowed declaration
for (int n = 0; n < 10; ++n) {<--- Shadowed declaration
// it is possible but not safe to move 'int i = 0;' here<--- Shadowed declaration
do_something(&i);<--- Shadowed declaration
}<--- Shadowed declaration
}<--- Shadowed declaration
}<--- Shadowed declaration
When you see this message it is always safe to reduce the variable scope 1 level.
<--- Shadowed declaration
try {
Console.peek(buffer, sizeof(buffer));
}
catch (...) {}
try {
gate_input_keycode_t k = 0;
char buffer[16];<--- Shadow variable
gate::size_t bufferLen = sizeof(buffer);
Console.readKey(k, receivedChar, 0, buffer, bufferLen);
}
catch (...) {}
try {
String line;
Console >> line;
}
catch (...) {}
try {
String line;
Console.readLineRaw(line, 0);
}
catch (...) {}
}
{
static char buffer[] = "Hello Console!\r\n";
gate::size_t chars_written;
GATEXX_TEST_CHECK_NOTHROW(chars_written = Console.write(buffer, sizeof(buffer) - 1));
GATEXX_TEST_CHECK(chars_written > 0);
GATEXX_TEST_CHECK_NOTHROW(chars_written = Console.writeBlock(buffer, sizeof(buffer) - 1));
GATEXX_TEST_CHECK_EQUAL(chars_written, sizeof(buffer) - 1);
GATEXX_TEST_CHECK_NOTHROW(Console.flush());
}
{
static gate::int32_t const i4 = 1;
static gate::int32_t const u4 = 2;
static gate::int32_t const i8 = 3;
static gate::int32_t const u8 = 4;
static gate::real64_t const r = 5.5;
GATEXX_TEST_CHECK_NOTHROW(Console.print(i4));
GATEXX_TEST_CHECK_NOTHROW(Console.print(u4));
GATEXX_TEST_CHECK_NOTHROW(Console.print(i8));
GATEXX_TEST_CHECK_NOTHROW(Console.print(u8));
GATEXX_TEST_CHECK_NOTHROW(Console.print(r, 0, 2, 0));
GATEXX_TEST_CHECK_NOTHROW(Console.println("."));
GATEXX_TEST_CHECK_NOTHROW(Console.flush());
}
{
static String const errMsg = String::createStaticFrom("Console Error Test");
GATEXX_TEST_CHECK_NOTHROW(Console << "Hello Console!" << strings::NewLine);
GATEXX_TEST_CHECK_NOTHROW(Console.writeErr(errMsg));
GATEXX_TEST_CHECK_NOTHROW(Console.writeErr("\r\n", 2));
GATEXX_TEST_CHECK_NOTHROW(Console.flushErr());
}
{
GATEXX_TEST_CHECK_NOTHROW(Console.getResource(GATE_STREAM_RESOURCE_INPUT));
GATEXX_TEST_CHECK_NOTHROW(Console.getResource(GATE_STREAM_RESOURCE_OUTPUT));
GATEXX_TEST_CHECK_NOTHROW(Console.getResource(GATE_STREAM_RESOURCE_ERROR));
}
}