File: units.cpp

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (133 lines) | stat: -rw-r--r-- 5,281 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
#include "utils/units.hpp"

#include "common/test.hpp"
#include "utils/units.hpp"

using namespace polybar;
using namespace units_utils;

namespace polybar {
  bool operator==(const extent_val lhs, const extent_val rhs) {
    return lhs.type == rhs.type && lhs.value == rhs.value;
  }

  bool operator==(const spacing_val lhs, const spacing_val rhs) {
    return lhs.type == rhs.type && lhs.value == rhs.value;
  }
} // namespace polybar

/**
 * \brief Class for parameterized tests on geom_format_to_pixels
 *
 * The first element in the tuple is the expected return value, the second
 * value represents the format string. The max value is always 1000 and dpi is always 96
 */
class GeomFormatToPixelsTest : public ::testing::Test,
                               public ::testing::WithParamInterface<pair<int, percentage_with_offset>> {};

vector<pair<int, percentage_with_offset>> to_pixels_no_offset_list = {
    {1000, percentage_with_offset{100.}},
    {0, percentage_with_offset{0.}},
    {1000, percentage_with_offset{150.}},
    {100, percentage_with_offset{10.}},
    {0, percentage_with_offset{0., ZERO_PX_EXTENT}},
    {1234, percentage_with_offset{0., extent_val{extent_type::PIXEL, 1234}}},
    {1, percentage_with_offset{0., extent_val{extent_type::PIXEL, 1}}},
};

vector<pair<int, percentage_with_offset>> to_pixels_with_pixels_list = {
    {1000, percentage_with_offset{100., ZERO_PX_EXTENT}},
    {1010, percentage_with_offset{100., extent_val{extent_type::PIXEL, 10}}},
    {990, percentage_with_offset{100., extent_val{extent_type::PIXEL, -10}}},
    {10, percentage_with_offset{0., extent_val{extent_type::PIXEL, 10}}},
    {1000, percentage_with_offset{99., extent_val{extent_type::PIXEL, 10}}},
    {-90, percentage_with_offset{1., extent_val{extent_type::PIXEL, -100}}},
};

vector<pair<int, percentage_with_offset>> to_pixels_with_points_list = {
    {1013, percentage_with_offset{100., extent_val{extent_type::POINT, 10}}},
    {987, percentage_with_offset{100., extent_val{extent_type::POINT, -10}}},
    {1003, percentage_with_offset{99., extent_val{extent_type::POINT, 10}}},
    {13, percentage_with_offset{0., extent_val{extent_type::POINT, 10}}},
};

INSTANTIATE_TEST_SUITE_P(NoOffset, GeomFormatToPixelsTest, ::testing::ValuesIn(to_pixels_no_offset_list));

INSTANTIATE_TEST_SUITE_P(WithPixels, GeomFormatToPixelsTest, ::testing::ValuesIn(to_pixels_with_pixels_list));

INSTANTIATE_TEST_SUITE_P(WithPoints, GeomFormatToPixelsTest, ::testing::ValuesIn(to_pixels_with_points_list));

static constexpr int MAX_WIDTH = 1000;
static constexpr int DPI = 96;

TEST_P(GeomFormatToPixelsTest, correctness) {
  int exp = GetParam().first;
  percentage_with_offset geometry = GetParam().second;
  EXPECT_DOUBLE_EQ(exp, percentage_with_offset_to_pixel(geometry, MAX_WIDTH, DPI));
}

TEST(UnitsUtils, point_to_pixel) {
  EXPECT_EQ(72, point_to_pixel(72, 72));
  EXPECT_EQ(96, point_to_pixel(72, 96));
  EXPECT_EQ(48, point_to_pixel(36, 96));
  EXPECT_EQ(-48, point_to_pixel(-36, 96));
}

TEST(UnitsUtils, extent_to_pixel) {
  EXPECT_EQ(100, extent_to_pixel_nonnegative({extent_type::PIXEL, 100}, 0));
  EXPECT_EQ(48, extent_to_pixel_nonnegative({extent_type::POINT, 36}, 96));

  EXPECT_EQ(0, extent_to_pixel_nonnegative({extent_type::PIXEL, -100}, 0));
  EXPECT_EQ(0, extent_to_pixel_nonnegative({extent_type::POINT, -36}, 96));
}

TEST(UnitsUtils, parse_extent_unit) {
  EXPECT_EQ(extent_type::PIXEL, parse_extent_unit("px"));
  EXPECT_EQ(extent_type::POINT, parse_extent_unit("pt"));

  EXPECT_EQ(extent_type::PIXEL, parse_extent_unit(""));

  EXPECT_THROW(parse_extent_unit("foo"), std::runtime_error);
}

TEST(UnitsUtils, parse_extent) {
  EXPECT_EQ((extent_val{extent_type::PIXEL, 100}), parse_extent("100px"));
  EXPECT_EQ((extent_val{extent_type::POINT, 36}), parse_extent("36pt"));

  EXPECT_EQ((extent_val{extent_type::PIXEL, -100}), parse_extent("-100px"));
  EXPECT_EQ((extent_val{extent_type::POINT, -36}), parse_extent("-36pt"));

  EXPECT_EQ((extent_val{extent_type::PIXEL, 100}), parse_extent("100"));
  EXPECT_EQ((extent_val{extent_type::PIXEL, -100}), parse_extent("-100"));

  EXPECT_THROW(parse_extent("100foo"), std::runtime_error);
}

TEST(UnitsUtils, extent_to_string) {
  EXPECT_EQ("100px", extent_to_string({extent_type::PIXEL, 100}));
  EXPECT_EQ("36pt", extent_to_string({extent_type::POINT, 36}));

  EXPECT_EQ("-100px", extent_to_string({extent_type::PIXEL, -100}));
  EXPECT_EQ("-36pt", extent_to_string({extent_type::POINT, -36}));
}

TEST(UnitsUtils, parse_spacing_unit) {
  EXPECT_EQ(spacing_type::PIXEL, parse_spacing_unit("px"));
  EXPECT_EQ(spacing_type::POINT, parse_spacing_unit("pt"));

  EXPECT_EQ(spacing_type::SPACE, parse_spacing_unit(""));

  EXPECT_THROW(parse_spacing_unit("foo"), std::runtime_error);
}

TEST(UnitsUtils, parse_spacing) {
  EXPECT_EQ((spacing_val{spacing_type::PIXEL, 100}), parse_spacing("100px"));
  EXPECT_EQ((spacing_val{spacing_type::POINT, 36}), parse_spacing("36pt"));

  EXPECT_EQ((spacing_val{spacing_type::SPACE, 100}), parse_spacing("100"));

  EXPECT_THROW(parse_spacing("-100px"), std::runtime_error);
  EXPECT_THROW(parse_spacing("-36pt"), std::runtime_error);
  EXPECT_THROW(parse_spacing("-100"), std::runtime_error);
  EXPECT_THROW(parse_spacing("100foo"), std::runtime_error);
}