Skip to content

Commit

Permalink
fix archive dep
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Dec 9, 2024
1 parent d46b710 commit e7dc9e2
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 73 deletions.
4 changes: 2 additions & 2 deletions app_archive/lib/src/gzip_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:tekartik_common_utils/byte_utils.dart';
/// GZip some text
Uint8List gzipText(String text, {bool? noDate}) {
noDate ??= false;
var data = const GZipEncoder().encode(utf8.encode(text));
var data = const GZipEncoder().encodeBytes(utf8.encode(text));
if (noDate) {
data[4] = 0;
data[5] = 0;
Expand All @@ -25,7 +25,7 @@ String ungzipText(Uint8List data) {
/// GZip some bytes
Uint8List gzipBytes(Uint8List bytes, {bool? noDate}) {
noDate ??= false;
var data = const GZipEncoder().encode(bytes);
var data = const GZipEncoder().encodeBytes(bytes);
if (noDate) {
data[4] = 0;
data[5] = 0;
Expand Down
2 changes: 1 addition & 1 deletion app_archive/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ dev_dependencies:
build_runner: ">=2.4.13"
build_test:
dependency_overrides:
archive: ">=4.0.0"
archive: ">=4.0.1"
190 changes: 120 additions & 70 deletions app_archive/test/gzip_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import 'package:tekartik_app_archive/gzip.dart';
import 'package:tekartik_common_utils/byte_utils.dart';
import 'package:tekartik_common_utils/env_utils.dart';
import 'package:tekartik_common_utils/hex_utils.dart';
import 'package:tekartik_common_utils/list_utils.dart';
import 'package:tekartik_common_utils/log_format.dart';
import 'package:tekartik_common_utils/string_utils.dart';
import 'package:test/test.dart';

Expand All @@ -15,7 +17,7 @@ extension Shuffle on String {

void main() {
group('gzip', () {
test('compress bigbytes', () {
test('no date compress bigbytes', () {
var allBytes = List.generate(256, (index) => index);

var bigBytes = asUint8List(listFlatten(
Expand All @@ -27,9 +29,28 @@ void main() {
var compressed = gzipBytes(bigBytes, noDate: true);
sw.stop();
print('compressed ${compressed.length} ${sw.elapsed}');
expect(gzipBytes(bigBytes, noDate: true), compressed);
//print('compressed ${compressed.length}');
expect(ungzipBytes(compressed), bigBytes);

expect(gzipBytes(bigBytes, noDate: true), compressed);
var result = ungzipBytes(compressed);
expect(result, bigBytes,
reason: '${logFormat(result)} != ${logFormat(bigBytes)}');
});
test('compress bigbytes', () {
var allBytes = List.generate(256, (index) => index);

var bigBytes = asUint8List(listFlatten(
List.generate(50000, (i) => allBytes..shuffle()).toList()));
print(
'bigUint8List ${bigBytes.length} ${toHexString(bigBytes.sublist(0, 128))}');
var sw = Stopwatch()..start();

var compressed = gzipBytes(bigBytes);
sw.stop();
print('compressed ${compressed.length} ${sw.elapsed}');
var result = ungzipBytes(compressed);
expect(result, bigBytes,
reason: '${logFormat(result)} != ${logFormat(bigBytes)}');
});
test('compress bigtext', () {
var text = 'abcdedfghijklmnopqrstuvwxyz';
Expand All @@ -40,83 +61,112 @@ void main() {
sw.stop();
print('compressed ${compressed.length} ${sw.elapsed}');
expect(gzipText(bigText, noDate: true), compressed);
//print('compressed ${compressed.length}');
expect(ungzipText(compressed), bigText);
var result = ungzipText(compressed);
expect(result, bigText,
reason: '${logFormat(result)} != ${logFormat(bigText)}');
});
String generateBigText(int alphabetCount) {
var text = 'abcdedfghijklmnopqrstuvwxyz';
return List.generate(alphabetCount, (i) => text.shuffled).join();
}

test('no date compress bigtext', () {
var text = 'abcdedfghijklmnopqrstuvwxyz';
var bigText = List.generate(50000, (i) => text.shuffled).join();
print('bigText ${bigText.length} ${bigText.truncate(128)}');
var sw = Stopwatch()..start();
var compressed = gzipText(bigText);
sw.stop();
print('compressed ${compressed.length} ${sw.elapsed}');
var result = ungzipText(compressed);
expect(result, bigText,
reason: '${logFormat(result)} != ${logFormat(bigText)}');
});
test('compress', () {
expect(gzipText('étoile', noDate: true), [
31,
139,
8,
0,
0,
0,
0,
0,
0,
255,
59,
188,
178,
36,
63,
51,
39,
21,
0,
199,
250,
11,
130,
7,
0,
0,
0,
]);

test('compress latest', () {
if (!kDartIsWeb) {
expect(gzipText('étoile', noDate: true), gzipBytesV2);
}

void roundTrip(String text) {
expect(ungzipText(gzipText(text)), text);
var result = ungzipText(gzipText(text));
expect(result, text,
reason: '${logFormat(result)} != ${logFormat(text)}');
}

roundTrip('étoile');
var bigText =
String.fromCharCodes(List.generate(5000000, (index) => index % 255));
var bigText = generateBigText(500000);
/*String.fromCharCodes(List.generate(
// 5000000 failing on chrome
50,
(index) => index % 255));*/
roundTrip(bigText);
expect(gzipText(bigText).length, 33103);
//expect(gzipText(bigText).length, 3689); // 33103);
});

test('decompress', () {
expect(
ungzipText(asUint8List([
31,
139,
8,
0,
0,
0,
0,
0,
0,
255,
59,
188,
178,
36,
63,
51,
39,
21,
0,
199,
250,
11,
130,
7,
0,
0,
0
])),
'étoile');
expect(gzipBytesV2, isNot(gzipBytesV1));
expect(ungzipText(gzipBytesV1), 'étoile');
expect(ungzipText(gzipBytesV2), 'étoile');
});
});
}

var gzipBytesV1 = asUint8List([
31,
139,
8,
0,
0,
0,
0,
0,
0,
255,
59,
188,
178,
36,
63,
51,
39,
21,
0,
199,
250,
11,
130,
7,
0,
0,
0,
]);
var gzipBytesV2 = asUint8List([
31,
139,
8,
0,
0,
0,
0,
0,
0,
3, // ? WAS 255,
59,
188,
178,
36,
63,
51,
39,
21,
0,
199,
250,
11,
130,
7,
0,
0,
0,
]);

0 comments on commit e7dc9e2

Please sign in to comment.