Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@
#
# SPDX-License-Identifier: MIT

WarningsAsErrors: '*'
WarningsAsErrors: "*"

Checks:
- clang-diagnostic-*,
- bugprone-*,
- cert-*,
- concurrency-*,
- misc-*,
- mpi-*,
- clang-analyzer-*,
# Disabled because `fprintf_s` is not available on all platforms.
- -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
name: Project's CI
on:
workflow_dispatch:
pull_request:
push:
branches:
- main
Expand Down
36 changes: 34 additions & 2 deletions include/perfect-helloworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@
#pragma once

/**
* @brief Print "Hello, World!\n" and exit
* @brief
* Result of the library function.
*/
void print_hello(void);
enum PERFECT_HELLOWORLD_RESULT {
PERFECT_HELLOWORLD_RESULT_OK = 0, //!< Success.
PERFECT_HELLOWORLD_RESULT_STDOUT_IO_ERROR, //!< Failure.
};

/**
* @brief
* Return a human-readable description of a PERFECT_HELLOWORLD_RESULT.
*
* @param res
* A result code returned by a function of this library.
*
* @return
* A pointer to a null-terminated, immutable string describing @p res.
*
* @note
* - The returned string has static storage duration and must not be freed or
* modified by the caller.
*
* - This function is thread-safe.
*
* - For unknown or unsupported values of @p res, a generic "Unknown error" string
* is returned.
*/
const char *perfect_helloworld_strerror(enum PERFECT_HELLOWORLD_RESULT res);

/**
* @brief
* Print "Hello, World!\n".
*/
enum PERFECT_HELLOWORLD_RESULT
perfect_helloworld_print_hello(void);
11 changes: 10 additions & 1 deletion src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
//
// SPDX-License-Identifier: MIT

#include <stdio.h>
#include <stdlib.h>

#include "perfect-helloworld.h"

int main(void) {
print_hello();
enum PERFECT_HELLOWORLD_RESULT res = perfect_helloworld_print_hello();
if (res != PERFECT_HELLOWORLD_RESULT_OK) {
(void)fprintf(
stderr,
"perfect_helloworld_print_hello failed: %s\n",
perfect_helloworld_strerror(res));
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
20 changes: 19 additions & 1 deletion src/lib/perfect-helloworld.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,22 @@

#include "perfect-helloworld.h"

void print_hello(void) { printf("Hello, World!\n"); }
const char *
perfect_helloworld_strerror(enum PERFECT_HELLOWORLD_RESULT res) {
switch (res) {
case PERFECT_HELLOWORLD_RESULT_OK:
return "Success";
case PERFECT_HELLOWORLD_RESULT_STDOUT_IO_ERROR:
return "I/O error while writing to standard output";
default:
return "Unknown perfect_helloworld error";
}
}

enum PERFECT_HELLOWORLD_RESULT perfect_helloworld_print_hello(void) {
if (fputs("Hello, World!\n", stdout) == EOF) {
return PERFECT_HELLOWORLD_RESULT_STDOUT_IO_ERROR;
}

return PERFECT_HELLOWORLD_RESULT_OK;
}
17 changes: 14 additions & 3 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ if get_option('tests').enabled()
unity = dependency('unity', required: get_option('tests'))

test(
'test-perfect-helloworld',
'test-perfect-helloworld-print-hello-ok',
executable(
'test-perfect-helloworld',
sources: files('test-perfect-helloworld.c'),
'test-perfect-helloworld-print-hello-ok',
sources: files('test-perfect-helloworld-print-hello-ok.c'),
link_with: lib,
dependencies: unity,
include_directories: inc,
),
)

test(
'test-perfect-helloworld-print-hello-err',
executable(
'test-perfect-helloworld-print-hello-err',
sources: files('test-perfect-helloworld-print-hello-err.c'),
link_with: lib,
dependencies: unity,
include_directories: inc,
Expand Down
31 changes: 31 additions & 0 deletions tests/test-perfect-helloworld-print-hello-err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: © 2026 Nikita Krasnov <nikita.nikita.krasnov@gmail.com>
//
// SPDX-License-Identifier: MIT

#include <stdio.h>
#include <unity.h>
#include <unity_internals.h>

#include "perfect-helloworld.h"

void setUp(void) {
// set stuff up here
}

void tearDown(void) {
// clean stuff up here
}

static void test_perfect_helloworld_print_hello_err(void) {
(void)fclose(stdout);
enum PERFECT_HELLOWORLD_RESULT res = perfect_helloworld_print_hello();
TEST_ASSERT_EQUAL(PERFECT_HELLOWORLD_RESULT_STDOUT_IO_ERROR, res);
}

int main(void) {
UNITY_BEGIN();

RUN_TEST(test_perfect_helloworld_print_hello_err);

return UNITY_END();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ void tearDown(void) {
// clean stuff up here
}

static void test_print_hello(void) {
print_hello();
static void test_perfect_helloworld_print_hello_ok(void) {
enum PERFECT_HELLOWORLD_RESULT res = perfect_helloworld_print_hello();
TEST_ASSERT_EQUAL(PERFECT_HELLOWORLD_RESULT_OK, res);
}

int main(void) {
UNITY_BEGIN();

RUN_TEST(test_print_hello);
RUN_TEST(test_perfect_helloworld_print_hello_ok);

return UNITY_END();
}