aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tests / test_fmt.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* test_fmt.c -- ck_vformat against the host libc snprintf for every |
| 18 | * conversion and flag the kernel console documents. */ |
| 19 | #include <stdint.h> |
| 20 | #include <string.h> |
| 21 | #include "ck_test.h" |
| 22 | #include "fmt.h" |
| 23 | |
| 24 | #define SAME(...) \ |
| 25 | do { \ |
| 26 | char a[256], b[256]; \ |
| 27 | int na = ck_snprintf(a, sizeof a, __VA_ARGS__); \ |
| 28 | int nb = snprintf(b, sizeof b, __VA_ARGS__); \ |
| 29 | CHECK(strcmp(a, b) == 0 && na == nb); \ |
| 30 | if (strcmp(a, b) || na != nb) \ |
| 31 | printf(" ck=\"%s\"(%d) libc=\"%s\"(%d)\n", a, na, b, nb); \ |
| 32 | } while (0) |
| 33 | |
| 34 | int main(void) |
| 35 | { |
| 36 | SAME("plain text"); |
| 37 | SAME("%s|%5s|%-5s|", "abc", "ab", "ab"); |
| 38 | SAME("%c%c", 'x', 'y'); |
| 39 | SAME("%d %d %d %i", 0, -1, 2147483647, -2147483647 - 1); |
| 40 | SAME("%u %x %X", 4294967295u, 0xdeadbeefu, 0xabcu); |
| 41 | SAME("%ld %lu %lx", -5l, 18446744073709551615ul, 0x123456789abcdeful); |
| 42 | SAME("%lld %llu %llx", -9223372036854775807ll - 1, 18446744073709551615ull, 0xfull); |
| 43 | SAME("%zu %zx", (size_t)12345, (size_t)0xfff); |
| 44 | SAME("%016llx|%08x|%-8d|%5d|%05d", 0xabcull, 0x1u, 42, -7, -7); |
| 45 | SAME("%*d|%-*d|", 6, 9, 4, 3); |
| 46 | SAME("%hhu %hu %hd", 300, 70000, -3); |
| 47 | SAME("100%%"); |
| 48 | SAME("%p", (void *)0x1234); |
| 49 | SAME("%x", 0u); |
| 50 | |
| 51 | /* NULL %s prints "(null)", truncation keeps the full return count. */ |
| 52 | char t[8]; |
| 53 | const char *volatile nul = 0; |
| 54 | CHECK(ck_snprintf(t, sizeof t, "%s", nul) == 6 && strcmp(t, "(null)") == 0); |
| 55 | CHECK(ck_snprintf(t, sizeof t, "0123456789") == 10 && strcmp(t, "0123456") == 0); |
| 56 | CHECK(ck_snprintf(t, 0, "abc") == 3); |
| 57 | return ck_t_verdict("CK_FMT"); |
| 58 | } |