diff options
author | aqua <aqua@iserlohn-fortress.net> | 2023-03-12 14:53:27 +0200 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2023-03-12 14:53:27 +0200 |
commit | 92e4b6d5522e53e6868b9b0c52b8e54d10bbf606 (patch) | |
tree | a23bd7054b6d0fdd9703e69035cd303d6b448e35 /devices/uart/unittest_uart_16550.cc | |
parent | Move all tests next to the code they're testing (diff) | |
download | kernel-92e4b6d5522e53e6868b9b0c52b8e54d10bbf606.tar.xz |
Add unit tests for C drivers
Diffstat (limited to 'devices/uart/unittest_uart_16550.cc')
-rw-r--r-- | devices/uart/unittest_uart_16550.cc | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/devices/uart/unittest_uart_16550.cc b/devices/uart/unittest_uart_16550.cc new file mode 100644 index 0000000..560c6c0 --- /dev/null +++ b/devices/uart/unittest_uart_16550.cc @@ -0,0 +1,116 @@ +#include "sys_io.hh" +#include <gtest/gtest.h> + +using ::testing::_; +using ::testing::MockFunction; +using ::testing::Return; + +namespace k { +extern "C" { +#include "../uart.h" +#include "uart_16550.h" +} +} // namespace k + +class Uart16550 : public ::testing::Test { +protected: + void + SetUp() + { + ASSERT_FALSE(mockPort); + mockPort.reset(new MockPort); + } + void + TearDown() + { + ASSERT_TRUE(mockPort); + mockPort.reset(); + } +}; + +TEST_F(Uart16550, uart_thre) +{ + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(1).WillOnce(Return(k::THRE)); + + const auto result = uart_thre(k::COM1); + EXPECT_TRUE(result); +} + +TEST_F(Uart16550, uart_putc) +{ + k::FILE f{k::COM1}; + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(1).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb('a', k::COM1)).Times(1); + + uart_putc(&f, 'a'); +} + +TEST_F(Uart16550, uart_putc_newline) +{ + k::FILE f{k::COM1}; + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(2).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb('\n', k::COM1)).Times(1); + EXPECT_CALL(*mockPort, outb('\r', k::COM1)).Times(1); + + uart_putc(&f, '\n'); +} + +TEST_F(Uart16550, uart_puts) +{ + k::FILE f{k::COM1}; + const char *string{"This is a test string to write over uart"}; + const int length = (int)strlen(string); + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(length).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb(_, k::COM1)).Times(length); + + const auto result = uart_puts(&f, string, length); + ASSERT_EQ(result, length); +} + +TEST_F(Uart16550, uart_puts_WithUnknownLength) +{ + k::FILE f{k::COM1}; + const char *string{"This is a test string to write over uart"}; + const int length = (int)strlen(string); + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(length).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb(_, k::COM1)).Times(length); + + const auto result = uart_puts(&f, string, -1); + ASSERT_EQ(result, length); +} + +TEST_F(Uart16550, uart_puts_WithPartialLength) +{ + k::FILE f{k::COM1}; + const char *string{"This is a test string to write over uart"}; + const int length = (int)strlen(string); + const int partial = 10; + + ASSERT_LT(partial, length); + + // set up expectations + EXPECT_CALL(*mockPort, inb(k::COM1 + k::LineStatus)).Times(partial).WillRepeatedly(Return(k::THRE)); + EXPECT_CALL(*mockPort, outb(_, k::COM1)).Times(partial); + + const auto result = uart_puts(&f, string, partial); + ASSERT_EQ(result, partial); +} + +TEST_F(Uart16550, uart_flush) +{ + k::FILE f{k::COM1}; + + // set up expectations + // no mock calls are expected + + uart_flush(&f); +} |