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

Release driver lock on return from reset_driver_and_replace_camera #200

Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/dcam.driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ reset_driver_and_replace_camera(struct Dcam4Camera* self)

// try to stop all the associated cameras
for (int i = 0; i < ncameras; ++i) {
camera_stop(&driver->cameras[i]->camera);
CHECK(Device_Ok == camera_stop(&driver->cameras[i]->camera));
}

// save properties
Expand Down Expand Up @@ -327,6 +327,7 @@ reset_driver_and_replace_camera(struct Dcam4Camera* self)
driver->cameras[i]->camera.device.identifier.name,
sizeof(self_name))) {
*self = *driver->cameras[i];
lock_release(&driver->lock);
return driver->cameras[i];
aliddell marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
99 changes: 99 additions & 0 deletions tests/unit-tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// This is a "unit test" driver.
//
// Adding unit test functions here will run them as part of the CTest suite
// in a standardized fashion.
//
// Unit tests should be focused on testing the smallest logically isolated
// parts of the code. Practically, this means they should live close to the
// code they're testing. That is usually under the public interface
// defined by this module - if you're test uses a private interface that's a
// good sign it might be a unit test.
//
// Adding a new unit test:
// 1. Define your unit test in the same source file as what you're testing.
// 2. Add it to the declarations list below. See TEST DECLARATIONS.
// 3. Add it to the test list. See TEST LIST.
//
// Template:
//
// ```c
// #ifndef NO_UNIT_TESTS
// int
// unit_test__my_descriptive_test_name()
// {
// // do stuff
// return 1; // success
// Error:
// return 0; // failure
// }
// #endif // NO_UNIT_TESTS
// ```

#include "platform.h"
#include "logger.h"

#include <cstdio>
#include <stdexcept>
#include <vector>

#define L (aq_logger)
#define LOG(...) L(0, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define ERR(...) L(1, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)

void
reporter(int is_error,
const char* file,
int line,
const char* function,
const char* msg)
{
fprintf(is_error ? stderr : stdout,
"%s%s(%d) - %s: %s\n",
is_error ? "ERROR " : "",
file,
line,
function,
msg);
}

typedef struct Driver* (*init_func_t)(void (*reporter)(int is_error,
const char* file,
int line,
const char* function,
const char* msg));
//
// TEST DRIVER
//

int
main()
{
logger_set_reporter(reporter);
struct lib lib = { 0 };
if (!lib_open_by_name(&lib, "acquire-driver-hdcam")) {
ERR("Failed to open \"acquire-driver-hdcam\".");
exit(2);
}

struct testcase
{
const char* name;
int (*test)();
};
const std::vector<testcase> tests{
#define CASE(e) { .name = #e, .test = (int (*)())lib_load(&lib, #e) }
CASE(unit_test__reset_driver_and_replace_camera),
#undef CASE
};

bool any = false;
for (const auto& test : tests) {
LOG("Running %s", test.name);
if (!(test.test())) {
ERR("unit test failed: %s", test.name);
any = true;
}
}
lib_close(&lib);
return any;
}
Loading