summaryrefslogtreecommitdiff
path: root/src/android
ModeNameSize
-rw-r--r--camera3_hal.cpp2659logplain
-rw-r--r--camera_device.cpp51251logplain
-rw-r--r--camera_device.h4141logplain
-rw-r--r--camera_hal_manager.cpp6287logplain
-rw-r--r--camera_hal_manager.h1503logplain
-rw-r--r--camera_metadata.cpp1870logplain
-rw-r--r--camera_metadata.h752logplain
-rw-r--r--camera_ops.cpp2347logplain
-rw-r--r--camera_ops.h361logplain
d---------jpeg199logplain
-rw-r--r--meson.build837logplain
d---------metadata99logplain
/a> 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2022, Google Inc.
 *
 * yaml-parser.cpp - YAML parser operations tests
 */

#include <array>
#include <iostream>
#include <map>
#include <string>
#include <unistd.h>

#include <libcamera/base/file.h>
#include <libcamera/base/utils.h>

#include "libcamera/internal/yaml_parser.h"

#include "test.h"

using namespace libcamera;
using namespace std;

static const string testYaml =
	"string: libcamera\n"
	"double: 3.14159\n"
	"uint32_t: 100\n"
	"int32_t: -100\n"
	"size: [1920, 1080]\n"
	"list:\n"
	"  - James\n"
	"  - Mary\n"
	"dictionary:\n"
	"  a: 1\n"
	"  c: 3\n"
	"  b: 2\n"
	"level1:\n"
	"  level2:\n"
	"    - [1, 2]\n"
	"    - {one: 1, two: 2}\n";

static const string invalidYaml =
	"Invalid : - YAML : - Content";

class YamlParserTest : public Test
{
protected:
	bool createFile(const string &content, string &filename)
	{
		filename = "/tmp/libcamera.test.XXXXXX";
		int fd = mkstemp(&filename.front());
		if (fd == -1)
			return false;

		int ret = write(fd, content.c_str(), content.size());
		close(fd);

		if (ret != static_cast<int>(content.size()))
			return false;

		return true;
	}

	int init()
	{
		if (!createFile(testYaml, testYamlFile_))
			return TestFail;

		if (!createFile(invalidYaml, invalidYamlFile_))
			return TestFail;

		return TestPass;
	}

	enum class Type {
		String,
		Int32,
		UInt32,
		Double,
		Size,
		List,
		Dictionary,
	};

	int testObjectType(const YamlObject &obj, const char *name, Type type)
	{
		bool isList = type == Type::List || type == Type::Size;
		bool isScalar = !isList && type != Type::Dictionary;
		bool isInteger = type == Type::Int32 || type == Type::UInt32;
		bool isSigned = type == Type::Int32;

		if ((isScalar && !obj.isValue()) || (!isScalar && obj.isValue())) {
			std::cerr
				<< "Object " << name << " type mismatch when compared to "
				<< "value" << std::endl;
			return TestFail;
		}

		if ((isList && !obj.isList()) || (!isList && obj.isList())) {
			std::cerr
				<< "Object " << name << " type mismatch when compared to "
				<< "list" << std::endl;
			return TestFail;
		}

		if ((type == Type::Dictionary && !obj.isDictionary()) ||
		    (type != Type::Dictionary && obj.isDictionary())) {
			std::cerr
				<< "Object " << name << " type mismatch when compared to "
				<< "dictionary" << std::endl;
			return TestFail;
		}

		if (!isScalar && obj.get<std::string>()) {
			std::cerr
				<< "Object " << name << " didn't fail to parse as "
				<< "string" << std::endl;
			return TestFail;
		}

		if (!isInteger && obj.get<int32_t>()) {
			std::cerr
				<< "Object " << name << " didn't fail to parse as "
				<< "int32_t" << std::endl;
			return TestFail;
		}

		if ((!isInteger || isSigned) && obj.get<uint32_t>()) {
			std::cerr
				<< "Object " << name << " didn't fail to parse as "
				<< "uint32_t" << std::endl;
			return TestFail;
		}

		if (!isInteger && type != Type::Double && obj.get<double>()) {
			std::cerr
				<< "Object " << name << " didn't fail to parse as "
				<< "double" << std::endl;
			return TestFail;
		}

		if (type != Type::Size && obj.get<Size>()) {
			std::cerr
				<< "Object " << name << " didn't fail to parse as "
				<< "Size" << std::endl;
			return TestFail;
		}

		return TestPass;
	}

	int testIntegerObject(const YamlObject &obj, const char *name, Type type,
			      int64_t value)
	{
		uint64_t unsignedValue = static_cast<uint64_t>(value);
		std::string strValue = std::to_string(value);
		bool isSigned = type == Type::Int32;

		/* All integers can be parsed as strings or double. */

		if (obj.get<string>().value_or("") != strValue ||
		    obj.get<string>("") != strValue) {
			std::cerr
				<< "Object " << name << " failed to parse as "
				<< "string" << std::endl;
			return TestFail;
		}

		if (obj.get<double>().value_or(0.0) != value ||
		    obj.get<double>(0.0) != value) {
			std::cerr
				<< "Object " << name << " failed to parse as "
				<< "double" << std::endl;
			return TestFail;
		}

		if (obj.get<int32_t>().value_or(0) != value ||
		    obj.get<int32_t>(0) != value) {
			std::cerr
				<< "Object " << name << " failed to parse as "
				<< "int32_t" << std::endl;
			return TestFail;
		}

		if (!isSigned) {
			if (obj.get<uint32_t>().value_or(0) != unsignedValue ||
			    obj.get<uint32_t>(0) != unsignedValue) {
				std::cerr
					<< "Object " << name << " failed to parse as "
					<< "uint32_t" << std::endl;
				return TestFail;
			}
		}

		return TestPass;
	}

	int run()
	{
		/* Test invalid YAML file */
		File file{ invalidYamlFile_ };
		if (!file.open(File::OpenModeFlag::ReadOnly)) {
			cerr << "Fail to open invalid YAML file" << std::endl;
			return TestFail;
		}

		std::unique_ptr<YamlObject> root = YamlParser::parse(file);
		if (root) {
			cerr << "Invalid YAML file parse successfully" << std::endl;
			return TestFail;
		}

		/* Test YAML file */
		file.close();
		file.setFileName(testYamlFile_);
		if (!file.open(File::OpenModeFlag::ReadOnly)) {
			cerr << "Fail to open test YAML file" << std::endl;
			return TestFail;
		}

		root = YamlParser::parse(file);

		if (!root) {
			cerr << "Fail to parse test YAML file: " << std::endl;
			return TestFail;
		}

		if (!root->isDictionary()) {
			cerr << "YAML root is not dictionary" << std::endl;
			return TestFail;
		}

		std::vector<const char *> rootElemNames = {
			"string", "double", "int32_t", "uint32_t", "size",
			"list", "dictionary", "level1",
		};

		for (const char *name : rootElemNames) {
			if (!root->contains(name)) {
				cerr << "Missing " << name << " object in YAML root"
				     << std::endl;
				return TestFail;
			}
		}

		/* Test string object */
		auto &strObj = (*root)["string"];

		if (testObjectType(strObj, "string", Type::String) != TestPass)
			return TestFail;

		if (strObj.get<string>().value_or("") != "libcamera" ||
		    strObj.get<string>("") != "libcamera") {
			cerr << "String object parse as wrong content" << std::endl;
			return TestFail;
		}

		/* Test int32_t object */
		auto &int32Obj = (*root)["int32_t"];

		if (testObjectType(int32Obj, "int32_t", Type::Int32) != TestPass)
			return TestFail;

		if (testIntegerObject(int32Obj, "int32_t", Type::Int32, -100) != TestPass)
			return TestFail;

		/* Test uint32_t object */
		auto &uint32Obj = (*root)["uint32_t"];

		if (testObjectType(uint32Obj, "uint32_t", Type::UInt32) != TestPass)
			return TestFail;

		if (testIntegerObject(uint32Obj, "uint32_t", Type::UInt32, 100) != TestPass)
			return TestFail;

		/* Test double value */
		auto &doubleObj = (*root)["double"];

		if (testObjectType(doubleObj, "double", Type::Double) != TestPass)
			return TestFail;

		if (doubleObj.get<string>().value_or("") != "3.14159" ||
		    doubleObj.get<string>("") != "3.14159") {
			cerr << "Double object fail to parse as string" << std::endl;