Clone this repo:
  1. 1b54b0f Add new file utilities library by Jack Rosenthal · 2 years, 1 month ago factory-14162.B factory-14778.B factory-ambassador-14265.B factory-asurada-13929.B factory-brya-14517.B factory-cherry-14455.B factory-dedede-13683.B factory-drallion-13080.B factory-endeavour-13295.B factory-firmware-ti50-guc-14778.B factory-guybrush-14600.B factory-keeby-14162.B factory-kukui-14374.B factory-puff-13329.B factory-puff-13813.B factory-strongbad-13963.B factory-test-13099.17.B factory-test-13517.B factory-test-13683.B factory-trogdor-13443.B factory-volteer-13600.B factory-zork-13427.B factory-zork-13700.B firmware-asurada-13885.B firmware-brya-14505.B firmware-chameleon-14280.B firmware-cherry-14454.B firmware-cr50-stab-14294.B firmware-cr50-stab-mp-14300.B firmware-dedede-13606.B firmware-endeavour-13259.B firmware-guybrush-14500.B firmware-keeby-14119.B firmware-puff-13324.B firmware-quiche-13883.B firmware-trogdor-13577.B firmware-volteer-13521.B firmware-volteer-13672.130.B firmware-volteer-13672.148.B firmware-volteer-13672.156.B firmware-volteer-13672.39.B firmware-volteer-13672.47.B firmware-volteer-13672.81.B firmware-volteer-13672.B firmware-zork-13434.B main master release-R100-14526.B release-R101-14588.B release-R102-14695.B release-R103-14816.B release-R84-13099.B release-R85-13310.B release-R86-13421.B release-R87-13505.B release-R88-13597.B release-R89-13729.B release-R90-13816.B release-R91-13904.B release-R92-13982.B release-R93-14092.B release-R94-14150.B release-R96-14268.B release-R97-14324.B release-R98-14388.B release-R99-14469.B stabilize-13070.B stabilize-13099.101.B stabilize-13099.110.B stabilize-13099.118.B stabilize-13099.70.B stabilize-13099.72.B stabilize-13099.73.B stabilize-13099.85.B stabilize-13099.90.B stabilize-13099.94.B stabilize-13310.72.B stabilize-13310.74.B stabilize-13310.83.B stabilize-13310.94.B stabilize-13310.99.B stabilize-13360.B stabilize-13421.102.B stabilize-13421.103.B stabilize-13421.42.B stabilize-13421.53.B stabilize-13421.73.B stabilize-13421.80.B stabilize-13421.89.B stabilize-13421.96.B stabilize-13421.99.B stabilize-13482.B stabilize-13505.1.B stabilize-13505.100.B stabilize-13505.111.B stabilize-13505.63.B stabilize-13505.65.B stabilize-13505.73.B stabilize-13505.85.B stabilize-13525.B stabilize-13532.B stabilize-13591.B stabilize-13597.103.B stabilize-13597.105.B stabilize-13597.66.B stabilize-13597.67.B stabilize-13597.68.B stabilize-13597.69.B stabilize-13597.70.B stabilize-13597.71.B stabilize-13597.84.B stabilize-13597.85.B stabilize-13597.90.B stabilize-13597.94.B stabilize-13597.95.B stabilize-13597.97.B stabilize-13605.B stabilize-13654.B stabilize-13729.16.B stabilize-13729.45.B stabilize-13729.49.B stabilize-13729.56.B stabilize-13729.57.B stabilize-13729.72.B stabilize-13768.B stabilize-13799.B stabilize-13816.40.B stabilize-13816.51.B stabilize-13816.53.B stabilize-13816.55.B stabilize-13816.63.B stabilize-13816.64.B stabilize-13816.80.B stabilize-13816.82.B stabilize-13821.B stabilize-13836.B stabilize-13851.B stabilize-13856.B stabilize-13895.B stabilize-13904.34.B stabilize-13904.41.B stabilize-13904.42.B stabilize-13904.43.B stabilize-13904.44.B stabilize-13904.47.B stabilize-13904.48.B stabilize-13904.49.B stabilize-13904.55.B stabilize-13904.58.B stabilize-13904.59.B stabilize-13904.62.B stabilize-13904.66.B stabilize-13904.67.B stabilize-13935.B stabilize-13942.B stabilize-13970.B stabilize-13971.B stabilize-13974.B stabilize-13982.51.B stabilize-13982.60.B stabilize-13982.69.B stabilize-13982.70.B stabilize-13982.82.B stabilize-13982.88.B stabilize-13983.B stabilize-14013.B stabilize-14023.B stabilize-14026.B stabilize-14029.B stabilize-14031.B stabilize-14057.B stabilize-14092.57.B stabilize-14092.66.B stabilize-14093.B stabilize-14094.B stabilize-14106.B stabilize-14149.B stabilize-14150.376.B stabilize-14150.43.B stabilize-14150.64.B stabilize-14150.74.B stabilize-14151.B stabilize-14163.B stabilize-14178.B stabilize-14179.B stabilize-14189.B stabilize-14217.B stabilize-14235.B stabilize-14238.B stabilize-14249.B stabilize-14267.B stabilize-14268.51.B stabilize-14268.52.B stabilize-14268.67.B stabilize-14285.B stabilize-14312.B stabilize-14321.B stabilize-14324.13.B stabilize-14324.41.B stabilize-14324.62.B stabilize-14324.72.B stabilize-14333.B stabilize-14336.B stabilize-14345.B stabilize-14385.B stabilize-14388.52.B stabilize-14388.61.B stabilize-14388.62.B stabilize-14395.B stabilize-14396.B stabilize-14411.B stabilize-14438.B stabilize-14442.B stabilize-14469.41.B stabilize-14469.58.B stabilize-14469.8.B stabilize-14469.9.B stabilize-14477.B stabilize-14496.B stabilize-14498.B stabilize-14526.57.B stabilize-14526.67.B stabilize-14526.73.B stabilize-14526.84.B stabilize-14526.89.B stabilize-14528.B stabilize-14532.B stabilize-14536.B stabilize-14588.14.B stabilize-14588.98.B stabilize-14589.B stabilize-14616.B stabilize-14633.B stabilize-14682.B stabilize-14771.B stabilize-14790.B stabilize-14803.B stabilize-14839.B stabilize-LTS-13816.60.B stabilize-RUST-13932.B stabilize-RUST-14057.B stabilize-ambassador-13597.79.B stabilize-ambassador-14268.43.B stabilize-coil-13902.B stabilize-glibc-13901.B stabilize-lazor-13278.B stabilize-quickfix-13099.93.B stabilize-quickfix-13310.73.B stabilize-quickfix-13310.76.B stabilize-quickfix-13310.91.B stabilize-quickfix-13421.100.B stabilize-quickfix-13421.74.B stabilize-quickfix-13597.30.B stabilize-quickfix-13729.60.B stabilize-quickfix-13729.73.B stabilize-quickfix-13729.84.B stabilize-quickfix-13816.67.B stabilize-quickfix-13904.98.B stabilize-quickfix-14526.91.B stabilize-rust-13514.B stabilize-rust-13555.B stabilize-rust-13562.B stabilize-rust-13613.B stabilize-rust-13720.B stabilize-rust-13776.B stabilize-rust-13795.B stabilize-rust-13836.B stabilize-rust-14220.B stabilize-rust-14224.B stabilize-rust-14225.B stabilize-voshyr-14637.B stabilize-wristpin-14469.59.B
  2. 862e052 cmdline: add some more unit tests by Jack Rosenthal · 2 years, 2 months ago
  3. 225a168 Get lithium working under LLVM/Clang by Jack Rosenthal · 2 years, 2 months ago release-R83-13020.B stabilize-13020.55.B stabilize-13020.67.B stabilize-13020.87.B stabilize-sylas-13019.B
  4. 22c6253 Setup repo presubmit config by Jack Rosenthal · 2 years, 2 months ago
  5. 17f444f Initial import of Lithium to Chromium OS by Jack Rosenthal · 2 years, 2 months ago

Lithium C Library and Unit Test Framework

Lithium is a C library designed to help you write high quality systems programs in C. Right now, it is in the early stages and is mostly a unit testing framework, but this may change in the future as Lithium grows more features.


  • A POSIX operating system and libc.

To compile the code as a shared object file, type:

$ make build/release/

To compile the code as a static library, type:

$ make build/release/libithium.a

Distribution packages are pending which will install the library and header files.

To run the unit tests (written using Lithium Unit and Lithium Mock), type:

$ make run-tests

Lithium Unit

Lithium Unit is the core of Lithium's unit testing features. Using Lithium Unit, tests can be written anywhere, and are automatically collected by the test runner. This way, even static helper functions can even be tested. To get started, #include <lithium/unit.h> where you want to write your test, and use the DEFTEST macro to define your test. An example is below:

DEFTEST("myproject.utils.math.fibonacci", {})
        EXPECT(fib(0) == 0);
        EXPECT(fib(1) == 1);
        EXPECT(fib(11) == 89);

The second parameter to DEFTEST is an options struct, for example, to disable tests or make them informational. See the header file for all available fields.

The EXPECT macro will test that the condition is true. If it were false, it would print an error message and mark the test as failed, but continue running. This differs from the ASSERT macro, in that failed assertions will abort the test immediately.

EXPECT will return true if the condition was true: this way you can conditionally choose to run additional code based on the result.

There are 4 other macros to know:

  • EXPECT_NULL - expect that a pointer is null.
  • ASSERT_NULL - assert that a pointer is null, aborting the test if the pointer is non-null.
  • EXPECT_NOT_NULL - expect that a pointer is non-null.
  • ASSERT_NOT_NULL - assert that a pointer is non-null, aborting the test if the pointer is null.

To run your test, compile your code with -DLITHIUM_TEST_BUILD and -lithium. Call return li_unit_run_tests_main(argv); from your main function. The test runner will run all tests in parallel automatically.

Lithium Mock

Lithium takes a different approach to mocking from other test frameworks. Instead of providing specific features to mock a function, its return values, or structures, Lithium provides a single highly-powerful macro to mock any expression in your code.

This relies on providing a “mocked expression variable” which is visible to both the code being mocked and the test code providing mocked values. This variable gets compiled away to nothing and using the macros incurs no additional overhead unless LITHIUM_TEST_BUILD is defined.

First, declare the variable as extern in the header file visible to both the code being mocked and the tests providing mocked values:

#include <lithium/mock.h>

#include "myproject/utils/file.h"

/* We are mocking the "read_file" function. You can also use a data
   type here instead (e.g. "char *"). */
extern LI_MOCKABLE_STORAGE_T(read_file) read_file_mock;

You'll need to provide a definition of the variable too, outside of the header file:

LI_MOCKABLE_STORAGE_T(read_file) read_file_mock = { 0 };

Then, in place of the expression which is mockable, use the LI_MOCKABLE(storage_var, default_value) macro. For example:

if (LI_MOCKABLE(read_file_mock, read_file)(path, buffer, buf_sz) < 0) {

Finally, use LI_SETUP_MOCK(storage_var, mocked_value) in your test code:

static ssize_t mocked_read_file(const char *path, void *buf, size_t buf_sz)
        /* implementation of your mocked function ... */

DEFTEST("myproject.some.test", {})
        LI_SETUP_MOCK(read_file_mock, mocked_read_file);
        /* ... */