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

Unit test for creating trampolines for functions with 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 macro to generate a complete subtest for a single 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

Unit test for creating trampolines for functions with primitive C types.

This test file verifies the core functionality of the infix library for the most fundamental C types (integers, floats, bool). It is a critical "smoke test" that ensures the basic JIT compilation pipeline is working correctly for each supported platform ABI.

The test covers the following for each primitive type:

  • **infix_forward_create_unbound_manual**: Creation of an unbound trampoline.
  • **infix_forward_create_manual**: Creation of a bound trampoline.
  • Calling: Correctly calling both unbound and bound trampolines.
  • Argument Passing: Verifying that the primitive argument is passed correctly to the target C function.
  • Return Value: Verifying that the primitive return value is correctly received from the target C function.

It uses a "passthrough" C function for each type (e.g., passthrough_int32) that simply returns its argument, allowing for a straightforward check of whether the value was transmitted correctly through the FFI boundary.

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 macro to generate a complete subtest for a single primitive type.

This macro automates the repetitive process of testing each primitive type. For a given C type and its corresponding infix ID, it generates a subtest that:

  1. Creates the infix_type for the primitive.
  2. Creates and calls an unbound trampoline.
  3. Verifies the result of the unbound call.
  4. Creates and calls a bound trampoline.
  5. Verifies the result of the bound call. This reduces code duplication and makes the test easy to read and extend.

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:163