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

Unit test for passing and returning unions by value. More...

#include "common/double_tap.h"
#include "types.h"
#include <infix/infix.h>
#include <math.h>
#include <string.h>
Include dependency graph for 103_unions.c:

Macros

#define DBLTAP_IMPLEMENTATION
 

Functions

int process_number_union_as_int (Number num)
 A C function that interprets a passed-in Number union as an integer.
 
float process_number_union_as_float (Number num)
 A C function that interprets a passed-in Number union as a float.
 
Number return_number_union (int selector)
 A C function that returns a Number union by value.
 
 if (!ok(status==INFIX_SUCCESS, "infix_type for Number union created successfully"))
 
 subtest ("Passing union as argument")
 
 subtest ("Returning union by value")
 
 infix_arena_destroy (arena)
 

Variables

 TEST
 
infix_arena_tarena = infix_arena_create(4096)
 
infix_struct_membermembers
 
infix_typeunion_type = nullptr
 
infix_status status = infix_type_create_union(arena, &union_type, members, 2)
 

Detailed Description

Unit test for passing and returning unions by value.

This test verifies that infix correctly handles C unions in FFI calls. A union is a special type of aggregate, and its handling by the ABI often follows the same rules as structs of the same size and alignment.

This test uses a simple Number union containing an int and a float. It verifies that:

  1. The infix_type for the union is created with the correct size (the size of the largest member) and alignment (the alignment of the most-aligned member).
  2. When the union is passed as an argument, its raw byte representation is correctly transmitted, allowing the callee to interpret it as either an int or a float.
  3. When a union is returned by value, its raw bytes are correctly received by the caller.

This test is important for validating the aggregate classification logic for a type that is less common than structs but still a core part of the C language.

Macro Definition Documentation

◆ DBLTAP_IMPLEMENTATION

#define DBLTAP_IMPLEMENTATION

Function Documentation

◆ if()

if ( okstatus==INFIX_SUCCESS, "infix_type for Number union created successfully")

◆ infix_arena_destroy()

infix_arena_destroy ( arena  )

◆ process_number_union_as_float()

float process_number_union_as_float ( Number  num)

A C function that interprets a passed-in Number union as a float.

◆ process_number_union_as_int()

int process_number_union_as_int ( Number  num)

A C function that interprets a passed-in Number union as an integer.

◆ return_number_union()

Number return_number_union ( int  selector)

A C function that returns a Number union by value.

◆ subtest() [1/2]

subtest ( "Passing union as argument"  )

◆ subtest() [2/2]

subtest ( "Returning union by value"  )

Variable Documentation

◆ arena

◆ members

members[1]
Initial value:
=
infix_arena_t * arena
Definition 103_unions.c:57
c23_nodiscard void * infix_arena_alloc(infix_arena_t *, size_t, size_t)
Allocates a block of memory from an arena.
Definition arena.c:126
Describes a single member of a C struct or union.
Definition infix.h:277

◆ status

◆ TEST

TEST
Initial value:
{
plan(3)
#define plan(count)
Definition double_tap.h:163

◆ union_type

infix_type* union_type = nullptr