function test_incomplete_final_chunk() { $input = range(0, 9); $chunker = chunk(3); $expect = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]; $actual = iterator_to_array($chunker($input)); $this->assertEquals($expect, $actual); }
function log_list() { pagetop(gTxt('visitor_logs')); extract(get_prefs()); safe_delete("txp_log", "`time` < date_sub(now(),interval " . $expire_logs_after . " day)"); safe_optimize("txp_log"); safe_repair("txp_log"); $page = gps('page'); $total = getCount('txp_log', "1"); $limit = 50; $numPages = ceil($total / $limit); $page = !$page ? 1 : $page; $offset = ($page - 1) * $limit; $nav[] = $page > 1 ? PrevNextLink("log", $page - 1, gTxt('prev'), 'prev') : ''; $nav[] = sp . small($page . '/' . $numPages) . sp; $nav[] = $page != $numPages ? PrevNextLink("log", $page + 1, gTxt('next'), 'next') : ''; $rs = safe_rows_start("*, unix_timestamp(time) as stamp", "txp_log", "1 order by time desc limit {$offset},{$limit}"); if ($rs) { echo startTable('list'), assHead('time', 'host', 'page', 'referrer'); $stamp = ''; while ($a = nextRow($rs)) { extract($a); if ($refer) { $referprint = preg_replace("/^www\\./", "", chunk(htmlspecialchars($refer), 50)); $referprint = '<a href="http://' . htmlspecialchars($refer) . '">' . $referprint . '</a>'; } else { $referprint = ' '; } $pageprint = preg_replace('/\\/$/', '', htmlspecialchars(substr($page, 1))); $pageprint = $pageprint == '' ? '' : '<a href="' . htmlspecialchars($page) . '" target="_blank">' . chunk($pageprint, 50) . '</a>'; if ($method == 'POST') { $pageprint = '<b>' . $pageprint . '</b>'; } $fstamp = date("n/j g:i a", $stamp + tz_offset()); $hostprint = chunk($host, 40); echo tr(td($fstamp) . td($hostprint) . td($pageprint) . td($referprint)); unset($refer, $referprint, $page, $pageprint); } echo '<tr><td colspan="4" align="right" style="padding:10px">', join('', $nav), "</td></tr>", endTable(); } else { echo graf(gTxt('no_refers_recorded'), ' align="center"'); } }
if (!is_dir($path)) { echo "[{$path}] already exists, but not dir\n"; exit - 1; } } $file = "{$path}/{$argv[1]}.{$argv[2]}"; if (file_exists($file)) { echo "[{$file}] already exists\n"; exit - 1; } $fout = fopen($file, 'w'); $to_file = function ($line) { global $fout; fwrite($fout, "{$line}"); }; chunk($argv[0], $argv[1], $argv[2], $to_file); fclose($fout); echo json_encode($argv) . " done."; function chunk($filename, $chunks, $chunk, $doadeed) { $size = filesize($filename); $chunk_size = ceil($size / $chunks); $buff_size = 1024; $start = $chunk * $chunk_size; $end = $start + $chunk_size; if (DEBUG) { echo "total size: {$size}, chunk size: {$chunk_size}, from: {$start}, to: {$end}\n"; } $fin = fopen($filename, 'r'); fseek($fin, $start); $head = "{$chunk}" == "0" ? false : true;
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Chunk size must be positive */ public function testNegativeChunkSizeError() { toArray(chunk([1, 2, 3], -1)); }
public function testChunk() { $iterable = new \ArrayIterator(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]); $this->assertSame([['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4], ['e' => 5]], toArray(chunk($iterable, 2))); $this->assertSame([[0 => 0, 1 => 1], [2 => 2, 3 => 3]], toArray(chunk([0, 1, 2, 3], 2))); $this->assertSame([[0, 1, 2]], toArray(chunk([0, 1, 2], 100000))); $this->assertSame([], toArray(chunk([], 100000))); }