Sha256: a091e23bba3e213baf25927f9070f72d19b6e095660466aae9821bdc070687da

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

const std = @import("std");
const testing = std.testing;

const ruby = @cImport(@cInclude("ruby/ruby.h"));
const clib = @cImport(@cInclude("lucas_lehmer.h"));

fn rb_lucas_lehmer(...) callconv(.C) ruby.VALUE {
    var ap = @cVaStart();
    defer @cVaEnd(&ap);

    // first argument is `self` in ruby; discard it
    var self = @cVaArg(&ap, ruby.VALUE); _ = self;

    // back and forth type conversions + delegation
    var p = ruby.NUM2ULONG(@cVaArg(&ap, ruby.VALUE));
    return if (clib.lucas_lehmer(p) == 1) ruby.Qtrue else ruby.Qfalse;
}

export fn Init_libzigrb_lucas_lehmer() void {
    ruby.ruby_init();
    var zig_rb_class: ruby.VALUE = ruby.rb_define_class("ZigExample", ruby.rb_cObject);
    _ = ruby.rb_define_method(zig_rb_class, "lucas_lehmer", rb_lucas_lehmer, 1);
}

test "lucas_lehmer 1 passes" {
    try testing.expect(clib.lucas_lehmer(1) == 0);
}
test "lucas_lehmer 2 passes" {
    try testing.expect(clib.lucas_lehmer(2) == 1);
}
test "lucas_lehmer 3 passes" {
    try testing.expect(clib.lucas_lehmer(3) == 1);
}
test "lucas_lehmer 4 passes" {
    try testing.expect(clib.lucas_lehmer(4) == 0);
}
test "lucas_lehmer 5 passes" {
    try testing.expect(clib.lucas_lehmer(5) == 1);
}
test "lucas_lehmer 11 passes" {
    try testing.expect(clib.lucas_lehmer(11) == 0);
}

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
zig_example-0.4.0 ext/zigrb_lucas_lehmer/src/wrapper.zig
zig_example-0.4.0.pre ext/zigrb_lucas_lehmer/src/wrapper.zig
zig_example-0.3.4 ext/zigrb_lucas_lehmer/src/wrapper.zig
zig_example-0.3.3.1 ext/zigrb_lucas_lehmer/src/wrapper.zig
zig_example-0.3.2 ext/zigrb_lucas_lehmer/src/wrapper.zig
zig_example-0.3.1 ext/zigrb_lucas_lehmer/src/wrapper.zig
zig_example-0.3.0 ext/zigrb_lucas_lehmer/src/wrapper.zig