Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F103672955
LinesOfALargeFileTestCase.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Mar 3, 22:21
Size
2 KB
Mime Type
text/x-php
Expires
Wed, Mar 5, 22:21 (2 d)
Engine
blob
Format
Raw Data
Handle
24635639
Attached To
rPHU libphutil
LinesOfALargeFileTestCase.php
View Options
<?php
/**
* @group testcase
*/
final
class
LinesOfALargeFileTestCase
extends
PhutilTestCase
{
public
function
testBasics
()
{
$this
->
writeAndRead
(
'abcd'
,
array
(
'abcd'
,
));
}
public
function
testTerminalDelimiterPresent
()
{
$this
->
writeAndRead
(
"bat
\n
cat
\n
dog
\n
"
,
array
(
'bat'
,
'cat'
,
'dog'
,
));
}
public
function
testTerminalDelimiterAbsent
()
{
$this
->
writeAndRead
(
"bat
\n
cat
\n
dog"
,
array
(
'bat'
,
'cat'
,
'dog'
,
));
}
public
function
testChangeDelimiter
()
{
$this
->
writeAndRead
(
"bat
\1
cat
\1
dog
\1
"
,
array
(
'bat'
,
'cat'
,
'dog'
,
),
"
\1
"
);
}
public
function
testEmptyLines
()
{
$this
->
writeAndRead
(
"
\n\n
bat
\n
"
,
array
(
''
,
''
,
'bat'
,
));
}
public
function
testLargeFile
()
{
$line
=
'The quick brown fox jumps over the lazy dog.'
;
$n
=
100
;
$this
->
writeAndRead
(
str_repeat
(
$line
.
"
\n
"
,
$n
),
array_fill
(
0
,
$n
,
$line
));
}
public
function
testLongLine
()
{
$line
=
str_repeat
(
'x'
,
64
*
1024
);
$this
->
writeAndRead
(
$line
,
array
(
$line
));
}
public
function
testReadFailure
()
{
$caught
=
null
;
try
{
$f
=
new
LinesOfALargeFile
(
'/does/not/exist.void'
);
$f
->
rewind
();
}
catch
(
FilesystemException
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertTrue
(
$caught
instanceof
$ex
);
}
public
function
testLineFilter
()
{
$write
=
"bat
\n
cat
\n
dog
\n
Bat
\n
Cat
\n
Dog
\n
"
;
$read
=
array
(
1
=>
'cat'
,
4
=>
'Cat'
,
);
$tmp
=
new
TempFile
();
Filesystem
::
writeFile
(
$tmp
,
$write
);
$lines
=
array
();
$iterator
=
new
PhutilCallbackFilterIterator
(
new
LinesOfALargeFile
(
$tmp
),
array
(
$this
,
'allowCatsOnly'
));
foreach
(
$iterator
as
$n
=>
$line
)
{
$lines
[
$n
-
1
]
=
$line
;
}
$this
->
assertEqual
(
$read
,
$lines
,
'Write: '
.
phutil_utf8_shorten
(
$write
,
32
));
}
public
function
allowCatsOnly
(
$line
)
{
$line
=
strtoupper
(
$line
);
if
(
$line
!=
'CAT'
)
{
return
null
;
}
return
$line
;
}
private
function
writeAndRead
(
$write
,
$read
,
$delimiter
=
"
\n
"
)
{
$tmp
=
new
TempFile
();
Filesystem
::
writeFile
(
$tmp
,
$write
);
$lines
=
array
();
$iterator
=
id
(
new
LinesOfALargeFile
(
$tmp
))->
setDelimiter
(
$delimiter
);
foreach
(
$iterator
as
$n
=>
$line
)
{
$lines
[
$n
-
1
]
=
$line
;
}
$this
->
assertEqual
(
$read
,
$lines
,
'Write: '
.
phutil_utf8_shorten
(
$write
,
32
));
}
}
Event Timeline
Log In to Comment