Affix

 view release on metacpan or  search on metacpan

t/021_shakedown.t  view on Meta::CPAN

use v5.40;
use lib '../lib', 'lib';
use blib;
use Test2::Tools::Affix qw[:all];
use Affix               qw[:all];
#
my $c_source = <<'END_C';
#include "std.h"
//ext: .c

#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

// Primitives & Globals for Pinning
DLLEXPORT int32_t global_counter = 100;
DLLEXPORT double  global_pi      = 3.14159;
DLLEXPORT char    global_buffer[64] = "Initial";

DLLEXPORT int32_t get_counter() { return global_counter; }
DLLEXPORT void    set_counter(int32_t v) { global_counter = v; }

// Structs & Nested Types
typedef struct {
    int x;
    int y;
} Point;

typedef struct {
    Point top_left;
    Point bottom_right;
    char label[16];
} Rect;

DLLEXPORT int rect_area_val(Rect r) {
    int w = r.bottom_right.x - r.top_left.x;
    int h = r.bottom_right.y - r.top_left.y;
    return w * h;
}

DLLEXPORT void move_rect_ptr(Rect* r, int dx, int dy) {
    if (r) {
        r->top_left.x += dx;
        r->top_left.y += dy;
        r->bottom_right.x += dx;
        r->bottom_right.y += dy;
        snprintf(r->label, 16, "Moved");
    }
}

DLLEXPORT Point return_struct_val(int x, int y) {
    Point p = { x, y };
    return p;
}

// Recursive Linked List
typedef struct Node {
    int value;
    struct Node* next;
} Node;

DLLEXPORT int sum_list(Node* head) {
    int sum = 0;
    while(head) {
        sum += head->value;
        head = head->next;
    }
    return sum;
}
END_C
#
my $lib = compile_ok($c_source);
ok( $lib, "Library compiled at $lib" );
#
typedef Point => Struct [ x => Int, y => Int ];
typedef Rect => Struct [ top_left => Point(), bottom_right => Point(), label => Array [ Char, 16 ] ];
typedef 'Node';
typedef Node => Struct [ value => Int, next => Pointer [ Node() ] ];    # Recursive
#
subtest 'Structs: Value, Pointers, and Write-back' => sub {
    isa_ok my $area = wrap( $lib, 'rect_area_val', [ Rect() ] => Int ), ['Affix'];



( run in 1.288 second using v1.01-cache-2.11-cpan-ceb78f64989 )