Skip to content

Commit

Permalink
Update moduledoc and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FelonEkonom committed May 7, 2021
1 parent 94f12a3 commit 1628497
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 152 deletions.
43 changes: 13 additions & 30 deletions lib/unifex/code_generator/base_types/enum.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Unifex.CodeGenerator.BaseTypes.Enum do
use Unifex.CodeGenerator.BaseType
alias Unifex.CodeGenerator.BaseType
alias Unifex.CodeGenerator.{BaseType, BaseTypes}

@enforce_keys [:name, :types]
defstruct @enforce_keys
Expand Down Expand Up @@ -59,8 +59,6 @@ defmodule Unifex.CodeGenerator.BaseTypes.Enum do

@impl BaseType
def generate_arg_parse(arg, var_name, ctx) do
%{postproc_fun: postproc_fun, generator: generator} = ctx

if_statements =
ctx.type_spec.types
|> Enum.map(&Atom.to_string/1)
Expand All @@ -79,20 +77,13 @@ defmodule Unifex.CodeGenerator.BaseTypes.Enum do
char* enum_as_string;
int res = 0;
#{
BaseType.generate_arg_parse(
:atom,
:enum_as_string,
arg,
postproc_fun,
generator,
ctx
)
};
#{if_statements}
if (#{BaseTypes.Atom.NIF.generate_arg_parse(arg, :enum_as_string, ctx)}) {
#{if_statements}
unifex_free((void *) enum_as_string);
if (enum_as_string != NULL) {
unifex_free((void *) enum_as_string);
}
}
res;
})
Expand Down Expand Up @@ -145,8 +136,6 @@ defmodule Unifex.CodeGenerator.BaseTypes.Enum do

@impl BaseType
def generate_arg_parse(arg, var_name, ctx) do
%{postproc_fun: postproc_fun, generator: generator} = ctx

if_statements =
ctx.type_spec.types
|> Enum.map(&Atom.to_string/1)
Expand All @@ -163,20 +152,14 @@ defmodule Unifex.CodeGenerator.BaseTypes.Enum do
({
int res = 1;
char* enum_as_string;
#{
BaseType.generate_arg_parse(
:atom,
:enum_as_string,
arg,
postproc_fun,
generator,
ctx
)
}
#{if_statements}
if (!#{BaseTypes.Atom.CNode.generate_arg_parse(arg, :enum_as_string, ctx)}) {
#{if_statements}
unifex_free((void *) enum_as_string);
if (enum_as_string != NULL) {
unifex_free((void *) enum_as_string);
}
}
res;
})
Expand Down
26 changes: 0 additions & 26 deletions lib/unifex/specs_dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,6 @@ defmodule Unifex.Specs.DSL do
sends {:example_msg :: label, number :: int}
type my_struct :: %My.Struct{
field1: type1,
field2: type2,
...
}
The enum specification should be in the form of
type my_enum :: :option_one | :option_two | :option_three | ...
Struct or enums specified in such way can be used in like any other supported type, E.g.
spec my_function(in_enum :: my_enum) :: {:ok :: label, out_struct :: my_struct}
According to this specification, module `Membrane.Element.Mad.Decoder.Native` should contain 2 functions: `create/0`
and `decode_frame/3` (which is a cpu-bound dirty NIF). The module should use `Unifex.Loader` to provide access to
these functions. What is more, messages of the form `{:example_msg, integer}` can be sent from the native code to
Expand Down
51 changes: 25 additions & 26 deletions test/fixtures/cnode_ref_generated/cnode/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -1211,38 +1211,37 @@ UNIFEX_TERM test_my_enum_caller(UnifexEnv *env, UnifexCNodeInBuff *in_buff) {
if (({
int res = 1;
char *enum_as_string;
if (({

if (!({
enum_as_string = (char *)unifex_alloc(MAXATOMLEN);
ei_decode_atom(in_buff->buff, in_buff->index, enum_as_string);
})) {
result = unifex_raise(env, "Unifex CNode: cannot parse argument "
"'in_enum' of type ':my_enum'");
goto exit_test_my_enum_caller;
}
if (strcmp(enum_as_string, "option_one") == 0) {
in_enum = OPTION_ONE;
res = 0;
}
if (strcmp(enum_as_string, "option_two") == 0) {
in_enum = OPTION_TWO;
res = 0;
}
if (strcmp(enum_as_string, "option_three") == 0) {
in_enum = OPTION_THREE;
res = 0;
}
if (strcmp(enum_as_string, "option_four") == 0) {
in_enum = OPTION_FOUR;
res = 0;
}
if (strcmp(enum_as_string, "option_five") == 0) {
in_enum = OPTION_FIVE;
res = 0;
}

if (strcmp(enum_as_string, "option_one") == 0) {
in_enum = OPTION_ONE;
res = 0;
}
if (strcmp(enum_as_string, "option_two") == 0) {
in_enum = OPTION_TWO;
res = 0;
}
if (strcmp(enum_as_string, "option_three") == 0) {
in_enum = OPTION_THREE;
res = 0;
}
if (strcmp(enum_as_string, "option_four") == 0) {
in_enum = OPTION_FOUR;
res = 0;
}
if (strcmp(enum_as_string, "option_five") == 0) {
in_enum = OPTION_FIVE;
res = 0;
if (enum_as_string != NULL) {
unifex_free((void *)enum_as_string);
}
}

unifex_free((void *)enum_as_string);

res;
})) {
result = unifex_raise(
Expand Down
51 changes: 25 additions & 26 deletions test/fixtures/cnode_ref_generated/cnode/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,38 +1211,37 @@ UNIFEX_TERM test_my_enum_caller(UnifexEnv *env, UnifexCNodeInBuff *in_buff) {
if (({
int res = 1;
char *enum_as_string;
if (({

if (!({
enum_as_string = (char *)unifex_alloc(MAXATOMLEN);
ei_decode_atom(in_buff->buff, in_buff->index, enum_as_string);
})) {
result = unifex_raise(env, "Unifex CNode: cannot parse argument "
"'in_enum' of type ':my_enum'");
goto exit_test_my_enum_caller;
}
if (strcmp(enum_as_string, "option_one") == 0) {
in_enum = OPTION_ONE;
res = 0;
}
if (strcmp(enum_as_string, "option_two") == 0) {
in_enum = OPTION_TWO;
res = 0;
}
if (strcmp(enum_as_string, "option_three") == 0) {
in_enum = OPTION_THREE;
res = 0;
}
if (strcmp(enum_as_string, "option_four") == 0) {
in_enum = OPTION_FOUR;
res = 0;
}
if (strcmp(enum_as_string, "option_five") == 0) {
in_enum = OPTION_FIVE;
res = 0;
}

if (strcmp(enum_as_string, "option_one") == 0) {
in_enum = OPTION_ONE;
res = 0;
}
if (strcmp(enum_as_string, "option_two") == 0) {
in_enum = OPTION_TWO;
res = 0;
}
if (strcmp(enum_as_string, "option_three") == 0) {
in_enum = OPTION_THREE;
res = 0;
}
if (strcmp(enum_as_string, "option_four") == 0) {
in_enum = OPTION_FOUR;
res = 0;
}
if (strcmp(enum_as_string, "option_five") == 0) {
in_enum = OPTION_FIVE;
res = 0;
if (enum_as_string != NULL) {
unifex_free((void *)enum_as_string);
}
}

unifex_free((void *)enum_as_string);

res;
})) {
result = unifex_raise(
Expand Down
43 changes: 21 additions & 22 deletions test/fixtures/nif_ref_generated/nif/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,29 +667,28 @@ static ERL_NIF_TERM export_test_my_enum(ErlNifEnv *env, int argc,
char *enum_as_string;
int res = 0;

if (!unifex_alloc_and_get_atom(env, argv[0], &enum_as_string)) {
result = unifex_raise_args_error(env, "in_enum", ":my_enum");
goto exit_export_test_my_enum;
};

if (strcmp(enum_as_string, "option_one") == 0) {
in_enum = OPTION_ONE;
res = 1;
} else if (strcmp(enum_as_string, "option_two") == 0) {
in_enum = OPTION_TWO;
res = 1;
} else if (strcmp(enum_as_string, "option_three") == 0) {
in_enum = OPTION_THREE;
res = 1;
} else if (strcmp(enum_as_string, "option_four") == 0) {
in_enum = OPTION_FOUR;
res = 1;
} else if (strcmp(enum_as_string, "option_five") == 0) {
in_enum = OPTION_FIVE;
res = 1;
}
if (unifex_alloc_and_get_atom(env, argv[0], &enum_as_string)) {
if (strcmp(enum_as_string, "option_one") == 0) {
in_enum = OPTION_ONE;
res = 1;
} else if (strcmp(enum_as_string, "option_two") == 0) {
in_enum = OPTION_TWO;
res = 1;
} else if (strcmp(enum_as_string, "option_three") == 0) {
in_enum = OPTION_THREE;
res = 1;
} else if (strcmp(enum_as_string, "option_four") == 0) {
in_enum = OPTION_FOUR;
res = 1;
} else if (strcmp(enum_as_string, "option_five") == 0) {
in_enum = OPTION_FIVE;
res = 1;
}

unifex_free((void *)enum_as_string);
if (enum_as_string != NULL) {
unifex_free((void *)enum_as_string);
}
}

res;
})) {
Expand Down
43 changes: 21 additions & 22 deletions test/fixtures/nif_ref_generated/nif/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,29 +667,28 @@ static ERL_NIF_TERM export_test_my_enum(ErlNifEnv *env, int argc,
char *enum_as_string;
int res = 0;

if (!unifex_alloc_and_get_atom(env, argv[0], &enum_as_string)) {
result = unifex_raise_args_error(env, "in_enum", ":my_enum");
goto exit_export_test_my_enum;
};

if (strcmp(enum_as_string, "option_one") == 0) {
in_enum = OPTION_ONE;
res = 1;
} else if (strcmp(enum_as_string, "option_two") == 0) {
in_enum = OPTION_TWO;
res = 1;
} else if (strcmp(enum_as_string, "option_three") == 0) {
in_enum = OPTION_THREE;
res = 1;
} else if (strcmp(enum_as_string, "option_four") == 0) {
in_enum = OPTION_FOUR;
res = 1;
} else if (strcmp(enum_as_string, "option_five") == 0) {
in_enum = OPTION_FIVE;
res = 1;
}
if (unifex_alloc_and_get_atom(env, argv[0], &enum_as_string)) {
if (strcmp(enum_as_string, "option_one") == 0) {
in_enum = OPTION_ONE;
res = 1;
} else if (strcmp(enum_as_string, "option_two") == 0) {
in_enum = OPTION_TWO;
res = 1;
} else if (strcmp(enum_as_string, "option_three") == 0) {
in_enum = OPTION_THREE;
res = 1;
} else if (strcmp(enum_as_string, "option_four") == 0) {
in_enum = OPTION_FOUR;
res = 1;
} else if (strcmp(enum_as_string, "option_five") == 0) {
in_enum = OPTION_FIVE;
res = 1;
}

unifex_free((void *)enum_as_string);
if (enum_as_string != NULL) {
unifex_free((void *)enum_as_string);
}
}

res;
})) {
Expand Down

0 comments on commit 1628497

Please sign in to comment.