Skip to content

Commit

Permalink
add C test, it's a clone of Python test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robadob committed Sep 26, 2023
1 parent 97a3fa3 commit 7539dc2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
10 changes: 10 additions & 0 deletions include/flamegpu/runtime/detail/curve/DeviceCurve.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ __device__ __forceinline__ char* DeviceCurve::getEnvironmentMacroProperty(const
return getVariablePtr<T, I*J*K*W>(name, Curve::variableHash("_macro_environment"), 0);
}

// https://stackoverflow.com/a/34873763/1646387
__device__ __forceinline__ int strcmp(const char *s1, const char *s2) {
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;

while(*p1 && *p1 == *p2) ++p1, ++p2;

Check failure on line 424 in include/flamegpu/runtime/detail/curve/DeviceCurve.cuh

View workflow job for this annotation

GitHub Actions / cpplint (11.8, ubuntu-20.04)

Missing space before ( in while(

return (*p1 > *p2) - (*p2 > *p1);
}

__device__ __forceinline__ bool DeviceCurve::isAgent(const char* agent_name) {
return strcmp(agent_name, "todo") == 0; // @todo
}
Expand Down
9 changes: 3 additions & 6 deletions tests/python/runtime/test_device_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class DeviceAPITest(TestCase):
return flamegpu::ALIVE;
}
"""



agent_fn_check_agent_name_state = """
FLAMEGPU_AGENT_FUNCTION(check_agent_name_state, flamegpu::MessageNone, flamegpu::MessageNone){
FLAMEGPU->setVariable<int>("correct_name", static_cast<int>(FLAMEGPU->isAgent("agent")));
Expand Down Expand Up @@ -104,7 +103,6 @@ def test_agent_death_array(self):
assert output_array[1] == 4 + j
assert output_array[2] == 8 + j
assert output_array[3] == 16 + j



def test_array_set(self):
Expand Down Expand Up @@ -148,7 +146,6 @@ def test_array_set(self):
assert output_array[1] == 4 + j
assert output_array[2] == 8 + j
assert output_array[3] == 16 + j



def test_array_get(self):
Expand Down Expand Up @@ -196,8 +193,8 @@ def test_array_get(self):
assert instance.getVariableInt("a2") == 4 + j
assert instance.getVariableInt("a3") == 8 + j
assert instance.getVariableInt("a4") == 16 + j


def test_check_agent_name_state(self):
model = pyflamegpu.ModelDescription("test_array_get")
agent = model.newAgent("agent")
Expand Down
43 changes: 43 additions & 0 deletions tests/test_cases/runtime/test_device_api.cu
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,48 @@ TEST(DeviceAPITest, getStepCounterFunctionCondition) {
}
}

FLAMEGPU_AGENT_FUNCTION(check_agent_name_state_fn, MessageNone, MessageNone) {
FLAMEGPU->setVariable<int>("correct_name", static_cast<int>(FLAMEGPU->isAgent("agent")));
FLAMEGPU->setVariable<int>("wrong_name", static_cast<int>(FLAMEGPU->isAgent("agent3")));
FLAMEGPU->setVariable<int>("correct_state", static_cast<int>(FLAMEGPU->isState("state")));
FLAMEGPU->setVariable<int>("wrong_state", static_cast<int>(FLAMEGPU->isState("state5")));
return ALIVE;
}

TEST(DeviceAPITest, check_agent_name_state) {
ModelDescription model("model");
AgentDescription agent = model.newAgent("agent");
agent.newState("state");
agent.newState("state8");
agent.newVariable<int>("correct_name", -1);
agent.newVariable<int>("wrong_name", -1);
agent.newVariable<int>("correct_state", -1);
agent.newVariable<int>("wrong_state", -1);
// Do nothing, but ensure variables are made available on device
AgentFunctionDescription func = agent.newFunction("some_function", check_agent_name_state_fn);
model.newLayer().addAgentFunction(func);
// Init pop
const unsigned int agentCount = 100;
AgentVector init_population(agent, agentCount);

Check failure on line 388 in tests/test_cases/runtime/test_device_api.cu

View workflow job for this annotation

GitHub Actions / cpplint (11.8, ubuntu-20.04)

Line ends in whitespace. Consider deleting these extra spaces.
// Setup Model
CUDASimulation cudaSimulation(model);
cudaSimulation.setPopulationData(init_population, "state");

Check failure on line 392 in tests/test_cases/runtime/test_device_api.cu

View workflow job for this annotation

GitHub Actions / cpplint (11.8, ubuntu-20.04)

Line ends in whitespace. Consider deleting these extra spaces.
// Run 1 step to ensure data is pushed to device
cudaSimulation.step();
// Recover data from device
AgentVector population(agent, AGENT_COUNT);
cudaSimulation.getPopulationData(population, "state");
// Check results are correct
EXPECT_EQ(population.size(), AGENT_COUNT);
for (const auto &instance : population) {
EXPECT_EQ(instance.getVariable<int>("correct_name"), 1);
EXPECT_EQ(instance.getVariable<int>("wrong_name"), 0);
EXPECT_EQ(instance.getVariable<int>("correct_state"), 1);
EXPECT_EQ(instance.getVariable<int>("wrong_state"), 0);
}
}

} // namespace test_device_api
} // namespace flamegpu

0 comments on commit 7539dc2

Please sign in to comment.