/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2020, Google Inc. * * span.cpp - Span tests */ /* * Include first to ensure the header is self-contained, as there's no span.cpp * in libcamera. */ #include #include #include #include #include "test.h" using namespace std; using namespace libcamera; class SpanTest : public Test { protected: int run() { int i[4]{ 1, 2, 3, 4 }; std::array a{ 1, 2, 3, 4 }; const std::array ca{ 1, 2, 3, 4 }; std::vector v{ 1, 2, 3, 4 }; const std::vector cv{ 1, 2, 3, 4 }; /* * Compile-test construction and usage of spans with static * extent. Commented-out tests are expected not to compile, or * to generate undefined behaviour. */ Span{}; /* Span{}; */ Span{ &i[0], 4 }; Span{ &i[0], &i[3] }; Span{ i }; /* Span{ i }; */ /* Span{ i }; */ Span{ a }; Span{ a }; /* Span{ a }; */ /* Span{ a }; */ Span{ ca }; /* Span{ ca }; */ /* Span{ ca }; */ /* Span{ ca }; */ Span{ v }; Span{ v }; /* Span{ v }; */ Span{ v }; /* Span{ v }; */ /* Span{ v }; */ Span staticSpan{ i }; Span{ staticSpan }; Span{ staticSpan }; /* Span{ staticSpan }; */ staticSpan = Span{ v }; if (*staticSpan.begin() != 1) { std::cout << "Span::begin() failed" << std::endl; return TestFail; } if (*staticSpan.cbegin() != 1) { std::cout << "Span::cbegin() failed" << std::endl; return TestFail; } staticSpan.end(); staticSpan.cend(); if (*staticSpan.rbegin() != 4) { std::cout << "Span::rbegin() failed" << std::endl; return TestFail; } if (*staticSpan.crbegin() != 4) { std::cout << "Span::crbegin() failed" << std::endl; return TestFail; } staticSpan.rend(); staticSpan.crend(); staticSpan.front(); staticSpan.back(); staticSpan[0]; staticSpan.data(); staticSpan.size(); staticSpan.size_bytes(); staticSpan.empty(); staticSpan.first<2>(); staticSpan.first(2); /* staticSpan.first<6>(); */ /* staticSpan.first(6); */ staticSpan.last<2>(); staticSpan.last(2); /* staticSpan.last<6>(); */ /* staticSpan.last(6); */ staticSpan.subspan<1>(); staticSpan.subspan<1, 2>(); staticSpan.subspan(1); staticSpan.subspan(1, 2); /* staticSpan.subspan(2, 4); */ /* * Compile-test construction and usage of spans with dynamic * extent. Commented-out tests are expected not to compile, or * to generate undefined behaviour. */ Span{}; Span{ &i[0], 4 }; Span{ &i[0], &i[3] }; Span{ i }; /* Span{ i }; */ Span{ a }; Span{ a }; /* Span{ a }; */ Span{ ca }; /* Span{ca}; */ /* Span{ca}; */ Span{ v }; Span{ v }; /* Span{ v }; */ Span{ v }; /* Span{ v }; */ /* Span{ v }; */ Span dynamicSpan{ i }; Span{ dynamicSpan }; Span{ dynamicSpan }; dynamicSpan = Span{ a }; if (*dynamicSpan.begin() != 1) { std::cout << "Span::begin() failed" << std::endl; return TestFail; } if (*dynamicSpan.cbegin() != 1) { std::cout << "Span::cbegin() failed" << std::endl; return TestFail; } dynamicSpan.end(); dynamicSpan.cend(); if (*dynamicSpan.rbegin() != 4) { std::cout << "Span::rbegin() failed" << std::endl; return TestFail; } if (*dynamicSpan.crbegin() != 4) { std::cout << "Span::crbegin() failed" << std::endl; return TestFail; } dynamicSpan.rend(); dynamicSpan.crend(); dynamicSpan.front(); dynamicSpan.back(); dynamicSpan[0]; dynamicSpan.data(); dynamicSpan.size(); dynamicSpan.size_bytes(); dynamicSpan.empty(); dynamicSpan.first<2>(); dynamicSpan.first(2); /* dynamicSpan.first<6>(); */ /* dynamicSpan.first(6); */ dynamicSpan.last<2>(); dynamicSpan.last(2); /* dynamicSpan.last<6>(); */ /* dynamicSpan.last(6); */ dynamicSpan.subspan<1>(); dynamicSpan.subspan<1, 2>(); dynamicSpan.subspan(1); dynamicSpan.subspan(1, 2); /* dynamicSpan.subspan(2, 4); */ return TestPass; } }; TEST_REGISTER(SpanTest) 54' href='#n54'>54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * log.cpp - log API test
 */

#include <algorithm>
#include <fcntl.h>
#include <iostream>
#include <list>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <libcamera/base/log.h>

#include <libcamera/logging.h>

#include "test.h"

using namespace std;
using namespace libcamera;

LOG_DEFINE_CATEGORY(LogAPITest)

class LogAPITest : public Test
{
protected:
	void doLogging()
	{
		logSetLevel("LogAPITest", "DEBUG");
		LOG(LogAPITest, Info) << "good 1";

		logSetLevel("LogAPITest", "WARN");
		LOG(LogAPITest, Info) << "bad";

		logSetLevel("LogAPITest", "ERROR");
		LOG(LogAPITest, Error) << "good 3";
		LOG(LogAPITest, Info) << "bad";

		logSetLevel("LogAPITest", "WARN");
		LOG(LogAPITest, Warning) << "good 5";
		LOG(LogAPITest, Info) << "bad";
	}

	int verifyOutput(istream &is)
	{
		list<int> goodList = { 1, 3, 5 };
		string line;
		while (getline(is, line)) {
			if (goodList.empty()) {
				cout << "Too many log lines" << endl;
				return TestFail;
			}

			unsigned int digit = line.back() - '0';
			unsigned int expect = goodList.front();
			goodList.pop_front();
			if (digit != expect) {
				cout << "Incorrect log line" << endl;
				return TestFail;
			}
		}

		if (!goodList.empty()) {
			cout << "Too few log lines" << endl;
			return TestFail;
		}

		return TestPass;
	}

	int testFile()
	{
		int fd = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
		if (fd < 0) {
			cerr << "Failed to open tmp log file" << endl;
			return TestFail;
		}

		char path[32];
		snprintf(path, sizeof(path), "/proc/self/fd/%u", fd);

		if (logSetFile(path) < 0) {
			cerr << "Failed to set log file" << endl;
			close(fd);
			return TestFail;
		}

		doLogging();

		char buf[1000];
		memset(buf, 0, sizeof(buf));
		lseek(fd, 0, SEEK_SET);
		if (read(fd, buf, sizeof(buf)) < 0) {
			cerr << "Failed to read tmp log file" << endl;
			close(fd);
			return TestFail;
		}
		close(fd);

		istringstream iss(buf);
		return verifyOutput(iss);
	}

	int testStream()
	{
		stringstream log;
		/* Never fails, so no need to check return value */
		logSetStream(&log);

		doLogging();

		return verifyOutput(log);
	}

	int testTarget()
	{
		logSetTarget(LoggingTargetNone);
		logSetLevel("LogAPITest", "DEBUG");
		LOG(LogAPITest, Info) << "don't crash please";

		if (!logSetTarget(LoggingTargetFile))
			return TestFail;

		if (!logSetTarget(LoggingTargetStream))
			return TestFail;

		return TestPass;
	}

	int run() override
	{
		int ret = testFile();
		if (ret != TestPass)
			return TestFail;

		ret = testStream();
		if (ret != TestPass)
			return TestFail;

		ret = testTarget();
		if (ret != TestPass)
			return TestFail;

		return TestPass;
	}
};

TEST_REGISTER(LogAPITest)