Skip to content

Commit

Permalink
add tests for bits and color
Browse files Browse the repository at this point in the history
  • Loading branch information
pit-ray committed Jul 22, 2024
1 parent 347d1fb commit dc9702c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
14 changes: 11 additions & 3 deletions include/fluent_tray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ typedef enum {
#include <string>
#include <vector>

#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif


/**
* @namespace fluent_tray
Expand Down Expand Up @@ -191,8 +195,8 @@ namespace fluent_tray
static constexpr auto bits = type2bit<OutType>() ;
static const auto lower_mask = util::bit2mask(bits) ;

upper = static_cast<OutType>(reinterpret_cast<std::size_t>(input) >> bits) ;
lower = static_cast<OutType>(reinterpret_cast<std::size_t>(input) & lower_mask) ;
upper = static_cast<OutType>((std::size_t)(input) >> bits) ;
lower = static_cast<OutType>((std::size_t)(input) & lower_mask) ;
}

/**
Expand All @@ -209,7 +213,7 @@ namespace fluent_tray

auto out_upper = static_cast<std::size_t>(upper) << bits ;
auto out_lower = static_cast<std::size_t>(lower) & lower_mask ;
out = reinterpret_cast<OutType>(out_upper | out_lower) ;
out = (OutType)(out_upper | out_lower) ;
}
/**
* @brief Calculate grayscale value from RGB
Expand Down Expand Up @@ -1685,4 +1689,8 @@ namespace fluent_tray
unsigned int FluentTray::message_id_ ;
}

#if defined(__GNUC__)
#pragma GCC diagnostic warning "-Wold-style-cast"
#endif

#endif
8 changes: 5 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ function(AddTest TEST_NAME)
)
endfunction()

AddTest(string_test string_test.cpp)
AddTest(menu_test menu_test.cpp)
AddTest(tray_test tray_test.cpp)
AddTest(test_string test_string.cpp)
AddTest(test_menu test_menu.cpp)
AddTest(test_tray test_tray.cpp)
AddTest(test_bits test_bits.cpp)
AddTest(test_color test_color.cpp)

set(
CMAKE_CTEST_ARGUMENTS
Expand Down
65 changes: 65 additions & 0 deletions tests/test_bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "test.hpp"

using namespace fluent_tray ;


TEST_CASE("Bits test: ") {
SUBCASE("bit2mask") {
CHECK_EQ(util::bit2mask(1), 0b1) ;
CHECK_EQ(util::bit2mask(2), 0b11) ;
CHECK_EQ(util::bit2mask(3), 0b111) ;
CHECK_EQ(util::bit2mask(4), 0b1111) ;
CHECK_EQ(util::bit2mask(5), 0b11111) ;
CHECK_EQ(util::bit2mask(6), 0b111111) ;
CHECK_EQ(util::bit2mask(7), 0b1111111) ;
CHECK_EQ(util::bit2mask(8), 0b11111111) ;
CHECK_EQ(util::bit2mask(9), 0b111111111) ;
CHECK_EQ(util::bit2mask(10), 0b1111111111) ;
CHECK_EQ(util::bit2mask(11), 0b11111111111) ;
CHECK_EQ(util::bit2mask(12), 0b111111111111) ;
CHECK_EQ(util::bit2mask(13), 0b1111111111111) ;
CHECK_EQ(util::bit2mask(14), 0b11111111111111) ;
CHECK_EQ(util::bit2mask(15), 0b111111111111111) ;
CHECK_EQ(util::bit2mask(16), 0b1111111111111111) ;
CHECK_EQ(util::bit2mask(17), 0b11111111111111111) ;
CHECK_EQ(util::bit2mask(18), 0b111111111111111111) ;
CHECK_EQ(util::bit2mask(19), 0b1111111111111111111) ;
CHECK_EQ(util::bit2mask(20), 0b11111111111111111111) ;
CHECK_EQ(util::bit2mask(21), 0b111111111111111111111) ;
CHECK_EQ(util::bit2mask(22), 0b1111111111111111111111) ;
CHECK_EQ(util::bit2mask(23), 0b11111111111111111111111) ;
CHECK_EQ(util::bit2mask(24), 0b111111111111111111111111) ;
CHECK_EQ(util::bit2mask(25), 0b1111111111111111111111111) ;
CHECK_EQ(util::bit2mask(26), 0b11111111111111111111111111) ;
CHECK_EQ(util::bit2mask(27), 0b111111111111111111111111111) ;
CHECK_EQ(util::bit2mask(28), 0b1111111111111111111111111111) ;
CHECK_EQ(util::bit2mask(29), 0b11111111111111111111111111111) ;
CHECK_EQ(util::bit2mask(30), 0b111111111111111111111111111111) ;
CHECK_EQ(util::bit2mask(31), 0b1111111111111111111111111111111) ;
CHECK_EQ(util::bit2mask(32), 0b11111111111111111111111111111111) ;
}

SUBCASE("type2bit") {
CHECK_EQ(util::type2bit<std::uint32_t>(), 32) ;
}

SUBCASE("split_bits") {
std::uint32_t value = 0x11f97892 ;
std::uint16_t upper, lower ;

util::split_bits(value, upper, lower) ;

CHECK_EQ(upper, 0x11f9) ;
CHECK_EQ(lower, 0x7892) ;
}

SUBCASE("concatenate_bits") {
std::uint16_t upper = 0x112f ;
std::uint16_t lower = 0x88fc ;
std::uint32_t value ;

util::concatenate_bits(upper, lower, value) ;

CHECK_EQ(value, 0x112f88fc) ;
}
}
9 changes: 9 additions & 0 deletions tests/test_color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "test.hpp"

using namespace fluent_tray ;

TEST_CASE("Color test: ") {
auto color = RGB(124, 214, 122) ;
unsigned char expected_color = 188 ;
CHECK_EQ(util::rgb2gray(color), expected_color) ;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit dc9702c

Please sign in to comment.