Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic test suite, number tests, list tests
- Loading branch information
Showing
10 changed files
with
110 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1. Bump rustler, rustler_codegen, rustler_mix (including lib/rustler.ex), installer template version numbers | ||
2. Publish crates, rustler_mix, installer | ||
3. Update documentation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![feature(plugin)] | ||
#![plugin(rustler_codegen)] | ||
|
||
#[macro_use] | ||
extern crate rustler; | ||
|
||
mod test_primitives; | ||
use test_primitives::{add_u32, add_i32, tuple_add}; | ||
|
||
mod test_list; | ||
use test_list::{sum_list}; | ||
|
||
rustler_export_nifs!( | ||
"Elixir.TestNative", | ||
[("add_u32", 2, add_u32), | ||
("add_i32", 2, add_i32), | ||
("tuple_add", 1, tuple_add), | ||
("sum_list", 1, sum_list)], | ||
None | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use rustler; | ||
use rustler::{NifEnv, NifTerm, NifError, NifEncoder, NifResult}; | ||
use rustler::list::NifListIterator; | ||
|
||
pub fn sum_list<'a>(env: &'a NifEnv, args: &Vec<NifTerm>) -> NifResult<NifTerm<'a>> { | ||
let iter: NifListIterator = try!(args[0].decode()); | ||
|
||
let res: Result<Vec<i64>, NifError> = iter | ||
.map(|x| x.decode::<i64>()) | ||
.collect(); | ||
|
||
match res { | ||
Ok(result) => Ok(result.iter().fold(0, |acc, &x| acc + x).encode(env)), | ||
Err(err) => Err(err), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use rustler; | ||
use rustler::{NifEnv, NifTerm, NifError, NifEncoder, NifResult}; | ||
|
||
pub fn add_u32<'a>(env: &'a NifEnv, args: &Vec<NifTerm>) -> NifResult<NifTerm<'a>> { | ||
let lhs: u32 = try!(args[0].decode()); | ||
let rhs: u32 = try!(args[1].decode()); | ||
|
||
Ok((lhs + rhs).encode(env)) | ||
} | ||
pub fn add_i32<'a>(env: &'a NifEnv, args: &Vec<NifTerm>) -> NifResult<NifTerm<'a>> { | ||
let lhs: i32 = try!(args[0].decode()); | ||
let rhs: i32 = try!(args[1].decode()); | ||
|
||
Ok((lhs + rhs).encode(env)) | ||
} | ||
|
||
#[NifTuple] struct AddTuple { lhs: i32, rhs: i32 } | ||
pub fn tuple_add<'a>(env: &'a NifEnv, args: &Vec<NifTerm>) -> NifResult<NifTerm<'a>> { | ||
let tuple: AddTuple = try!(args[0].decode()); | ||
Ok((tuple.lhs + tuple.rhs).encode(env)) | ||
} | ||
|
||
pub fn echo_u8<'a>(env: &'a NifEnv, args: &Vec<NifTerm>) -> NifResult<NifTerm<'a>> { | ||
let num: u8 = try!(args[0].decode()); | ||
Ok(num.encode(env)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule ListTest do | ||
use ExUnit.Case, async: true | ||
|
||
test "list iteration" do | ||
assert 8 == TestNative.sum_list([1, 2, 1, 4]) | ||
end | ||
|
||
test "list iteration fails on improper lists" do | ||
assert_raise ErlangError, fn -> TestNative.sum_list([1, 4, 2 | :invalid]) end | ||
end | ||
|
||
test "list iteration fails on invalid entries" do | ||
assert_raise ArgumentError, fn -> TestNative.sum_list([1, 4, :invalid, 2]) end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
defmodule PrimitivesTest do | ||
use ExUnit.Case, async: true | ||
use TestLoadNif, [ | ||
module: PrimitivesTestNative, | ||
native: "test_primitives", | ||
functions: [ | ||
add_u32: 2, | ||
add_i32: 2, | ||
tuple_add: 1, | ||
], | ||
] | ||
|
||
test "u8 decoding and encoding" do | ||
PrimitivesTestNative.test(1, 2) | ||
test "number decoding and encoding" do | ||
assert 3 == TestNative.add_u32(1, 2) | ||
assert 3 == TestNative.add_i32(6, -3) | ||
assert -3 == TestNative.add_i32(3, -6) | ||
end | ||
|
||
test "number decoding should fail on invalid terms" do | ||
assert_raise ArgumentError, fn -> TestNative.add_u32(-1, 1) end | ||
assert_raise ArgumentError, fn -> TestNative.add_u32("1", 1) end | ||
assert_raise ArgumentError, fn -> TestNative.add_i32(2147483648, 1) end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.