summary refs log tree commit diff stats
path: root/test/tc_history.py
blob: ad340992bc064ac44fe2c4b8db05ee4ed77b70f4 (plain) (blame)
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
39
40
41
if __name__ == '__main__': from __init__ import init; init()

from ranger.container import History
from unittest import TestCase, main
import unittest

class Test(TestCase):
	def test_everything(self):
		hist = History(3)
		for i in range(6):
			hist.add(i)
		self.assertEqual([3,4,5], list(hist))

		hist.back()

		self.assertEqual(4, hist.top())
		self.assertEqual([3,4], list(hist))

		hist.back()
		self.assertEqual(3, hist.top())
		self.assertEqual([3], list(hist))

		# no change if top == bottom
		self.assertEqual(hist.top(), hist.bottom())
		last_top = hist.top()
		hist.back()
		self.assertEqual(hist.top(), last_top)


		hist.forward()
		hist.forward()
		self.assertEqual(5, hist.top())
		self.assertEqual([3,4,5], list(hist))


		self.assertEqual(3, hist.bottom())
		hist.add(6)
		self.assertEqual(4, hist.bottom())
		self.assertEqual([4,5,6], list(hist))

if __name__ == '__main__': main()
d='n388' href='#n388'>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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608































































































































































































































































































































































































































































































































































































































                                                                               
/*
 * A test program to make sure that dirent works correctly.
 *
 * Copyright (C) 1998-2019 Toni Ronkko
 * This file is part of dirent.  Dirent may be freely distributed
 * under the MIT license.  For all details and documentation, see
 * https://github.com/tronkko/dirent
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#	include <direct.h>
#	define chdir(x) _chdir(x)
#else
#	include <unistd.h>
#endif
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>

#undef NDEBUG
#include <assert.h>

static void test_macros(void);
static void test_retrieval(void);
static void test_nonexistent(void);
static void test_isfile(void);
static void test_zero(void);
static void test_rewind(void);
static void test_chdir(void);
static void test_filename(void);
static void test_readdir(void);
static void test_wreaddir(void);

int
main(int argc, char *argv[])
{
	(void) argc;
	(void) argv;

	/* Execute tests */
	test_macros();
	test_retrieval();
	test_nonexistent();
	test_isfile();
	test_zero();
	test_rewind();
	test_chdir();
	test_filename();
	test_readdir();
	test_wreaddir();

	printf("OK\n");
	return EXIT_SUCCESS;
}

/* Test file type macros */
static void
test_macros(void)
{
	assert(DTTOIF(DT_REG) == S_IFREG);
	assert(DTTOIF(DT_DIR) == S_IFDIR);
	assert(DTTOIF(DT_FIFO) == S_IFIFO);
	assert(DTTOIF(DT_SOCK) == S_IFSOCK);
	assert(DTTOIF(DT_CHR) == S_IFCHR);
	assert(DTTOIF(DT_BLK) == S_IFBLK);

	assert(IFTODT(S_IFREG) == DT_REG);
	assert(IFTODT(S_IFDIR) == DT_DIR);
	assert(IFTODT(S_IFIFO) == DT_FIFO);
	assert(IFTODT(S_IFSOCK) == DT_SOCK);
	assert(IFTODT(S_IFCHR) == DT_CHR);
	assert(IFTODT(S_IFBLK) == DT_BLK);
}

/* Test basic directory retrieval */
static void
test_retrieval(void)
{
	/* Open directory */
	DIR *dir = opendir("tests/1");
	if (dir == NULL) {
		fprintf(stderr, "Directory tests/1 not found\n");
		abort();
	}

	/* Read entries */
	struct dirent *ent;
	int found = 0;
	while ((ent = readdir(dir)) != NULL) {
		/* Check each file */
		if (strcmp(ent->d_name, ".") == 0) {
			/* Directory itself */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 1);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 1);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 1);
#endif
			found += 1;
		} else if (strcmp(ent->d_name, "..") == 0) {
			/* Parent directory */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 2);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 2);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 2);
#endif
			found += 2;
		} else if (strcmp(ent->d_name, "file") == 0) {
			/* Regular file */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_REG);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 4);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 4);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 4);
#endif
			found += 4;
		} else if (strcmp(ent->d_name, "dir") == 0) {
			/* Just a directory */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 3);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 3);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 3);
#endif
			found += 8;
		} else {
			/* Other file */
			fprintf(stderr, "Unexpected file %s\n", ent->d_name);
			abort();
		}
	}

	/* Make sure that all files were found */
	assert(found == 0xf);

	closedir(dir);
}

/* Function opendir() fails if directory doesn't exist */
static void
test_nonexistent(void)
{
	DIR *dir = opendir("tests/invalid");
	assert(dir == NULL);
	assert(errno == ENOENT);
}

/* Function opendir() fails if pathname is really a file */
static void
test_isfile(void)
{
	DIR *dir = opendir("tests/1/file");
	assert(dir == NULL);
	assert(errno == ENOTDIR);
}

/* Function opendir() fails if pathname is a zero-length string */
static void
test_zero(void)
{
	DIR *dir = opendir("");
	assert(dir == NULL);
	assert(errno == ENOENT);
}

/* Test rewind of directory stream */
static void
test_rewind(void)
{
	/* Open directory */
	DIR *dir = opendir("tests/1");
	assert(dir != NULL);

	/* Read entries */
	int found = 0;
	struct dirent *ent;
	while ((ent = readdir(dir)) != NULL) {
		/* Check each file */
		if (strcmp(ent->d_name, ".") == 0) {
			/* Directory itself */
			found += 1;
		} else if (strcmp(ent->d_name, "..") == 0) {
			/* Parent directory */
			found += 2;
		} else if (strcmp(ent->d_name, "file") == 0) {
			/* Regular file */
			found += 4;
		} else if (strcmp(ent->d_name, "dir") == 0) {
			/* Just a directory */
			found += 8;
		} else {
			/* Other file */
			fprintf(stderr, "Unexpected file %s\n", ent->d_name);
			abort();
		}
	}

	/* Make sure that all files were found */
	assert(found == 0xf);

	/* Rewind stream and read entries again */
	rewinddir(dir);
	found = 0;

	/* Read entries */
	while ((ent = readdir(dir)) != NULL) {
		/* Check each file */
		if (strcmp(ent->d_name, ".") == 0) {
			/* Directory itself */
			found += 1;
		} else if (strcmp(ent->d_name, "..") == 0) {
			/* Parent directory */
			found += 2;
		} else if (strcmp(ent->d_name, "file") == 0) {
			/* Regular file */
			found += 4;
		} else if (strcmp(ent->d_name, "dir") == 0) {
			/* Just a directory */
			found += 8;
		} else {
			/* Other file */
			fprintf(stderr, "Unexpected file %s\n", ent->d_name);
			abort();
		}
	}

	/* Make sure that all files were found */
	assert(found == 0xf);

	closedir(dir);
}

/* Test rewind with intervening change of working directory */
static void
test_chdir(void)
{
	/* Open directory */
	DIR *dir = opendir("tests/1");
	assert(dir != NULL);

	/* Read entries */
	struct dirent *ent;
	int found = 0;
	while ((ent = readdir(dir)) != NULL) {
		/* Check each file */
		if (strcmp(ent->d_name, ".") == 0) {
			/* Directory itself */
			found += 1;
		} else if (strcmp(ent->d_name, "..") == 0) {
			/* Parent directory */
			found += 2;
		} else if (strcmp(ent->d_name, "file") == 0) {
			/* Regular file */
			found += 4;
		} else if (strcmp(ent->d_name, "dir") == 0) {
			/* Just a directory */
			found += 8;
		} else {
			/* Other file */
			fprintf(stderr, "Unexpected file %s\n", ent->d_name);
			abort();
		}

	}

	/* Make sure that all files were found */
	assert(found == 0xf);

	/* Change working directory */
	int errorcode = chdir("tests");
	assert(errorcode == 0);

	/* Rewind stream and read entries again */
	rewinddir(dir);
	found = 0;

	/* Read entries */
	while ((ent = readdir(dir)) != NULL) {
		/* Check each file */
		if (strcmp(ent->d_name, ".") == 0) {
			/* Directory itself */
			found += 1;
		} else if (strcmp(ent->d_name, "..") == 0) {
			/* Parent directory */
			found += 2;
		} else if (strcmp(ent->d_name, "file") == 0) {
			/* Regular file */
			found += 4;
		} else if (strcmp(ent->d_name, "dir") == 0) {
			/* Just a directory */
			found += 8;
		} else {
			/* Other file */
			fprintf(stderr, "Unexpected file %s\n", ent->d_name);
			abort();
		}
	}

	/* Make sure that all files were found */
	assert(found == 0xf);

	/* Restore working directory */
	errorcode = chdir("..");
	assert(errorcode == 0);

	closedir(dir);
}

/* Test long file name */
static void
test_filename(void)
{
	/* Open directory */
	DIR *dir = opendir("tests/2");
	if (dir == NULL) {
		fprintf(stderr, "Directory tests/2 not found\n");
		abort();
	}

	/* Read entries */
	struct dirent *ent;
	int found = 0;
	while ((ent = readdir(dir)) != NULL) {
		/* Check each file */
		if (strcmp(ent->d_name, ".") == 0) {
			/* Directory itself */
			found += 1;
		} else if (strcmp(ent->d_name, "..") == 0) {
			/* Parent directory */
			found += 2;
		} else if (strcmp(ent->d_name, "file.txt") == 0) {
			/* Regular 8+3 filename */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_REG);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 8);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 8);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 8);
#endif
			found += 4;
		} else if (strcmp(ent->d_name, "Testfile-1.2.3.dat") == 0) {
			/* Long file name with multiple dots */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_REG);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 18);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 18);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 18);
#endif
			found += 8;
		} else {
			/* Other file */
			fprintf(stderr, "Unexpected file %s\n", ent->d_name);
			abort();
		}
	}

	/* Make sure that all files were found */
	assert(found == 0xf);

	closedir(dir);
}

/* Test basic directory retrieval with readdir_r */
static void
test_readdir(void)
{
	/* Open directory */
	DIR *dir = opendir("tests/1");
	if (dir == NULL) {
		fprintf(stderr, "Directory tests/1 not found\n");
		abort();
	}

	/* Read entries to table */
	struct dirent ent[10];
	struct dirent *entry;
	size_t i = 0;
	size_t n = 0;
	while (readdir_r(dir, &ent[n], &entry) == /*OK*/0 && entry != 0) {
		n++;
		assert(n <= 4);
	}

	/* Make sure that we got all the files from directory */
	assert(n == 4);

	/* Check entries in memory */
	int found = 0;
	for (i = 0; i < 4; i++) {
		entry = &ent[i];

		/* Check each file */
		if (strcmp(entry->d_name, ".") == 0) {
			/* Directory itself */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(entry->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(entry->d_namlen == 1);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(entry) == 1);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(entry) > 1);
#endif
			found += 1;
		} else if (strcmp(entry->d_name, "..") == 0) {
			/* Parent directory */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(entry->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(entry->d_namlen == 2);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(entry) == 2);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(entry) > 2);
#endif
			found += 2;
		} else if (strcmp(entry->d_name, "file") == 0) {
			/* Regular file */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(entry->d_type == DT_REG);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(entry->d_namlen == 4);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(entry) == 4);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(entry) > 4);
#endif
			found += 4;
		} else if (strcmp(entry->d_name, "dir") == 0) {
			/* Just a directory */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(entry->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(entry->d_namlen == 3);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(entry) == 3);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(entry) > 3);
#endif
			found += 8;
		} else {
			/* Other file */
			fprintf(stderr, "Unexpected file %s\n", entry->d_name);
			abort();
		}

	}

	/* Make sure that all files were found */
	assert(found == 0xf);

	closedir(dir);
}

/* Basic directory retrieval with _wreaddir_r */
static void
test_wreaddir(void)
{
#ifdef WIN32
	/* Open directory */
	_WDIR *dir = _wopendir(L"tests/1");
	if (dir == NULL) {
		fprintf(stderr, "Directory tests/1 not found\n");
		abort();
	}

	/* Read entries to table */
	struct _wdirent ent[10];
	struct _wdirent *entry;
	size_t i = 0;
	size_t n = 0;
	while (_wreaddir_r(dir, &ent[n], &entry) == /*OK*/0 && entry != 0) {
		n++;
		assert(n <= 4);
	}

	/* Make sure that we got all the files from directory */
	assert(n == 4);

	/* Check entries in memory */
	int found = 0;
	for (i = 0; i < 4; i++) {
		entry = &ent[i];

		/* Check each file */
		if (wcscmp(entry->d_name, L".") == 0) {
			/* Directory itself */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(entry->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(entry->d_namlen == 1);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(entry) == 1);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(entry) > 1);
#endif
			found += 1;
		} else if (wcscmp(entry->d_name, L"..") == 0) {
			/* Parent directory */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(entry->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(entry->d_namlen == 2);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(entry) == 2);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(entry) > 2);
#endif
			found += 2;
		} else if (wcscmp(entry->d_name, L"file") == 0) {
			/* Regular file */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(entry->d_type == DT_REG);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(entry->d_namlen == 4);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(entry) == 4);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(entry) > 4);
#endif
			found += 4;
		} else if (wcscmp(entry->d_name, L"dir") == 0) {
			/* Just a directory */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(entry->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(entry->d_namlen == 3);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(entry) == 3);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(entry) > 3);
#endif
			found += 8;
		} else {
			/* Other file */
			fprintf(stderr, "Unexpected file\n");
			abort();
		}
	}

	/* Make sure that all files were found */
	assert(found == 0xf);

	_wclosedir(dir);
#endif
}