Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
c++ source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
6502-c++ 11.1.0
ARM GCC 10.2.0
ARM GCC 10.3.0
ARM GCC 10.4.0
ARM GCC 10.5.0
ARM GCC 11.1.0
ARM GCC 11.2.0
ARM GCC 11.3.0
ARM GCC 11.4.0
ARM GCC 12.1.0
ARM GCC 12.2.0
ARM GCC 12.3.0
ARM GCC 12.4.0
ARM GCC 13.1.0
ARM GCC 13.2.0
ARM GCC 13.2.0 (unknown-eabi)
ARM GCC 13.3.0
ARM GCC 13.3.0 (unknown-eabi)
ARM GCC 14.1.0
ARM GCC 14.1.0 (unknown-eabi)
ARM GCC 14.2.0
ARM GCC 14.2.0 (unknown-eabi)
ARM GCC 4.5.4
ARM GCC 4.6.4
ARM GCC 5.4
ARM GCC 6.3.0
ARM GCC 6.4.0
ARM GCC 7.3.0
ARM GCC 7.5.0
ARM GCC 8.2.0
ARM GCC 8.5.0
ARM GCC 9.3.0
ARM GCC 9.4.0
ARM GCC 9.5.0
ARM GCC trunk
ARM gcc 10.2.1 (none)
ARM gcc 10.3.1 (2021.07 none)
ARM gcc 10.3.1 (2021.10 none)
ARM gcc 11.2.1 (none)
ARM gcc 5.4.1 (none)
ARM gcc 7.2.1 (none)
ARM gcc 8.2 (WinCE)
ARM gcc 8.3.1 (none)
ARM gcc 9.2.1 (none)
ARM msvc v19.0 (WINE)
ARM msvc v19.10 (WINE)
ARM msvc v19.14 (WINE)
ARM64 Morello gcc 10.1 Alpha 2
ARM64 gcc 10.2
ARM64 gcc 10.3
ARM64 gcc 10.4
ARM64 gcc 10.5.0
ARM64 gcc 11.1
ARM64 gcc 11.2
ARM64 gcc 11.3
ARM64 gcc 11.4.0
ARM64 gcc 12.1
ARM64 gcc 12.2.0
ARM64 gcc 12.3.0
ARM64 gcc 12.4.0
ARM64 gcc 13.1.0
ARM64 gcc 13.2.0
ARM64 gcc 13.3.0
ARM64 gcc 14.1.0
ARM64 gcc 14.2.0
ARM64 gcc 4.9.4
ARM64 gcc 5.4
ARM64 gcc 5.5.0
ARM64 gcc 6.3
ARM64 gcc 6.4
ARM64 gcc 7.3
ARM64 gcc 7.5
ARM64 gcc 8.2
ARM64 gcc 8.5
ARM64 gcc 9.3
ARM64 gcc 9.4
ARM64 gcc 9.5
ARM64 gcc trunk
ARM64 msvc v19.14 (WINE)
AVR gcc 10.3.0
AVR gcc 11.1.0
AVR gcc 12.1.0
AVR gcc 12.2.0
AVR gcc 12.3.0
AVR gcc 12.4.0
AVR gcc 13.1.0
AVR gcc 13.2.0
AVR gcc 13.3.0
AVR gcc 14.1.0
AVR gcc 14.2.0
AVR gcc 4.5.4
AVR gcc 4.6.4
AVR gcc 5.4.0
AVR gcc 9.2.0
AVR gcc 9.3.0
Arduino Mega (1.8.9)
Arduino Uno (1.8.9)
BPF clang (trunk)
BPF clang 13.0.0
BPF clang 14.0.0
BPF clang 15.0.0
BPF clang 16.0.0
BPF clang 17.0.1
BPF clang 18.1.0
BPF clang 19.1.0
EDG (experimental reflection)
EDG 6.5
EDG 6.5 (GNU mode gcc 13)
EDG 6.6
EDG 6.6 (GNU mode gcc 13)
FRC 2019
FRC 2020
FRC 2023
HPPA gcc 14.2.0
KVX ACB 4.1.0 (GCC 7.5.0)
KVX ACB 4.1.0-cd1 (GCC 7.5.0)
KVX ACB 4.10.0 (GCC 10.3.1)
KVX ACB 4.11.1 (GCC 10.3.1)
KVX ACB 4.12.0 (GCC 11.3.0)
KVX ACB 4.2.0 (GCC 7.5.0)
KVX ACB 4.3.0 (GCC 7.5.0)
KVX ACB 4.4.0 (GCC 7.5.0)
KVX ACB 4.6.0 (GCC 9.4.1)
KVX ACB 4.8.0 (GCC 9.4.1)
KVX ACB 4.9.0 (GCC 9.4.1)
KVX ACB 5.0.0 (GCC 12.2.1)
KVX ACB 5.2.0 (GCC 13.2.1)
LoongArch64 clang (trunk)
LoongArch64 clang 17.0.1
LoongArch64 clang 18.1.0
LoongArch64 clang 19.1.0
M68K gcc 13.1.0
M68K gcc 13.2.0
M68K gcc 13.3.0
M68K gcc 14.1.0
M68K gcc 14.2.0
M68k clang (trunk)
MRISC32 gcc (trunk)
MSP430 gcc 4.5.3
MSP430 gcc 5.3.0
MSP430 gcc 6.2.1
MinGW clang 14.0.3
MinGW clang 14.0.6
MinGW clang 15.0.7
MinGW clang 16.0.0
MinGW clang 16.0.2
MinGW gcc 11.3.0
MinGW gcc 12.1.0
MinGW gcc 12.2.0
MinGW gcc 13.1.0
RISC-V (32-bits) gcc (trunk)
RISC-V (32-bits) gcc 10.2.0
RISC-V (32-bits) gcc 10.3.0
RISC-V (32-bits) gcc 11.2.0
RISC-V (32-bits) gcc 11.3.0
RISC-V (32-bits) gcc 11.4.0
RISC-V (32-bits) gcc 12.1.0
RISC-V (32-bits) gcc 12.2.0
RISC-V (32-bits) gcc 12.3.0
RISC-V (32-bits) gcc 12.4.0
RISC-V (32-bits) gcc 13.1.0
RISC-V (32-bits) gcc 13.2.0
RISC-V (32-bits) gcc 13.3.0
RISC-V (32-bits) gcc 14.1.0
RISC-V (32-bits) gcc 14.2.0
RISC-V (32-bits) gcc 8.2.0
RISC-V (32-bits) gcc 8.5.0
RISC-V (32-bits) gcc 9.4.0
RISC-V (64-bits) gcc (trunk)
RISC-V (64-bits) gcc 10.2.0
RISC-V (64-bits) gcc 10.3.0
RISC-V (64-bits) gcc 11.2.0
RISC-V (64-bits) gcc 11.3.0
RISC-V (64-bits) gcc 11.4.0
RISC-V (64-bits) gcc 12.1.0
RISC-V (64-bits) gcc 12.2.0
RISC-V (64-bits) gcc 12.3.0
RISC-V (64-bits) gcc 12.4.0
RISC-V (64-bits) gcc 13.1.0
RISC-V (64-bits) gcc 13.2.0
RISC-V (64-bits) gcc 13.3.0
RISC-V (64-bits) gcc 14.1.0
RISC-V (64-bits) gcc 14.2.0
RISC-V (64-bits) gcc 8.2.0
RISC-V (64-bits) gcc 8.5.0
RISC-V (64-bits) gcc 9.4.0
RISC-V rv32gc clang (trunk)
RISC-V rv32gc clang 10.0.0
RISC-V rv32gc clang 10.0.1
RISC-V rv32gc clang 11.0.0
RISC-V rv32gc clang 11.0.1
RISC-V rv32gc clang 12.0.0
RISC-V rv32gc clang 12.0.1
RISC-V rv32gc clang 13.0.0
RISC-V rv32gc clang 13.0.1
RISC-V rv32gc clang 14.0.0
RISC-V rv32gc clang 15.0.0
RISC-V rv32gc clang 16.0.0
RISC-V rv32gc clang 17.0.1
RISC-V rv32gc clang 18.1.0
RISC-V rv32gc clang 19.1.0
RISC-V rv32gc clang 9.0.0
RISC-V rv32gc clang 9.0.1
RISC-V rv64gc clang (trunk)
RISC-V rv64gc clang 10.0.0
RISC-V rv64gc clang 10.0.1
RISC-V rv64gc clang 11.0.0
RISC-V rv64gc clang 11.0.1
RISC-V rv64gc clang 12.0.0
RISC-V rv64gc clang 12.0.1
RISC-V rv64gc clang 13.0.0
RISC-V rv64gc clang 13.0.1
RISC-V rv64gc clang 14.0.0
RISC-V rv64gc clang 15.0.0
RISC-V rv64gc clang 16.0.0
RISC-V rv64gc clang 17.0.1
RISC-V rv64gc clang 18.1.0
RISC-V rv64gc clang 19.1.0
RISC-V rv64gc clang 9.0.0
RISC-V rv64gc clang 9.0.1
Raspbian Buster
Raspbian Stretch
SPARC LEON gcc 12.2.0
SPARC LEON gcc 12.3.0
SPARC LEON gcc 12.4.0
SPARC LEON gcc 13.1.0
SPARC LEON gcc 13.2.0
SPARC LEON gcc 13.3.0
SPARC LEON gcc 14.1.0
SPARC LEON gcc 14.2.0
SPARC gcc 12.2.0
SPARC gcc 12.3.0
SPARC gcc 12.4.0
SPARC gcc 13.1.0
SPARC gcc 13.2.0
SPARC gcc 13.3.0
SPARC gcc 14.1.0
SPARC gcc 14.2.0
SPARC64 gcc 12.2.0
SPARC64 gcc 12.3.0
SPARC64 gcc 12.4.0
SPARC64 gcc 13.1.0
SPARC64 gcc 13.2.0
SPARC64 gcc 13.3.0
SPARC64 gcc 14.1.0
SPARC64 gcc 14.2.0
TI C6x gcc 12.2.0
TI C6x gcc 12.3.0
TI C6x gcc 12.4.0
TI C6x gcc 13.1.0
TI C6x gcc 13.2.0
TI C6x gcc 13.3.0
TI C6x gcc 14.1.0
TI C6x gcc 14.2.0
TI CL430 21.6.1
VAX gcc NetBSDELF 10.4.0
VAX gcc NetBSDELF 10.5.0 (Nov 15 03:50:22 2023)
WebAssembly clang (trunk)
Xtensa ESP32 gcc 11.2.0 (2022r1)
Xtensa ESP32 gcc 12.2.0 (20230208)
Xtensa ESP32 gcc 8.2.0 (2019r2)
Xtensa ESP32 gcc 8.2.0 (2020r1)
Xtensa ESP32 gcc 8.2.0 (2020r2)
Xtensa ESP32 gcc 8.4.0 (2020r3)
Xtensa ESP32 gcc 8.4.0 (2021r1)
Xtensa ESP32 gcc 8.4.0 (2021r2)
Xtensa ESP32-S2 gcc 11.2.0 (2022r1)
Xtensa ESP32-S2 gcc 12.2.0 (20230208)
Xtensa ESP32-S2 gcc 8.2.0 (2019r2)
Xtensa ESP32-S2 gcc 8.2.0 (2020r1)
Xtensa ESP32-S2 gcc 8.2.0 (2020r2)
Xtensa ESP32-S2 gcc 8.4.0 (2020r3)
Xtensa ESP32-S2 gcc 8.4.0 (2021r1)
Xtensa ESP32-S2 gcc 8.4.0 (2021r2)
Xtensa ESP32-S3 gcc 11.2.0 (2022r1)
Xtensa ESP32-S3 gcc 12.2.0 (20230208)
Xtensa ESP32-S3 gcc 8.4.0 (2020r3)
Xtensa ESP32-S3 gcc 8.4.0 (2021r1)
Xtensa ESP32-S3 gcc 8.4.0 (2021r2)
arm64 msvc v19.20 VS16.0
arm64 msvc v19.21 VS16.1
arm64 msvc v19.22 VS16.2
arm64 msvc v19.23 VS16.3
arm64 msvc v19.24 VS16.4
arm64 msvc v19.25 VS16.5
arm64 msvc v19.27 VS16.7
arm64 msvc v19.28 VS16.8
arm64 msvc v19.28 VS16.9
arm64 msvc v19.29 VS16.10
arm64 msvc v19.29 VS16.11
arm64 msvc v19.30 VS17.0
arm64 msvc v19.31 VS17.1
arm64 msvc v19.32 VS17.2
arm64 msvc v19.33 VS17.3
arm64 msvc v19.34 VS17.4
arm64 msvc v19.35 VS17.5
arm64 msvc v19.36 VS17.6
arm64 msvc v19.37 VS17.7
arm64 msvc v19.38 VS17.8
arm64 msvc v19.39 VS17.9
arm64 msvc v19.40 VS17.10
arm64 msvc v19.latest
armv7-a clang (trunk)
armv7-a clang 10.0.0
armv7-a clang 10.0.1
armv7-a clang 11.0.0
armv7-a clang 11.0.1
armv7-a clang 12.0.0
armv7-a clang 12.0.1
armv7-a clang 13.0.0
armv7-a clang 13.0.1
armv7-a clang 14.0.0
armv7-a clang 15.0.0
armv7-a clang 16.0.0
armv7-a clang 17.0.1
armv7-a clang 18.1.0
armv7-a clang 19.1.0
armv7-a clang 9.0.0
armv7-a clang 9.0.1
armv8-a clang (all architectural features, trunk)
armv8-a clang (trunk)
armv8-a clang 10.0.0
armv8-a clang 10.0.1
armv8-a clang 11.0.0
armv8-a clang 11.0.1
armv8-a clang 12.0.0
armv8-a clang 13.0.0
armv8-a clang 14.0.0
armv8-a clang 15.0.0
armv8-a clang 16.0.0
armv8-a clang 17.0.1
armv8-a clang 18.1.0
armv8-a clang 19.1.0
armv8-a clang 9.0.0
armv8-a clang 9.0.1
clang-cl 18.1.0
ellcc 0.1.33
ellcc 0.1.34
ellcc 2017-07-16
hexagon-clang 16.0.5
llvm-mos atari2600-3e
llvm-mos atari2600-4k
llvm-mos atari2600-common
llvm-mos atari5200-supercart
llvm-mos atari8-cart-megacart
llvm-mos atari8-cart-std
llvm-mos atari8-cart-xegs
llvm-mos atari8-common
llvm-mos atari8-dos
llvm-mos c128
llvm-mos c64
llvm-mos commodore
llvm-mos cpm65
llvm-mos cx16
llvm-mos dodo
llvm-mos eater
llvm-mos mega65
llvm-mos nes
llvm-mos nes-action53
llvm-mos nes-cnrom
llvm-mos nes-gtrom
llvm-mos nes-mmc1
llvm-mos nes-mmc3
llvm-mos nes-nrom
llvm-mos nes-unrom
llvm-mos nes-unrom-512
llvm-mos osi-c1p
llvm-mos pce
llvm-mos pce-cd
llvm-mos pce-common
llvm-mos pet
llvm-mos rp6502
llvm-mos rpc8e
llvm-mos supervision
llvm-mos vic20
loongarch64 gcc 12.2.0
loongarch64 gcc 12.3.0
loongarch64 gcc 12.4.0
loongarch64 gcc 13.1.0
loongarch64 gcc 13.2.0
loongarch64 gcc 13.3.0
loongarch64 gcc 14.1.0
loongarch64 gcc 14.2.0
mips clang 13.0.0
mips clang 14.0.0
mips clang 15.0.0
mips clang 16.0.0
mips clang 17.0.1
mips clang 18.1.0
mips clang 19.1.0
mips gcc 11.2.0
mips gcc 12.1.0
mips gcc 12.2.0
mips gcc 12.3.0
mips gcc 12.4.0
mips gcc 13.1.0
mips gcc 13.2.0
mips gcc 13.3.0
mips gcc 14.1.0
mips gcc 14.2.0
mips gcc 4.9.4
mips gcc 5.4
mips gcc 5.5.0
mips gcc 9.3.0 (codescape)
mips gcc 9.5.0
mips64 (el) gcc 12.1.0
mips64 (el) gcc 12.2.0
mips64 (el) gcc 12.3.0
mips64 (el) gcc 12.4.0
mips64 (el) gcc 13.1.0
mips64 (el) gcc 13.2.0
mips64 (el) gcc 13.3.0
mips64 (el) gcc 14.1.0
mips64 (el) gcc 14.2.0
mips64 (el) gcc 4.9.4
mips64 (el) gcc 5.4.0
mips64 (el) gcc 5.5.0
mips64 (el) gcc 9.5.0
mips64 clang 13.0.0
mips64 clang 14.0.0
mips64 clang 15.0.0
mips64 clang 16.0.0
mips64 clang 17.0.1
mips64 clang 18.1.0
mips64 clang 19.1.0
mips64 gcc 11.2.0
mips64 gcc 12.1.0
mips64 gcc 12.2.0
mips64 gcc 12.3.0
mips64 gcc 12.4.0
mips64 gcc 13.1.0
mips64 gcc 13.2.0
mips64 gcc 13.3.0
mips64 gcc 14.1.0
mips64 gcc 14.2.0
mips64 gcc 4.9.4
mips64 gcc 5.4.0
mips64 gcc 5.5.0
mips64 gcc 9.5.0
mips64el clang 13.0.0
mips64el clang 14.0.0
mips64el clang 15.0.0
mips64el clang 16.0.0
mips64el clang 17.0.1
mips64el clang 18.1.0
mips64el clang 19.1.0
mipsel clang 13.0.0
mipsel clang 14.0.0
mipsel clang 15.0.0
mipsel clang 16.0.0
mipsel clang 17.0.1
mipsel clang 18.1.0
mipsel clang 19.1.0
mipsel gcc 12.1.0
mipsel gcc 12.2.0
mipsel gcc 12.3.0
mipsel gcc 12.4.0
mipsel gcc 13.1.0
mipsel gcc 13.2.0
mipsel gcc 13.3.0
mipsel gcc 14.1.0
mipsel gcc 14.2.0
mipsel gcc 4.9.4
mipsel gcc 5.4.0
mipsel gcc 5.5.0
mipsel gcc 9.5.0
nanoMIPS gcc 6.3.0 (mtk)
power gcc 11.2.0
power gcc 12.1.0
power gcc 12.2.0
power gcc 12.3.0
power gcc 12.4.0
power gcc 13.1.0
power gcc 13.2.0
power gcc 13.3.0
power gcc 14.1.0
power gcc 14.2.0
power gcc 4.8.5
power64 AT12.0 (gcc8)
power64 AT13.0 (gcc9)
power64 gcc 11.2.0
power64 gcc 12.1.0
power64 gcc 12.2.0
power64 gcc 12.3.0
power64 gcc 12.4.0
power64 gcc 13.1.0
power64 gcc 13.2.0
power64 gcc 13.3.0
power64 gcc 14.1.0
power64 gcc 14.2.0
power64 gcc trunk
power64le AT12.0 (gcc8)
power64le AT13.0 (gcc9)
power64le clang (trunk)
power64le gcc 11.2.0
power64le gcc 12.1.0
power64le gcc 12.2.0
power64le gcc 12.3.0
power64le gcc 12.4.0
power64le gcc 13.1.0
power64le gcc 13.2.0
power64le gcc 13.3.0
power64le gcc 14.1.0
power64le gcc 14.2.0
power64le gcc 6.3.0
power64le gcc trunk
powerpc64 clang (trunk)
qnx 8.0.0
s390x gcc 11.2.0
s390x gcc 12.1.0
s390x gcc 12.2.0
s390x gcc 12.3.0
s390x gcc 12.4.0
s390x gcc 13.1.0
s390x gcc 13.2.0
s390x gcc 13.3.0
s390x gcc 14.1.0
s390x gcc 14.2.0
sh gcc 12.2.0
sh gcc 12.3.0
sh gcc 12.4.0
sh gcc 13.1.0
sh gcc 13.2.0
sh gcc 13.3.0
sh gcc 14.1.0
sh gcc 14.2.0
sh gcc 4.9.4
sh gcc 9.5.0
vast (trunk)
x64 msvc v19.0 (WINE)
x64 msvc v19.10 (WINE)
x64 msvc v19.14 (WINE)
x64 msvc v19.20 VS16.0
x64 msvc v19.21 VS16.1
x64 msvc v19.22 VS16.2
x64 msvc v19.23 VS16.3
x64 msvc v19.24 VS16.4
x64 msvc v19.25 VS16.5
x64 msvc v19.27 VS16.7
x64 msvc v19.28 VS16.8
x64 msvc v19.28 VS16.9
x64 msvc v19.29 VS16.10
x64 msvc v19.29 VS16.11
x64 msvc v19.30 VS17.0
x64 msvc v19.31 VS17.1
x64 msvc v19.32 VS17.2
x64 msvc v19.33 VS17.3
x64 msvc v19.34 VS17.4
x64 msvc v19.35 VS17.5
x64 msvc v19.36 VS17.6
x64 msvc v19.37 VS17.7
x64 msvc v19.38 VS17.8
x64 msvc v19.39 VS17.9
x64 msvc v19.40 VS17.10
x64 msvc v19.latest
x86 djgpp 4.9.4
x86 djgpp 5.5.0
x86 djgpp 6.4.0
x86 djgpp 7.2.0
x86 msvc v19.0 (WINE)
x86 msvc v19.10 (WINE)
x86 msvc v19.14 (WINE)
x86 msvc v19.20 VS16.0
x86 msvc v19.21 VS16.1
x86 msvc v19.22 VS16.2
x86 msvc v19.23 VS16.3
x86 msvc v19.24 VS16.4
x86 msvc v19.25 VS16.5
x86 msvc v19.27 VS16.7
x86 msvc v19.28 VS16.8
x86 msvc v19.28 VS16.9
x86 msvc v19.29 VS16.10
x86 msvc v19.29 VS16.11
x86 msvc v19.30 VS17.0
x86 msvc v19.31 VS17.1
x86 msvc v19.32 VS17.2
x86 msvc v19.33 VS17.3
x86 msvc v19.34 VS17.4
x86 msvc v19.35 VS17.5
x86 msvc v19.36 VS17.6
x86 msvc v19.37 VS17.7
x86 msvc v19.38 VS17.8
x86 msvc v19.39 VS17.9
x86 msvc v19.40 VS17.10
x86 msvc v19.latest
x86 nvc++ 22.11
x86 nvc++ 22.7
x86 nvc++ 22.9
x86 nvc++ 23.1
x86 nvc++ 23.11
x86 nvc++ 23.3
x86 nvc++ 23.5
x86 nvc++ 23.7
x86 nvc++ 23.9
x86 nvc++ 24.1
x86 nvc++ 24.11
x86 nvc++ 24.3
x86 nvc++ 24.5
x86 nvc++ 24.7
x86 nvc++ 24.9
x86-64 Zapcc 190308
x86-64 clang (Chris Bazley N3089)
x86-64 clang (EricWF contracts)
x86-64 clang (amd-staging)
x86-64 clang (assertions trunk)
x86-64 clang (clangir)
x86-64 clang (dascandy contracts)
x86-64 clang (experimental -Wlifetime)
x86-64 clang (experimental P1061)
x86-64 clang (experimental P1144)
x86-64 clang (experimental P1221)
x86-64 clang (experimental P2996)
x86-64 clang (experimental P2998)
x86-64 clang (experimental P3068)
x86-64 clang (experimental P3309)
x86-64 clang (experimental P3367)
x86-64 clang (experimental P3372)
x86-64 clang (experimental metaprogramming - P2632)
x86-64 clang (old concepts branch)
x86-64 clang (p1974)
x86-64 clang (pattern matching - P2688)
x86-64 clang (reflection)
x86-64 clang (resugar)
x86-64 clang (string interpolation - P3412)
x86-64 clang (thephd.dev)
x86-64 clang (trunk)
x86-64 clang (variadic friends - P2893)
x86-64 clang (widberg)
x86-64 clang 10.0.0
x86-64 clang 10.0.0 (assertions)
x86-64 clang 10.0.1
x86-64 clang 11.0.0
x86-64 clang 11.0.0 (assertions)
x86-64 clang 11.0.1
x86-64 clang 12.0.0
x86-64 clang 12.0.0 (assertions)
x86-64 clang 12.0.1
x86-64 clang 13.0.0
x86-64 clang 13.0.0 (assertions)
x86-64 clang 13.0.1
x86-64 clang 14.0.0
x86-64 clang 14.0.0 (assertions)
x86-64 clang 15.0.0
x86-64 clang 15.0.0 (assertions)
x86-64 clang 16.0.0
x86-64 clang 16.0.0 (assertions)
x86-64 clang 17.0.1
x86-64 clang 17.0.1 (assertions)
x86-64 clang 18.1.0
x86-64 clang 18.1.0 (assertions)
x86-64 clang 18.1.0 (clad 1.8)
x86-64 clang 19.1.0
x86-64 clang 19.1.0 (assertions)
x86-64 clang 2.6.0 (assertions)
x86-64 clang 2.7.0 (assertions)
x86-64 clang 2.8.0 (assertions)
x86-64 clang 2.9.0 (assertions)
x86-64 clang 3.0.0
x86-64 clang 3.0.0 (assertions)
x86-64 clang 3.1
x86-64 clang 3.1 (assertions)
x86-64 clang 3.2
x86-64 clang 3.2 (assertions)
x86-64 clang 3.3
x86-64 clang 3.3 (assertions)
x86-64 clang 3.4 (assertions)
x86-64 clang 3.4.1
x86-64 clang 3.5
x86-64 clang 3.5 (assertions)
x86-64 clang 3.5.1
x86-64 clang 3.5.2
x86-64 clang 3.6
x86-64 clang 3.6 (assertions)
x86-64 clang 3.7
x86-64 clang 3.7 (assertions)
x86-64 clang 3.7.1
x86-64 clang 3.8
x86-64 clang 3.8 (assertions)
x86-64 clang 3.8.1
x86-64 clang 3.9.0
x86-64 clang 3.9.0 (assertions)
x86-64 clang 3.9.1
x86-64 clang 4.0.0
x86-64 clang 4.0.0 (assertions)
x86-64 clang 4.0.1
x86-64 clang 5.0.0
x86-64 clang 5.0.0 (assertions)
x86-64 clang 5.0.1
x86-64 clang 5.0.2
x86-64 clang 6.0.0
x86-64 clang 6.0.0 (assertions)
x86-64 clang 6.0.1
x86-64 clang 7.0.0
x86-64 clang 7.0.0 (assertions)
x86-64 clang 7.0.1
x86-64 clang 7.1.0
x86-64 clang 8.0.0
x86-64 clang 8.0.0 (assertions)
x86-64 clang 8.0.1
x86-64 clang 9.0.0
x86-64 clang 9.0.0 (assertions)
x86-64 clang 9.0.1
x86-64 clang rocm-4.5.2
x86-64 clang rocm-5.0.2
x86-64 clang rocm-5.1.3
x86-64 clang rocm-5.2.3
x86-64 clang rocm-5.3.3
x86-64 clang rocm-5.7.0
x86-64 clang rocm-6.0.2
x86-64 clang rocm-6.1.2
x86-64 gcc (contract labels)
x86-64 gcc (contracts natural syntax)
x86-64 gcc (contracts)
x86-64 gcc (coroutines)
x86-64 gcc (modules)
x86-64 gcc (trunk)
x86-64 gcc 10.1
x86-64 gcc 10.2
x86-64 gcc 10.3
x86-64 gcc 10.3 (assertions)
x86-64 gcc 10.4
x86-64 gcc 10.4 (assertions)
x86-64 gcc 10.5
x86-64 gcc 10.5 (assertions)
x86-64 gcc 11.1
x86-64 gcc 11.1 (assertions)
x86-64 gcc 11.2
x86-64 gcc 11.2 (assertions)
x86-64 gcc 11.3
x86-64 gcc 11.3 (assertions)
x86-64 gcc 11.4
x86-64 gcc 11.4 (assertions)
x86-64 gcc 12.1
x86-64 gcc 12.1 (assertions)
x86-64 gcc 12.2
x86-64 gcc 12.2 (assertions)
x86-64 gcc 12.3
x86-64 gcc 12.3 (assertions)
x86-64 gcc 12.4
x86-64 gcc 12.4 (assertions)
x86-64 gcc 13.1
x86-64 gcc 13.1 (assertions)
x86-64 gcc 13.2
x86-64 gcc 13.2 (assertions)
x86-64 gcc 13.3
x86-64 gcc 13.3 (assertions)
x86-64 gcc 14.1
x86-64 gcc 14.1 (assertions)
x86-64 gcc 14.2
x86-64 gcc 14.2 (assertions)
x86-64 gcc 3.4.6
x86-64 gcc 4.0.4
x86-64 gcc 4.1.2
x86-64 gcc 4.4.7
x86-64 gcc 4.5.3
x86-64 gcc 4.6.4
x86-64 gcc 4.7.1
x86-64 gcc 4.7.2
x86-64 gcc 4.7.3
x86-64 gcc 4.7.4
x86-64 gcc 4.8.1
x86-64 gcc 4.8.2
x86-64 gcc 4.8.3
x86-64 gcc 4.8.4
x86-64 gcc 4.8.5
x86-64 gcc 4.9.0
x86-64 gcc 4.9.1
x86-64 gcc 4.9.2
x86-64 gcc 4.9.3
x86-64 gcc 4.9.4
x86-64 gcc 5.1
x86-64 gcc 5.2
x86-64 gcc 5.3
x86-64 gcc 5.4
x86-64 gcc 5.5
x86-64 gcc 6.1
x86-64 gcc 6.2
x86-64 gcc 6.3
x86-64 gcc 6.4
x86-64 gcc 6.5
x86-64 gcc 7.1
x86-64 gcc 7.2
x86-64 gcc 7.3
x86-64 gcc 7.4
x86-64 gcc 7.5
x86-64 gcc 8.1
x86-64 gcc 8.2
x86-64 gcc 8.3
x86-64 gcc 8.4
x86-64 gcc 8.5
x86-64 gcc 9.1
x86-64 gcc 9.2
x86-64 gcc 9.3
x86-64 gcc 9.4
x86-64 gcc 9.5
x86-64 icc 13.0.1
x86-64 icc 16.0.3
x86-64 icc 17.0.0
x86-64 icc 18.0.0
x86-64 icc 19.0.0
x86-64 icc 19.0.1
x86-64 icc 2021.1.2
x86-64 icc 2021.10.0
x86-64 icc 2021.2.0
x86-64 icc 2021.3.0
x86-64 icc 2021.4.0
x86-64 icc 2021.5.0
x86-64 icc 2021.6.0
x86-64 icc 2021.7.0
x86-64 icc 2021.7.1
x86-64 icc 2021.8.0
x86-64 icc 2021.9.0
x86-64 icx 2021.1.2
x86-64 icx 2021.2.0
x86-64 icx 2021.3.0
x86-64 icx 2021.4.0
x86-64 icx 2022.0.0
x86-64 icx 2022.1.0
x86-64 icx 2022.2.0
x86-64 icx 2022.2.1
x86-64 icx 2023.0.0
x86-64 icx 2023.1.0
x86-64 icx 2023.2.1
x86-64 icx 2024.0.0
x86-64 icx 2024.1.0
x86-64 icx 2024.2.0
x86-64 icx 2025.0.0
x86-64 icx 2025.0.0
zig c++ 0.10.0
zig c++ 0.11.0
zig c++ 0.12.0
zig c++ 0.12.1
zig c++ 0.13.0
zig c++ 0.6.0
zig c++ 0.7.0
zig c++ 0.7.1
zig c++ 0.8.0
zig c++ 0.9.0
zig c++ trunk
Options
Source code
/* The latest version of this library is available on GitHub; https://github.com/sheredom/utest.h */ /* This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to <http://unlicense.org/> */ #ifndef SHEREDOM_UTEST_H_INCLUDED #define SHEREDOM_UTEST_H_INCLUDED #ifdef _MSC_VER /* Disable warning about not inlining 'inline' functions. TODO: We'll fix this later by not using fprintf within our macros, and instead use snprintf to a realloc'ed buffer. */ #pragma warning(disable : 4710) /* Disable warning about inlining functions that are not marked 'inline'. TODO: add a UTEST_NOINLINE onto the macro generated functions to fix this. */ #pragma warning(disable : 4711) #pragma warning(push, 1) #endif #if defined(_MSC_VER) typedef __int64 int64_t; typedef unsigned __int64 uint64_t; #else #include <stdint.h> #endif #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #if defined(_MSC_VER) #pragma warning(pop) #endif #if defined(_MSC_VER) #if defined(_M_IX86) #define _X86_ #endif #if defined(_M_AMD64) #define _AMD64_ #endif #pragma warning(push, 1) #include <windef.h> #include <winbase.h> #pragma warning(pop) #elif defined(__linux__) /* slightly obscure include here - we need to include glibc's features.h, but we don't want to just include a header that might not be defined for other c libraries like musl. Instead we include limits.h, which we know on all glibc distributions includes features.h */ #include <limits.h> #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) #include <time.h> #if ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) /* glibc is version 2.17 or above, so we can just use clock_gettime */ #define UTEST_USE_CLOCKGETTIME #else #include <sys/syscall.h> #include <unistd.h> #endif #endif #elif defined(__APPLE__) #include <mach/mach_time.h> #endif #if defined(_MSC_VER) #define UTEST_PRId64 "I64d" #define UTEST_PRIu64 "I64u" #define UTEST_INLINE __forceinline #pragma section(".CRT$XCU", read) #define UTEST_INITIALIZER(f) \ static void __cdecl f(void); \ __declspec(allocate(".CRT$XCU")) void(__cdecl * f##_)(void) = f; \ static void __cdecl f(void) #else #if defined(__linux__) #if defined(__clang__) #if __has_warning("-Wreserved-id-macro") #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wreserved-id-macro" #endif #endif #define __STDC_FORMAT_MACROS 1 #if defined(__clang__) #if __has_warning("-Wreserved-id-macro") #pragma clang diagnostic pop #endif #endif #endif #include <inttypes.h> #define UTEST_PRId64 PRId64 #define UTEST_PRIu64 PRIu64 #define UTEST_INLINE inline #define UTEST_INITIALIZER(f) \ static void f(void) __attribute__((constructor)); \ static void f(void) #endif #if defined(__cplusplus) #define UTEST_CAST(type, x) static_cast<type>(x) #define UTEST_PTR_CAST(type, x) reinterpret_cast<type>(x) #define UTEST_EXTERN extern "C" #else #define UTEST_CAST(type, x) ((type)x) #define UTEST_PTR_CAST(type, x) ((type)x) #define UTEST_EXTERN extern #endif #ifdef _MSC_VER /* io.h contains definitions for some structures with natural padding. This is uninteresting, but for some reason MSVC's behaviour is to warn about including this system header. That *is* interesting */ #pragma warning(disable : 4820) #pragma warning(push, 1) #include <io.h> #pragma warning(pop) #define UTEST_COLOUR_OUTPUT() (_isatty(_fileno(stdout))) #else #include <unistd.h> #define UTEST_COLOUR_OUTPUT() (isatty(STDOUT_FILENO)) #endif static UTEST_INLINE int64_t utest_ns(void) { #ifdef _MSC_VER LARGE_INTEGER counter; LARGE_INTEGER frequency; QueryPerformanceCounter(&counter); QueryPerformanceFrequency(&frequency); return UTEST_CAST(int64_t, (counter.QuadPart * 1000000000) / frequency.QuadPart); #elif defined(__linux) struct timespec ts; const clockid_t cid = CLOCK_REALTIME; #if defined(UTEST_USE_CLOCKGETTIME) clock_gettime(cid, &ts); #else syscall(SYS_clock_gettime, cid, &ts); #endif return UTEST_CAST(int64_t, ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec; #elif __APPLE__ return UTEST_CAST(int64_t, mach_absolute_time()); #endif } typedef void (*utest_testcase_t)(int *, size_t); struct utest_test_state_s { utest_testcase_t func; size_t index; char *name; }; struct utest_state_s { struct utest_test_state_s *tests; size_t tests_length; FILE *output; }; /* extern to the global state utest needs to execute */ UTEST_EXTERN struct utest_state_s utest_state; #if defined(_MSC_VER) #define UTEST_WEAK __forceinline #else #define UTEST_WEAK __attribute__((weak)) #endif #if defined(_MSC_VER) #define UTEST_UNUSED #else #define UTEST_UNUSED __attribute__((unused)) #endif #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wvariadic-macros" #endif #define UTEST_PRINTF(...) \ if (utest_state.output) { \ fprintf(utest_state.output, __VA_ARGS__); \ } \ printf(__VA_ARGS__) #ifdef __clang__ #pragma clang diagnostic pop #endif #ifdef _MSC_VER #define UTEST_SNPRINTF(BUFFER, N, ...) _snprintf_s(BUFFER, N, N, __VA_ARGS__) #else #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wvariadic-macros" #endif #define UTEST_SNPRINTF(...) snprintf(__VA_ARGS__) #ifdef __clang__ #pragma clang diagnostic pop #endif #endif #if defined(__cplusplus) /* if we are using c++ we can use overloaded methods (its in the language) */ #define UTEST_OVERLOADABLE #elif defined(__clang__) /* otherwise, if we are using clang with c - use the overloadable attribute */ #define UTEST_OVERLOADABLE __attribute__((overloadable)) #endif #if defined(UTEST_OVERLOADABLE) UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f) { UTEST_PRINTF("%f", UTEST_CAST(double, f)); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d) { UTEST_PRINTF("%f", d); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d) { UTEST_PRINTF("%Lf", d); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i) { UTEST_PRINTF("%d", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i) { UTEST_PRINTF("%u", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i) { UTEST_PRINTF("%ld", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i) { UTEST_PRINTF("%lu", i); } /* long long is a c++11 extension TODO: grok for c++11 version here */ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i) { UTEST_PRINTF("%lld", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i) { UTEST_PRINTF("%llu", i); } #endif #else /* we don't have the ability to print the values we got, so we create a macro to tell our users we can't do anything fancy */ #define utest_type_printer(...) UTEST_PRINTF("undef") #endif #if defined(__clang__) #define UTEST_EXPECT(x, y, cond) \ { \ _Pragma("clang diagnostic push") _Pragma( \ "clang diagnostic ignored \"-Wlanguage-extension-token\"") typeof(y) \ xEval = (x); \ typeof(y) yEval = (y); \ _Pragma("clang diagnostic pop") if (!((xEval)cond(yEval))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \ utest_type_printer(xEval); \ UTEST_PRINTF("\n"); \ UTEST_PRINTF(" Actual : "); \ utest_type_printer(yEval); \ UTEST_PRINTF("\n"); \ *utest_result = 1; \ } \ } #elif defined(__GNUC__) #define UTEST_EXPECT(x, y, cond) \ { \ typeof(y) xEval = (x); \ typeof(y) yEval = (y); \ if (!((xEval)cond(yEval))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \ utest_type_printer(xEval); \ UTEST_PRINTF("\n"); \ UTEST_PRINTF(" Actual : "); \ utest_type_printer(yEval); \ UTEST_PRINTF("\n"); \ *utest_result = 1; \ } \ } #else #define UTEST_EXPECT(x, y, cond) \ { \ if (!((x)cond(y))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ *utest_result = 1; \ } \ } #endif #define EXPECT_TRUE(x) \ if (!(x)) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : true\n"); \ UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ } #define EXPECT_FALSE(x) \ if (x) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : false\n"); \ UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ } #define EXPECT_EQ(x, y) UTEST_EXPECT(x, y, ==) #define EXPECT_NE(x, y) UTEST_EXPECT(x, y, !=) #define EXPECT_LT(x, y) UTEST_EXPECT(x, y, <) #define EXPECT_LE(x, y) UTEST_EXPECT(x, y, <=) #define EXPECT_GT(x, y) UTEST_EXPECT(x, y, >) #define EXPECT_GE(x, y) UTEST_EXPECT(x, y, >=) #define EXPECT_STREQ(x, y) \ if (0 != strcmp(x, y)) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : \"%s\"\n", x); \ UTEST_PRINTF(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ } #define EXPECT_STRNE(x, y) \ if (0 == strcmp(x, y)) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : \"%s\"\n", x); \ UTEST_PRINTF(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ } #if defined(__clang__) #define UTEST_ASSERT(x, y, cond) \ { \ _Pragma("clang diagnostic push") _Pragma( \ "clang diagnostic ignored \"-Wlanguage-extension-token\"") typeof(y) \ xEval = (x); \ typeof(y) yEval = (y); \ _Pragma("clang diagnostic pop") if (!((xEval)cond(yEval))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \ utest_type_printer(xEval); \ UTEST_PRINTF("\n"); \ UTEST_PRINTF(" Actual : "); \ utest_type_printer(yEval); \ UTEST_PRINTF("\n"); \ *utest_result = 1; \ return; \ } \ } #elif defined(__GNUC__) #define UTEST_ASSERT(x, y, cond) \ { \ typeof(y) xEval = (x); \ typeof(y) yEval = (y); \ if (!((xEval)cond(yEval))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \ utest_type_printer(xEval); \ UTEST_PRINTF("\n"); \ UTEST_PRINTF(" Actual : "); \ utest_type_printer(yEval); \ UTEST_PRINTF("\n"); \ *utest_result = 1; \ return; \ } \ } #else #define UTEST_ASSERT(x, y, cond) \ { \ if (!((x)cond(y))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ *utest_result = 1; \ return; \ } \ } #endif #define ASSERT_TRUE(x) \ if (!(x)) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : true\n"); \ UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ return; \ } #define ASSERT_FALSE(x) \ if (x) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : false\n"); \ UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ return; \ } #define ASSERT_EQ(x, y) UTEST_ASSERT(x, y, ==) #define ASSERT_NE(x, y) UTEST_ASSERT(x, y, !=) #define ASSERT_LT(x, y) UTEST_ASSERT(x, y, <) #define ASSERT_LE(x, y) UTEST_ASSERT(x, y, <=) #define ASSERT_GT(x, y) UTEST_ASSERT(x, y, >) #define ASSERT_GE(x, y) UTEST_ASSERT(x, y, >=) #define ASSERT_STREQ(x, y) \ EXPECT_STREQ(x, y); \ if (0 != strcmp(x, y)) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : \"%s\"\n", x); \ UTEST_PRINTF(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ return; \ } #define ASSERT_STRNE(x, y) \ EXPECT_STRNE(x, y); \ if (0 == strcmp(x, y)) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : \"%s\"\n", x); \ UTEST_PRINTF(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ return; \ } #define UTEST(SET, NAME) \ UTEST_EXTERN struct utest_state_s utest_state; \ static void utest_run_##SET##_##NAME(int *utest_result); \ static void utest_##SET##_##NAME(int *utest_result, size_t utest_index) { \ (void)utest_index; \ utest_run_##SET##_##NAME(utest_result); \ } \ UTEST_INITIALIZER(utest_register_##SET##_##NAME) { \ const size_t index = utest_state.tests_length++; \ const char *name_part = #SET "." #NAME; \ const size_t name_size = strlen(name_part) + 1; \ char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ utest_state.tests = \ UTEST_PTR_CAST(struct utest_test_state_s *, \ realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ sizeof(struct utest_test_state_s) * \ utest_state.tests_length)); \ utest_state.tests[index].func = &utest_##SET##_##NAME; \ utest_state.tests[index].name = name; \ UTEST_SNPRINTF(name, name_size, "%s", name_part); \ } \ void utest_run_##SET##_##NAME(int *utest_result) #define UTEST_F_SETUP(FIXTURE) \ static void utest_f_setup_##FIXTURE(int *utest_result, \ struct FIXTURE *utest_fixture) #define UTEST_F_TEARDOWN(FIXTURE) \ static void utest_f_teardown_##FIXTURE(int *utest_result, \ struct FIXTURE *utest_fixture) #define UTEST_F(FIXTURE, NAME) \ UTEST_EXTERN struct utest_state_s utest_state; \ static void utest_f_setup_##FIXTURE(int *, struct FIXTURE *); \ static void utest_f_teardown_##FIXTURE(int *, struct FIXTURE *); \ static void utest_run_##FIXTURE##_##NAME(int *, struct FIXTURE *); \ static void utest_f_##FIXTURE##_##NAME(int *utest_result, \ size_t utest_index) { \ struct FIXTURE fixture; \ (void)utest_index; \ memset(&fixture, 0, sizeof(fixture)); \ utest_f_setup_##FIXTURE(utest_result, &fixture); \ if (0 != *utest_result) { \ return; \ } \ utest_run_##FIXTURE##_##NAME(utest_result, &fixture); \ utest_f_teardown_##FIXTURE(utest_result, &fixture); \ } \ UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME) { \ const size_t index = utest_state.tests_length++; \ const char *name_part = #FIXTURE "." #NAME; \ const size_t name_size = strlen(name_part) + 1; \ char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ utest_state.tests = \ UTEST_PTR_CAST(struct utest_test_state_s *, \ realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ sizeof(struct utest_test_state_s) * \ utest_state.tests_length)); \ utest_state.tests[index].func = &utest_f_##FIXTURE##_##NAME; \ utest_state.tests[index].name = name; \ UTEST_SNPRINTF(name, name_size, "%s", name_part); \ } \ void utest_run_##FIXTURE##_##NAME(int *utest_result, \ struct FIXTURE *utest_fixture) #define UTEST_I_SETUP(FIXTURE) \ static void utest_i_setup_##FIXTURE( \ int *utest_result, struct FIXTURE *utest_fixture, size_t utest_index) #define UTEST_I_TEARDOWN(FIXTURE) \ static void utest_i_teardown_##FIXTURE( \ int *utest_result, struct FIXTURE *utest_fixture, size_t utest_index) #define UTEST_I(FIXTURE, NAME, INDEX) \ UTEST_EXTERN struct utest_state_s utest_state; \ static void utest_run_##FIXTURE##_##NAME##_##INDEX(int *, struct FIXTURE *); \ static void utest_i_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ size_t index) { \ struct FIXTURE fixture; \ memset(&fixture, 0, sizeof(fixture)); \ utest_i_setup_##FIXTURE(utest_result, &fixture, index); \ if (0 != *utest_result) { \ return; \ } \ utest_run_##FIXTURE##_##NAME##_##INDEX(utest_result, &fixture); \ utest_i_teardown_##FIXTURE(utest_result, &fixture, index); \ } \ UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME##_##INDEX) { \ size_t i; \ uint64_t iUp; \ for (i = 0; i < (INDEX); i++) { \ const size_t index = utest_state.tests_length++; \ const char *name_part = #FIXTURE "." #NAME; \ const size_t name_size = strlen(name_part) + 32; \ char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ utest_state.tests = \ UTEST_PTR_CAST(struct utest_test_state_s *, \ realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ sizeof(struct utest_test_state_s) * \ utest_state.tests_length)); \ utest_state.tests[index].func = &utest_i_##FIXTURE##_##NAME##_##INDEX; \ utest_state.tests[index].index = i; \ utest_state.tests[index].name = name; \ iUp = UTEST_CAST(uint64_t, i); \ UTEST_SNPRINTF(name, name_size, "%s/%" UTEST_PRIu64, name_part, iUp); \ } \ } \ void utest_run_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ struct FIXTURE *utest_fixture) UTEST_WEAK int utest_should_filter_test(const char *filter, const char *testcase); UTEST_WEAK int utest_should_filter_test(const char *filter, const char *testcase) { if (filter) { const char *filter_cur = filter; const char *testcase_cur = testcase; const char *filter_wildcard = 0; while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { if ('*' == *filter_cur) { /* store the position of the wildcard */ filter_wildcard = filter_cur; /* skip the wildcard character */ filter_cur++; while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { if ('*' == *filter_cur) { /* we found another wildcard (filter is something like *foo*) so we exit the current loop, and return to the parent loop to handle the wildcard case */ break; } else if (*filter_cur != *testcase_cur) { /* otherwise our filter didn't match, so reset it */ filter_cur = filter_wildcard; } /* move testcase along */ testcase_cur++; /* move filter along */ filter_cur++; } if (('\0' == *filter_cur) && ('\0' == *testcase_cur)) { return 0; } /* if the testcase has been exhausted, we don't have a match! */ if ('\0' == *testcase_cur) { return 1; } } else { if (*testcase_cur != *filter_cur) { /* test case doesn't match filter */ return 1; } else { /* move our filter and testcase forward */ testcase_cur++; filter_cur++; } } } if (('\0' != *filter_cur) || (('\0' != *testcase_cur) && ((filter == filter_cur) || ('*' != filter_cur[-1])))) { /* we have a mismatch! */ return 1; } } return 0; } static UTEST_INLINE int utest_strncmp(const char *a, const char *b, size_t n) { /* strncmp breaks on Wall / Werror on gcc/clang, so we avoid using it */ unsigned i; for (i = 0; i < n; i++) { if (a[i] < b[i]) { return -1; } else if (a[i] > b[i]) { return 1; } } return 0; } static UTEST_INLINE FILE *utest_fopen(const char *filename, const char *mode) { #ifdef _MSC_VER FILE *file; if (0 == fopen_s(&file, filename, mode)) { return file; } else { return 0; } #else return fopen(filename, mode); #endif } UTEST_WEAK int utest_main(int argc, const char *const argv[]); UTEST_WEAK int utest_main(int argc, const char *const argv[]) { uint64_t failed = 0; size_t index = 0; size_t *failed_testcases = 0; size_t failed_testcases_length = 0; const char *filter = 0; uint64_t ran_tests = 0; enum colours { RESET, GREEN, RED }; const int use_colours = UTEST_COLOUR_OUTPUT(); const char *colours[] = {"\033[0m", "\033[32m", "\033[31m"}; if (!use_colours) { for (index = 0; index < sizeof colours / sizeof colours[0]; index++) { colours[index] = ""; } } /* loop through all arguments looking for our options */ for (index = 1; index < UTEST_CAST(size_t, argc); index++) { const char help_str[] = "--help"; const char filter_str[] = "--filter="; const char output_str[] = "--output="; if (0 == utest_strncmp(argv[index], help_str, strlen(help_str))) { printf("utest.h - the single file unit testing solution for C/C++!\n" "Command line Options:\n" " --help Show this message and exit.\n" " --filter=<filter> Filter the test cases to run (EG. MyTest*.a " "would run MyTestCase.a but not MyTestCase.b).\n" " --output=<output> Output an xunit XML file to the file " "specified in <output>.\n"); goto cleanup; } else if (0 == utest_strncmp(argv[index], filter_str, strlen(filter_str))) { /* user wants to filter what test cases run! */ filter = argv[index] + strlen(filter_str); } else if (0 == utest_strncmp(argv[index], output_str, strlen(output_str))) { utest_state.output = utest_fopen(argv[index] + strlen(output_str), "w+"); } } for (index = 0; index < utest_state.tests_length; index++) { if (utest_should_filter_test(filter, utest_state.tests[index].name)) { continue; } ran_tests++; } printf("%s[==========]%s Running %" UTEST_PRIu64 " test cases.\n", colours[GREEN], colours[RESET], UTEST_CAST(uint64_t, ran_tests)); if (utest_state.output) { fprintf(utest_state.output, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); fprintf(utest_state.output, "<testsuites tests=\"%" UTEST_PRIu64 "\" name=\"All\">\n", UTEST_CAST(uint64_t, ran_tests)); fprintf(utest_state.output, "<testsuite name=\"Tests\" tests=\"%" UTEST_PRIu64 "\">\n", UTEST_CAST(uint64_t, ran_tests)); } for (index = 0; index < utest_state.tests_length; index++) { int result = 0; int64_t ns = 0; if (utest_should_filter_test(filter, utest_state.tests[index].name)) { continue; } printf("%s[ RUN ]%s %s\n", colours[GREEN], colours[RESET], utest_state.tests[index].name); if (utest_state.output) { fprintf(utest_state.output, "<testcase name=\"%s\">", utest_state.tests[index].name); } ns = utest_ns(); utest_state.tests[index].func(&result, utest_state.tests[index].index); ns = utest_ns() - ns; if (utest_state.output) { fprintf(utest_state.output, "</testcase>\n"); } if (0 != result) { const size_t failed_testcase_index = failed_testcases_length++; failed_testcases = UTEST_PTR_CAST( size_t *, realloc(UTEST_PTR_CAST(void *, failed_testcases), sizeof(size_t) * failed_testcases_length)); failed_testcases[failed_testcase_index] = index; failed++; printf("%s[ FAILED ]%s %s (%" UTEST_PRId64 "ns)\n", colours[RED], colours[RESET], utest_state.tests[index].name, ns); } else { printf("%s[ OK ]%s %s (%" UTEST_PRId64 "ns)\n", colours[GREEN], colours[RESET], utest_state.tests[index].name, ns); } } printf("%s[==========]%s %" UTEST_PRIu64 " test cases ran.\n", colours[GREEN], colours[RESET], ran_tests); printf("%s[ PASSED ]%s %" UTEST_PRIu64 " tests.\n", colours[GREEN], colours[RESET], ran_tests - failed); if (0 != failed) { printf("%s[ FAILED ]%s %" UTEST_PRIu64 " tests, listed below:\n", colours[RED], colours[RESET], failed); for (index = 0; index < failed_testcases_length; index++) { printf("%s[ FAILED ]%s %s\n", colours[RED], colours[RESET], utest_state.tests[failed_testcases[index]].name); } } if (utest_state.output) { fprintf(utest_state.output, "</testsuite>\n</testsuites>\n"); } cleanup: for (index = 0; index < utest_state.tests_length; index++) { free(UTEST_PTR_CAST(void *, utest_state.tests[index].name)); } free(UTEST_PTR_CAST(void *, failed_testcases)); free(UTEST_PTR_CAST(void *, utest_state.tests)); if (utest_state.output) { fclose(utest_state.output); } return UTEST_CAST(int, failed); } /* we need, in exactly one source file, define the global struct that will hold the data we need to run utest. This macro allows the user to declare the data without having to use the UTEST_MAIN macro, thus allowing them to write their own main() function. */ #define UTEST_STATE() struct utest_state_s utest_state = {0, 0, 0} /* define a main() function to call into utest.h and start executing tests! A user can optionally not use this macro, and instead define their own main() function and manually call utest_main. The user must, in exactly one source file, use the UTEST_STATE macro to declare a global struct variable that utest requires. */ #define UTEST_MAIN() \ UTEST_STATE(); \ int main(int argc, const char *const argv[]) { \ return utest_main(argc, argv); \ } #endif /* SHEREDOM_UTEST_H_INCLUDED */ char* foo; UTEST(test, x) { ASSERT_TRUE(foo); } UTEST_MAIN();
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree