summaryrefslogtreecommitdiff
path: root/test/libtest/test.h
blob: ec08bf97c03d563ea6f4aaabda7a1ea3c7abbb03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2018, Google Inc.
 *
 * test.h - libcamera test base class
 */
#ifndef __TEST_TEST_H__
#define __TEST_TEST_H__

#include <sstream>

enum TestStatus {
	TestPass = 0,
	TestFail = -1,
	TestSkip = 77,
};

class Test
{
public:
	Test();
	virtual ~Test();

	int execute();

protected:
	virtual int init() { return 0; }
	virtual int run() = 0;
	virtual void cleanup() { }
};

#define TEST_REGISTER(klass)						\
int main(int argc, char *argv[])					\
{									\
	return klass().execute();					\
}

#endif /* __TEST_TEST_H__ */
>BufferWriter::BufferWriter(const std::string &pattern) : pattern_(pattern) { } BufferWriter::~BufferWriter() { for (auto &iter : mappedBuffers_) { void *memory = iter.second.first; unsigned int length = iter.second.second; munmap(memory, length); } mappedBuffers_.clear(); } void BufferWriter::mapBuffer(FrameBuffer *buffer) { for (const FrameBuffer::Plane &plane : buffer->planes()) { void *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED, plane.fd.fd(), 0); mappedBuffers_[plane.fd.fd()] = std::make_pair(memory, plane.length); } } int BufferWriter::write(FrameBuffer *buffer, const std::string &streamName) { std::string filename; size_t pos; int fd, ret = 0; filename = pattern_; pos = filename.find_first_of('#'); if (pos != std::string::npos) { std::stringstream ss; ss << streamName << "-" << std::setw(6) << std::setfill('0') << buffer->metadata().sequence; filename.replace(pos, 1, ss.str()); } fd = open(filename.c_str(), O_CREAT | O_WRONLY | (pos == std::string::npos ? O_APPEND : O_TRUNC), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (fd == -1) return -errno; for (unsigned int i = 0; i < buffer->planes().size(); ++i) { const FrameBuffer::Plane &plane = buffer->planes()[i]; const FrameMetadata::Plane &meta = buffer->metadata().planes[i]; void *data = mappedBuffers_[plane.fd.fd()].first; unsigned int length = std::min(meta.bytesused, plane.length); if (meta.bytesused > plane.length) std::cerr << "payload size " << meta.bytesused << " larger than plane size " << plane.length << std::endl; ret = ::write(fd, data, length); if (ret < 0) { ret = -errno; std::cerr << "write error: " << strerror(-ret) << std::endl; break; } else if (ret != (int)length) { std::cerr << "write error: only " << ret << " bytes written instead of " << length << std::endl; break; } } close(fd); return ret; }