| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "battery-capacity.h" |
| 6 |
#include "errno-util.h" |
| 7 |
#include "hashmap.h" |
| 8 |
#include "log.h" |
| 9 |
#include "tests.h" |
| 10 |
#include "time-util.h" |
| 11 |
|
| 12 |
TEST(fetch_batteries_capacity_by_name) { |
| 13 |
_cleanup_hashmap_free_ Hashmap *capacity = NULL; |
| 14 |
int r; |
| 15 |
|
| 16 |
assert_se(fetch_batteries_capacity_by_name(&capacity) >= 0); |
| 17 |
log_debug("fetch_batteries_capacity_by_name: %u entries", hashmap_size(capacity)); |
| 18 |
|
| 19 |
const char *name; |
| 20 |
void *cap; |
| 21 |
HASHMAP_FOREACH_KEY(cap, name, capacity) { |
| 22 |
assert(cap); /* Anything non-null is fine. */ |
| 23 |
log_info("Battery %s: capacity = %i", name, get_capacity_by_name(capacity, name)); |
| 24 |
} |
| 25 |
|
| 26 |
for (int i = 0; i < 2; i++) { |
| 27 |
usec_t interval; |
| 28 |
|
| 29 |
if (i > 0) |
| 30 |
sleep(1); |
| 31 |
|
| 32 |
r = get_total_suspend_interval(capacity, &interval); |
| 33 |
assert_se(r >= 0 || r == -ENOENT); |
| 34 |
log_info("%d: get_total_suspend_interval: %s", i, |
| 35 |
r < 0 ? STRERROR(r) : FORMAT_TIMESPAN(interval, USEC_PER_SEC)); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
static int intro(void) { |
| 40 |
if (getuid() != 0) |
| 41 |
log_warning("This program is unlikely to work for unprivileged users"); |
| 42 |
|
| 43 |
return EXIT_SUCCESS; |
| 44 |
} |
| 45 |
|
| 46 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); |