Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849// c++ -std=c++11 -Wall -Wextra -Werror -Isrc tests/battery_test.cpp -o /tmp/battery-test#include "battery_status.h"#include "orientation.h"#include <cassert>#include <cstring>#include <initializer_list>
int main() { using namespace battery_status; for (int raw = 0; raw < 256; ++raw) { assert(decode(0, raw) == disconnected); assert(decode(0x20, raw) == disconnected); // USB alone is not a battery. assert(decode(0x08, raw) == (raw <= 100 ? raw : unknown)); assert(decode(0x28, raw) == (raw <= 100 ? raw : unknown)); // USB + battery. } for (int status1 = 0; status1 < 256; ++status1) for (int status2 = 0; status2 < 256; ++status2) assert(charging(status1, status2) == ((status1 & 0x08) != 0 && (status2 & 0xE0) == 0x20)); assert(charging(0x28, 0x20)); // Battery + USB, actively charging. assert(!charging(0x28, 0x00)); // USB attached but idle/full. assert(!charging(0x28, 0x40)); // Discharging, despite USB flag. assert(!charging(0x20, 0x20)); // No battery: never show a bolt. char label[8]; format(label, sizeof(label), 0); assert(std::strcmp(label, "0%") == 0); format(label, sizeof(label), 85); assert(std::strcmp(label, "85%") == 0); format(label, sizeof(label), 100); assert(std::strcmp(label, "100%") == 0); format(label, sizeof(label), unknown); assert(std::strcmp(label, "--%") == 0); format(label, sizeof(label), 255); assert(std::strcmp(label, "--%") == 0); format(label, sizeof(label), disconnected); assert(std::strcmp(label, "NO BAT") == 0); for (unsigned rotation = 0; rotation < 4; ++rotation) { const int w = orientation::width(rotation), h = orientation::height(rotation); for (int percent : {0, 9, 10, 99, 100, unknown, disconnected}) { format(label, sizeof(label), percent); const auto box = bounds(w, h, std::strlen(label)); assert(box.x >= 0 && box.y >= 0); assert(box.x + box.width == w - inset); assert(box.y + box.height == h - inset); assert(box.y - 3 >= h - footer_height); const auto icon = charging_bounds(w, h, std::strlen(label)); assert(icon.x >= inset + 12); // Clear of the bottom-left activity dot. assert(icon.x + icon.width + 8 == box.x); assert(icon.y == box.y && icon.height == box.height); assert(icon.y >= h - footer_height); assert(icon.y + icon.height == h - inset); } }}