Skip to content

Add tests for parameter special cases, fix array defaults #130

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

Merged
merged 1 commit into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions robotpy_build/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ def _resolve_default(self, fn, p, name):
return name

if name and name[0] == "{" and name[-1] == "}":
if p["array"]:
return name
return f"{p['x_type']}{name}"

# if there's a parent, look there
Expand Down Expand Up @@ -393,9 +395,12 @@ def _function_hook(self, fn, data: FunctionData, internal: bool = False):
ptype = "ignored"

elif p.get("force_out") or (
p["pointer"] and not p["constant"] and p["fundamental"]
(p["pointer"] or p["reference"] == 1)
and not p["constant"]
and p["fundamental"]
):
p["x_callname"] = "&%(x_callname)s" % p
if p["pointer"]:
p["x_callname"] = f"&{p['x_callname']}"
ptype = "out"
elif p["array"]:
asz = p.get("array_size", 0)
Expand Down
7 changes: 7 additions & 0 deletions tests/cpp/gen/ft/parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

functions:
fnParamArrayOutWithDefault:
param_override:
x:
default: "{0, 1, 2}"
1 change: 1 addition & 0 deletions tests/cpp/pyproject.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ generate = [
{ ignore = "ignore.h" },
{ lifetime = "lifetime.h" },
{ overloads = "overloads.h" },
{ parameters = "parameters.h" },
{ rename = "rename.h" },
{ subpkg = "subpkg.h" },
{ static_only = "static_only.h" },
Expand Down
10 changes: 10 additions & 0 deletions tests/cpp/rpytest/ft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
fnEmptyDefaultParam,
fnIgnoredParam,
fnOverload,
fnParamArrayOut,
fnParamArrayOutWithDefault,
fnParamFundConstRef,
fnParamFundPtr,
fnParamFundRef,
fnRenamed,
fnRenamedParam,
fnSimpleDefaultParam,
Expand Down Expand Up @@ -116,6 +121,11 @@
"fnEmptyDefaultParam",
"fnIgnoredParam",
"fnOverload",
"fnParamArrayOut",
"fnParamArrayOutWithDefault",
"fnParamFundConstRef",
"fnParamFundPtr",
"fnParamFundRef",
"fnRenamed",
"fnRenamedParam",
"fnSimpleDefaultParam",
Expand Down
43 changes: 43 additions & 0 deletions tests/cpp/rpytest/ft/include/parameters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#pragma once

//
// Various tests related to function parameters
//

// the array parameter is returned
int fnParamArrayOut(int x[3])
{
x[0] = 5;
x[1] = 6;
x[2] = 7;
return 4;
}

// the array parameter is returned, with a default initialization
int fnParamArrayOutWithDefault(int x[3])
{
x[1] = 6;
return 4;
}

// parameters that are pointers and fundamental types are out by default
int fnParamFundPtr(int x, int * y)
{
*y = x + 1;
return x - 1;
}

// parameters that are references and fundamental types are out by default
int fnParamFundRef(int x, int &y)
{
y = x - 1;
return x + 1;
}

// parameters that are const references and fundamental types are inputs
int fnParamFundConstRef(int x, const int &y)
{
return x + y;
}

21 changes: 21 additions & 0 deletions tests/test_ft_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from rpytest import ft


def test_array_out():
assert ft.fnParamArrayOut() == (4, [5, 6, 7])


def test_array_out_w_default():
assert ft.fnParamArrayOutWithDefault() == (4, [0, 6, 2])


def test_fund_ptr():
assert ft.fnParamFundPtr(4) == (3, 5)


def test_fund_ref():
assert ft.fnParamFundRef(6) == (7, 5)


def test_fund_const_ref():
assert ft.fnParamFundConstRef(1, 2) == 3