aboutsummaryrefslogtreecommitdiffstats
path: root/t/merge.t
diff options
context:
space:
mode:
authorGravatar Steven Schubiger <stsc@refcnt.org> 2015-09-30 16:14:11 +0200
committerGravatar Steven Schubiger <stsc@refcnt.org> 2015-09-30 16:14:11 +0200
commit7f313b1c22e61541aea2bcbe666639e05e69c9ad (patch)
tree18c7f55a23e8c53cbbcbc3270b5973b1c483472c /t/merge.t
parent3582f0672895201b3c460c4aeec9fa7b8ee7480f (diff)
parentf77dfa416cafc1a9db80349b31e6cb82e7f0491d (diff)
downloadcolorize-7f313b1c22e61541aea2bcbe666639e05e69c9ad.tar.gz
colorize-7f313b1c22e61541aea2bcbe666639e05e69c9ad.tar.bz2
Merge branch 'merge_part_line'
When cleaning lines with --clean[-all], reduce allocating space for the partial line in memory. Previously, string concatenation may have lead to excessive use of memory.
Diffstat (limited to 't/merge.t')
-rwxr-xr-xt/merge.t153
1 files changed, 153 insertions, 0 deletions
diff --git a/t/merge.t b/t/merge.t
new file mode 100755
index 0000000..b5c6dc7
--- /dev/null
+++ b/t/merge.t
@@ -0,0 +1,153 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use constant true => 1;
+use constant false => 0;
+
+use File::Temp qw(tmpnam);
+use Test::More;
+
+# sequence, buffer sizes
+my @merge_success = (
+ [ "\e[30m", [ 1..4 ] ],
+ [ "\e[31m", [ 1..4 ] ],
+ [ "\e[32m", [ 1..4 ] ],
+ [ "\e[33m", [ 1..4 ] ],
+ [ "\e[34m", [ 1..4 ] ],
+ [ "\e[35m", [ 1..4 ] ],
+ [ "\e[36m", [ 1..4 ] ],
+ [ "\e[37m", [ 1..4 ] ],
+ [ "\e[39m", [ 1..4 ] ],
+ [ "\e[1;30m", [ 1..6 ] ],
+ [ "\e[1;31m", [ 1..6 ] ],
+ [ "\e[1;32m", [ 1..6 ] ],
+ [ "\e[1;33m", [ 1..6 ] ],
+ [ "\e[1;34m", [ 1..6 ] ],
+ [ "\e[1;35m", [ 1..6 ] ],
+ [ "\e[1;36m", [ 1..6 ] ],
+ [ "\e[1;37m", [ 1..6 ] ],
+ [ "\e[1;39m", [ 1..6 ] ],
+ [ "\e[40m", [ 1..4 ] ],
+ [ "\e[41m", [ 1..4 ] ],
+ [ "\e[42m", [ 1..4 ] ],
+ [ "\e[43m", [ 1..4 ] ],
+ [ "\e[44m", [ 1..4 ] ],
+ [ "\e[45m", [ 1..4 ] ],
+ [ "\e[46m", [ 1..4 ] ],
+ [ "\e[47m", [ 1..4 ] ],
+ [ "\e[49m", [ 1..4 ] ],
+ [ "\e[0m", [ 1..3 ] ],
+ [ "\e[m", [ 1..2 ] ],
+ [ "\e[;;m", [ 1..4 ] ],
+ [ "\e[123456m", [ 1 ] ], # tightly coupled to ALLOC_COMPLETE_PART_LINE
+);
+# sequence, buffer size
+my @merge_fail = (
+ [ "\e30m", 1 ], # missing bracket
+ [ "\e[am", 2 ], # not a digit nor ; nor m
+);
+# sequence
+my @buffer = (
+ "\e[30mz",
+ "\e[31mz",
+ "\e[32mz",
+ "\e[33mz",
+ "\e[34mz",
+ "\e[35mz",
+ "\e[36mz",
+ "\e[37mz",
+ "\e[39mz",
+ "\e[1;30mz",
+ "\e[1;31mz",
+ "\e[1;32mz",
+ "\e[1;33mz",
+ "\e[1;34mz",
+ "\e[1;35mz",
+ "\e[1;36mz",
+ "\e[1;37mz",
+ "\e[1;39mz",
+ "\e[40mz",
+ "\e[41mz",
+ "\e[42mz",
+ "\e[43mz",
+ "\e[44mz",
+ "\e[45mz",
+ "\e[46mz",
+ "\e[47mz",
+ "\e[49mz",
+ "\e[0mz",
+ "\e[mz",
+ "\e[;;mz",
+);
+# sequence, buffer size
+my @pushback = (
+ [ "\ezm", 1 ],
+ [ "\e[z", 2 ],
+);
+
+my $tests = 0;
+foreach (@merge_success) {
+ $tests += @{$_->[1]};
+}
+$tests += @merge_fail;
+$tests += @buffer;
+$tests += @pushback;
+
+my $source = 'colorize.c';
+my %programs;
+
+my $compile = sub
+{
+ my ($buf_size) = @_;
+ return true if exists $programs{$buf_size};
+ my $program = tmpnam();
+ return false unless system("gcc -DTEST_MERGE_PART_LINE -DBUF_SIZE=$buf_size -o $program $source") == 0;
+ $programs{$buf_size} = $program;
+ return true; # compiling succeeded
+};
+
+my $test_name = sub
+{
+ my ($sequence, $buf_size) = @_;
+ my $substr = substr($sequence, 0, $buf_size);
+ $substr =~ s/^\e/ESC/;
+ $sequence =~ s/^\e/ESC/;
+ return "$sequence: $substr";
+};
+
+plan tests => $tests;
+
+foreach my $test (@merge_success) {
+ foreach my $buf_size (@{$test->[1]}) {
+ SKIP: {
+ skip 'compiling failed (merge part line)', 1 unless $compile->($buf_size);
+ ok(qx(echo -n "$test->[0]" | $programs{$buf_size} --clean) eq $test->[0], 'merge success: ' . $test_name->($test->[0], $buf_size));
+ }
+ }
+}
+foreach my $test (@merge_fail) {
+ my $buf_size = $test->[1];
+ SKIP: {
+ skip 'compiling failed (merge part line)', 1 unless $compile->($buf_size);
+ ok(qx(echo -n "$test->[0]" | $programs{$buf_size} --clean) eq substr($test->[0], 0, $buf_size), 'merge fail: ' . $test_name->($test->[0], $buf_size));
+ }
+}
+foreach my $test (@buffer) {
+ my $buf_size = length($test) - 1;
+ SKIP: {
+ skip 'compiling failed (merge part line)', 1 unless $compile->($buf_size);
+ ok(qx(echo -n "$test" | $programs{$buf_size} --clean) eq substr($test, 0, $buf_size), 'buffer: ' . $test_name->($test, $buf_size));
+ }
+}
+foreach my $test (@pushback) {
+ my $buf_size = $test->[1];
+ SKIP: {
+ my $program = tmpnam();
+ skip 'compiling failed (merge part line)', 1 unless system("gcc -DBUF_SIZE=$buf_size -o $program $source") == 0;
+ ok(qx(echo -n "$test->[0]" | $program --clean) eq $test->[0], 'pushback: ' . $test_name->($test->[0], $buf_size));
+ unlink $program;
+ }
+}
+
+unlink $programs{$_} foreach keys %programs;