blob: 4c3c46a8c91258db3cd7017aec5c3f0d0f86b648 [file] [log] [blame]
Andrew Walbranf3ac84d2019-07-26 16:10:02 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Walbranf3ac84d2019-07-26 16:10:02 +01007 */
8
9#include "hf/dlog.h"
10
Karl Meakin93157d02024-03-26 22:57:53 +000011#include <limits.h>
12#include <stddef.h>
13
14#include "hf/ffa.h"
15
Andrew Walbran1e7c7742019-12-13 17:10:02 +000016#include "test/hftest.h"
Andrew Walbranf3ac84d2019-07-26 16:10:02 +010017
Karl Meakin93157d02024-03-26 22:57:53 +000018#define assert_format(expected, ...) \
19 assert_format_impl(expected, sizeof(expected), __VA_ARGS__)
Andrew Walbranf3ac84d2019-07-26 16:10:02 +010020
Karl Meakin93157d02024-03-26 22:57:53 +000021static void assert_format_impl(char* expected, size_t expected_len,
22 const char* fmt, ...)
23{
24 va_list args;
25 size_t chars_written;
26
27 memset_s(dlog_buffer, DLOG_BUFFER_SIZE, 0, DLOG_BUFFER_SIZE);
28 dlog_buffer_offset = 0;
29
30 va_start(args, fmt);
31 chars_written = vdlog(fmt, args);
32 va_end(args);
33
34 ASSERT_NE(chars_written, (size_t)-1);
35 ASSERT_EQ(chars_written, expected_len - 1);
36
37 dlog_buffer[chars_written] = '\0';
38 ASSERT_STRING_EQ(expected, dlog_buffer);
39 for (size_t i = expected_len - 1; i < DLOG_BUFFER_SIZE; ++i) {
Andrew Walbranf3ac84d2019-07-26 16:10:02 +010040 EXPECT_EQ(dlog_buffer[i], '\0');
41 }
42}
Karl Meakin93157d02024-03-26 22:57:53 +000043
44/**
45 * Test formatting of a format string with no format specifiers
46 */
47TEST(dlog, no_format_specifiers)
48{
49 assert_format("Hello world\n", "Hello world\n");
50}
51
52/**
53 * Test formatting of a format string with a percent format specifier (`%%`)
54 */
55TEST(dlog, percent_format_specifier)
56{
57 assert_format("Hello %\n", "Hello %%\n");
58}
59
60/**
61 * Test formatting of a format string with a char format specifier (`%c`)
62 */
63TEST(dlog, char_format_specifier)
64{
65 assert_format("Hello w\n", "Hello %c\n", 'w');
66}
67
68/**
69 * Test formatting of a format string with a string format specifier (`%s`)
70 */
71TEST(dlog, string_format_specifier)
72{
73 assert_format("Hello world\n", "Hello %s\n", "world");
74}
75
76/**
77 * Test formatting of a format string with a `int` format specifier (`%d`/`%i`)
78 */
79TEST(dlog, int_format_specifier)
80{
81 assert_format("Hello 0\n", "Hello %i\n", 0);
82 assert_format("Hello 0\n", "Hello %d\n", 0);
83 assert_format("Hello 1234\n", "Hello %d\n", 1234);
84
85 assert_format("Hello 2147483647\n", "Hello %d\n", INT_MAX);
86 assert_format("Hello -2147483648\n", "Hello %d\n", INT_MIN);
87
88 assert_format("Hello -1\n", "Hello %d\n", UINT_MAX);
89
90 assert_format("Hello -2147483648\n", "Hello %d\n",
91 ((int64_t)INT_MAX) + 1);
92 assert_format("Hello 2147483647\n", "Hello %d\n",
93 ((int64_t)INT_MIN) - 1);
94}
95
96/**
97 * Test formatting of a format string with a `unsigned int` format specifier
98 * (`%u`)
99 */
100TEST(dlog, unsigned_int_format_specifier)
101{
102 assert_format("Hello 0\n", "Hello %u\n", 0);
103 assert_format("Hello 1234567890\n", "Hello %u\n", 1234567890);
104
105 assert_format("Hello 4294967295\n", "Hello %u\n", UINT_MAX);
Karl Meakin70894da2024-03-30 21:06:12 +0000106 assert_format("Hello 0\n", "Hello %u\n", ((uint64_t)UINT_MAX) + 1);
Karl Meakin93157d02024-03-26 22:57:53 +0000107
108 assert_format("Hello 2147483648\n", "Hello %u\n", INT_MIN);
109}
110
111/**
112 * Test formatting of a format string with an octal `unsigned int` format
113 * specifier (`%o`)
114 */
115TEST(dlog, octal_unsigned_int_format_specifier)
116{
117 assert_format("Hello 0\n", "Hello %o\n", 0);
118 assert_format("Hello 12345670\n", "Hello %o\n", 012345670);
119
120 assert_format("Hello 37777777777\n", "Hello %o\n", UINT_MAX);
Karl Meakin70894da2024-03-30 21:06:12 +0000121 assert_format("Hello 0\n", "Hello %o\n", ((uint64_t)UINT_MAX) + 1);
Karl Meakin93157d02024-03-26 22:57:53 +0000122
123 assert_format("Hello 20000000000\n", "Hello %o\n", INT_MIN);
124}
125
126/**
127 * Test formatting of a format string with a hexadecimal `unsigned int` format
128 * specifier (`%x`)
129 */
130TEST(dlog, hexadecimal_unsigned_int_format_specifier)
131{
132 assert_format("Hello 0\n", "Hello %x\n", 0);
133 assert_format("Hello 12345678\n", "Hello %x\n", 0x12345678);
134 assert_format("Hello 9abcdef\n", "Hello %x\n", 0x9abcdef);
135
136 assert_format("Hello 0x0\n", "Hello %#x\n", 0);
137 assert_format("Hello 0x12345678\n", "Hello %#x\n", 0x12345678);
138 assert_format("Hello 0x9abcdef\n", "Hello %#x\n", 0x9abcdef);
139
140 assert_format("Hello 0X0\n", "Hello %#X\n", 0);
141 assert_format("Hello 0X12345678\n", "Hello %#X\n", 0x12345678);
142 assert_format("Hello 0X9ABCDEF\n", "Hello %#X\n", 0x9abcdef);
143}
144
145/**
146 * Test formatting of a format string with a `void*` format
147 * specifier (`%p`)
148 */
149TEST(dlog, pointer_format_specifier)
150{
Karl Meakine980e612024-03-28 17:46:32 +0000151 assert_format("Hello 0x0000000000000000\n", "Hello %p\n", 0);
152 assert_format("Hello 0x123456789abcdef0\n", "Hello %p\n",
Karl Meakin93157d02024-03-26 22:57:53 +0000153 0x123456789abcdef0);
154}