From e7dc9e2fdc6b3b66ba28de3ce39ea8b1ad347fcc Mon Sep 17 00:00:00 2001 From: Alexandre Roux Date: Mon, 9 Dec 2024 09:53:21 +0100 Subject: [PATCH] fix archive dep --- app_archive/lib/src/gzip_impl.dart | 4 +- app_archive/pubspec.yaml | 2 +- app_archive/test/gzip_test.dart | 190 ++++++++++++++++++----------- 3 files changed, 123 insertions(+), 73 deletions(-) diff --git a/app_archive/lib/src/gzip_impl.dart b/app_archive/lib/src/gzip_impl.dart index 0c2c861..791b413 100644 --- a/app_archive/lib/src/gzip_impl.dart +++ b/app_archive/lib/src/gzip_impl.dart @@ -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; @@ -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; diff --git a/app_archive/pubspec.yaml b/app_archive/pubspec.yaml index e3806e7..6c472f2 100644 --- a/app_archive/pubspec.yaml +++ b/app_archive/pubspec.yaml @@ -22,4 +22,4 @@ dev_dependencies: build_runner: ">=2.4.13" build_test: dependency_overrides: - archive: ">=4.0.0" \ No newline at end of file + archive: ">=4.0.1" \ No newline at end of file diff --git a/app_archive/test/gzip_test.dart b/app_archive/test/gzip_test.dart index d815d3a..dd63f23 100644 --- a/app_archive/test/gzip_test.dart +++ b/app_archive/test/gzip_test.dart @@ -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'; @@ -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( @@ -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'; @@ -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, +]);