Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add builtin function enum2int #682

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

michalwypustek
Copy link
Collaborator

@michalwypustek michalwypustek commented Nov 16, 2022

enum2int(in Enumerated_type inpar) return integer

This function accepts an enumerated value and returns the integer value associated to the enumerated value. The actual parameter passed to inpar always shall be a typed object.

type enumerated MyFirstEnumType {
    Monday, Tuesday, Wednesday, Thursday, Friday
};
type enumerated MySecondEnumType {
    Saturday(-3), Sunday (0), Monday
};

//within a dynamic language element:
var MyFirstEnumType vl_FirstEnum := Monday;
var MySecondEnumType vl_SecondEnum := Monday;
enum2int(vl_FirstEnum) // returns 0
enum2int(vl_SecondEnum) // returns 1

@@ -308,6 +308,10 @@ func TestBuiltinFunctions(t *testing.T) {
{`type enumerated E1 {red, green, blue}; var E1 testVar := blue; int2enum(0, "wrong")`, runtime.Errorf("second argument must be an enum value")},
{`type enumerated E1 {red, green, blue}; var E1 testVar := blue; int2enum(4, testVar)`, runtime.Errorf("id 4 does not exist in any ranges of Enum E1")},

{`type enumerated E1 {red, green, blue}; var E1 testVar := blue; enum2int(testVar);`, `2`},
{`type enumerated E1 {red, green, blue}; var E1 testVar := blue; enum2int("wrong")`, runtime.Errorf("Argument must be an enum value")},
{`type enumerated E1 {red(10..20), green, blue}; var E1 testVar := red; enum2int(testVar);`, `10`},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enumerated values in TTCN-3 do not only have a name, but also a value (in that sense they are more similar to Rust, than C++):

Suggested change
{`type enumerated E1 {red(10..20), green, blue}; var E1 testVar := red; enum2int(testVar);`, `10`},
{`type enumerated E1 {red(10..20), green, blue}; var E1 testVar := red(15); enum2int(testVar);`, `15`},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants