infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
001_primitives.c File Reference

Tests FFI functionality with all supported primitive C types. More...

#include "common/double_tap.h"
#include "common/infix_config.h"
#include <infix/infix.h>
#include <inttypes.h>
Include dependency graph for 001_primitives.c:

Macros

#define DBLTAP_IMPLEMENTATION
 
#define TEST_PRIMITIVE(test_name, c_type, infix_id, passthrough_func, input_val, format_specifier)
 A helper macro to generate a subtest for a specific primitive type.
 

Functions

bool passthrough_bool (bool v)
 
uint8_t passthrough_uint8 (uint8_t v)
 
int8_t passthrough_sint8 (int8_t v)
 
uint16_t passthrough_uint16 (uint16_t v)
 
int16_t passthrough_sint16 (int16_t v)
 
uint32_t passthrough_uint32 (uint32_t v)
 
int32_t passthrough_sint32 (int32_t v)
 
uint64_t passthrough_uint64 (uint64_t v)
 
int64_t passthrough_sint64 (int64_t v)
 
float passthrough_float (float v)
 
double passthrough_double (double v)
 
long double passthrough_long_double (long double v)
 
__uint128_t passthrough_uint128 (__uint128_t v)
 
__int128_t passthrough_sint128 (__int128_t v)
 
 TEST_PRIMITIVE ("bool", bool, INFIX_PRIMITIVE_BOOL, passthrough_bool, true, "%d")
 
 TEST_PRIMITIVE ("uint8_t", uint8_t, INFIX_PRIMITIVE_UINT8, passthrough_uint8, 255, "%u")
 
 TEST_PRIMITIVE ("int8_t", int8_t, INFIX_PRIMITIVE_SINT8, passthrough_sint8, -128, "%d")
 
 TEST_PRIMITIVE ("uint16_t", uint16_t, INFIX_PRIMITIVE_UINT16, passthrough_uint16, 65535, "%u")
 
 TEST_PRIMITIVE ("int16_t", int16_t, INFIX_PRIMITIVE_SINT16, passthrough_sint16, -32768, "%d")
 
 TEST_PRIMITIVE ("uint32_t", uint32_t, INFIX_PRIMITIVE_UINT32, passthrough_uint32, 0xFFFFFFFF, "%u")
 
 TEST_PRIMITIVE ("int32_t", int32_t, INFIX_PRIMITIVE_SINT32, passthrough_sint32, -2147483647 - 1, "%d")
 
 TEST_PRIMITIVE ("uint64_t", uint64_t, INFIX_PRIMITIVE_UINT64, passthrough_uint64, 0xFFFFFFFFFFFFFFFF, "%" PRIu64)
 
 TEST_PRIMITIVE ("int64_t", int64_t, INFIX_PRIMITIVE_SINT64, passthrough_sint64, -9223372036854775807LL - 1, "%" PRId64)
 
 TEST_PRIMITIVE ("float", float, INFIX_PRIMITIVE_FLOAT, passthrough_float, 3.14159f, "%f")
 
 TEST_PRIMITIVE ("double", double, INFIX_PRIMITIVE_DOUBLE, passthrough_double, 2.718281828459045, "%f")
 
 subtest ("long double")
 
 subtest ("__uint128_t")
 
 subtest ("__int128_t")
 

Variables

 TEST
 

Detailed Description

Tests FFI functionality with all supported primitive C types.

Copyright (c) 2025 Sanko Robinson

This source code is dual-licensed under the Artistic License 2.0 or the MIT License. You may choose to use this code under the terms of either license.

SPDX-License-Identifier: (Artistic-2.0 OR MIT)

The documentation blocks within this file are licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).

SPDX-License-Identifier: CC-BY-4.0

This test suite verifies the core capability of the infix library to handle all fundamental C data types. It does this by creating a simple "passthrough" C function for each primitive (e.g., bool, int8_t, double, __int128_t).

For each type, the test performs the following steps:

  1. Defines the infix_type using infix_type_create_primitive.
  2. Generates a forward trampoline for the passthrough function's signature.
  3. Calls the native C function through the generated trampoline.
  4. Asserts that the value returned from the FFI call is identical to the value that was passed in.

This process validates that the library correctly handles the size, alignment, and ABI-specific calling conventions for every primitive type on the target platform, forming the foundational layer of the entire test suite.

These tests use the manual API to test primitive types to verify functionality without bringing the signature system in.

Macro Definition Documentation

◆ DBLTAP_IMPLEMENTATION

#define DBLTAP_IMPLEMENTATION

◆ TEST_PRIMITIVE

#define TEST_PRIMITIVE (   test_name,
  c_type,
  infix_id,
  passthrough_func,
  input_val,
  format_specifier 
)

A helper macro to generate a subtest for a specific primitive type.

This macro encapsulates the repetitive logic for testing a single primitive type. It creates a subtest, generates a trampoline, performs the FFI call, checks the result, and cleans up resources. This reduces code duplication and makes the test suite easier to read and maintain.

Parameters
test_nameThe string name for the subtest.
c_typeThe C data type (e.g., uint8_t).
infix_idThe infix_primitive_type_id enum value.
passthrough_funcThe name of the native C passthrough function.
input_valA literal value to use for testing.
format_specifierA printf format specifier for diagnostic messages.

Function Documentation

◆ passthrough_bool()

bool passthrough_bool ( bool  v)

◆ passthrough_double()

double passthrough_double ( double  v)

◆ passthrough_float()

float passthrough_float ( float  v)

◆ passthrough_long_double()

long double passthrough_long_double ( long double  v)

◆ passthrough_sint128()

__int128_t passthrough_sint128 ( __int128_t  v)

◆ passthrough_sint16()

int16_t passthrough_sint16 ( int16_t  v)

◆ passthrough_sint32()

int32_t passthrough_sint32 ( int32_t  v)

◆ passthrough_sint64()

int64_t passthrough_sint64 ( int64_t  v)

◆ passthrough_sint8()

int8_t passthrough_sint8 ( int8_t  v)

◆ passthrough_uint128()

__uint128_t passthrough_uint128 ( __uint128_t  v)

◆ passthrough_uint16()

uint16_t passthrough_uint16 ( uint16_t  v)

◆ passthrough_uint32()

uint32_t passthrough_uint32 ( uint32_t  v)

◆ passthrough_uint64()

uint64_t passthrough_uint64 ( uint64_t  v)

◆ passthrough_uint8()

uint8_t passthrough_uint8 ( uint8_t  v)

◆ subtest() [1/3]

subtest ( "__int128_t"  )

◆ subtest() [2/3]

subtest ( "__uint128_t"  )

◆ subtest() [3/3]

subtest ( "long double"  )

◆ TEST_PRIMITIVE() [1/11]

TEST_PRIMITIVE ( "bool"  ,
bool  ,
INFIX_PRIMITIVE_BOOL  ,
passthrough_bool  ,
true  ,
"%d"   
)

◆ TEST_PRIMITIVE() [2/11]

TEST_PRIMITIVE ( "double"  ,
double  ,
INFIX_PRIMITIVE_DOUBLE  ,
passthrough_double  ,
2.  718281828459045,
"%f"   
)

◆ TEST_PRIMITIVE() [3/11]

TEST_PRIMITIVE ( "float"  ,
float  ,
INFIX_PRIMITIVE_FLOAT  ,
passthrough_float  ,
3.  14159f,
"%f"   
)

◆ TEST_PRIMITIVE() [4/11]

TEST_PRIMITIVE ( "int16_t"  ,
int16_t  ,
INFIX_PRIMITIVE_SINT16  ,
passthrough_sint16  ,
32768,
"%d"   
)

◆ TEST_PRIMITIVE() [5/11]

TEST_PRIMITIVE ( "int32_t"  ,
int32_t  ,
INFIX_PRIMITIVE_SINT32  ,
passthrough_sint32  ,
-2147483647 -  1,
"%d"   
)

◆ TEST_PRIMITIVE() [6/11]

TEST_PRIMITIVE ( "int64_t"  ,
int64_t  ,
INFIX_PRIMITIVE_SINT64  ,
passthrough_sint64  ,
-9223372036854775807LL -  1,
"%"  PRId64 
)

◆ TEST_PRIMITIVE() [7/11]

TEST_PRIMITIVE ( "int8_t"  ,
int8_t  ,
INFIX_PRIMITIVE_SINT8  ,
passthrough_sint8  ,
128,
"%d"   
)

◆ TEST_PRIMITIVE() [8/11]

TEST_PRIMITIVE ( "uint16_t"  ,
uint16_t  ,
INFIX_PRIMITIVE_UINT16  ,
passthrough_uint16  ,
65535  ,
"%u"   
)

◆ TEST_PRIMITIVE() [9/11]

TEST_PRIMITIVE ( "uint32_t"  ,
uint32_t  ,
INFIX_PRIMITIVE_UINT32  ,
passthrough_uint32  ,
0xFFFFFFFF  ,
"%u"   
)

◆ TEST_PRIMITIVE() [10/11]

TEST_PRIMITIVE ( "uint64_t"  ,
uint64_t  ,
INFIX_PRIMITIVE_UINT64  ,
passthrough_uint64  ,
0xFFFFFFFFFFFFFFFF  ,
"%"  PRIu64 
)

◆ TEST_PRIMITIVE() [11/11]

TEST_PRIMITIVE ( "uint8_t"  ,
uint8_t  ,
INFIX_PRIMITIVE_UINT8  ,
passthrough_uint8  ,
255  ,
"%u"   
)

Variable Documentation

◆ TEST

TEST
Initial value:
{
plan(14)
#define plan(count)
Definition double_tap.h:132