File: test_indexed_gzip.py

package info (click to toggle)
indexed-gzip 1.8.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 572 kB
  • sloc: ansic: 1,916; python: 1,648; makefile: 13; sh: 12
file content (1233 lines) | stat: -rw-r--r-- 36,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
#
# Tests for the indexed_gzip module.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#

from __future__ import print_function

import                    os
import os.path         as op
import itertools       as it
import functools       as ft
import subprocess      as sp
import multiprocessing as mp
import copy            as cp
import                    sys
import                    time
import                    gzip
import                    random
import                    shutil
import                    pickle
import                    hashlib
import                    textwrap
import                    tempfile
import                    contextlib

import numpy as np
from io import BytesIO
import pytest

import indexed_gzip as igzip

from . import gen_test_data
from . import check_data_valid
from . import tempdir
from . import touch
from . import compress


SEEK_SET = os.SEEK_SET
SEEK_CUR = os.SEEK_CUR
SEEK_END = os.SEEK_END


def error_fn(*args, **kwargs):
    raise Exception("Error")


def read_element(gzf, element, seek=True):

    if seek:
        gzf.seek(int(element) * 8)

    bytes = gzf.read(8)
    val   = np.ndarray(1, np.uint64, buffer=bytes)

    return val[0]


def write_text_to_gzip_file(fname, lines):
    with gzip.open(fname, mode='wb') as f:
        for line in lines:
            f.write('{}\n'.format(line).encode())


@pytest.mark.parametrize('drop', [False, True])
def test_open_close(testfile, nelems, seed, drop):

    f = igzip._IndexedGzipFile(filename=testfile, drop_handles=drop)

    assert not f.closed

    element = np.random.randint(0, nelems, 1)[0]
    readval = read_element(f, element)

    assert readval == element

    f.close()

    assert f.closed

    with pytest.raises(IOError):
        f.close()

def test_open_function(testfile, nelems):

    f1 = None
    f2 = None

    try:

        f1 = igzip.IndexedGzipFile(testfile)
        f2 = igzip.open(           testfile)

        element  = np.random.randint(0, nelems, 1)[0]
        readval1 = read_element(f1, element)
        readval2 = read_element(f2, element)

        assert readval1 == element
        assert readval2 == element

    finally:
        if f1 is not None: f1.close()
        if f2 is not None: f2.close()


@pytest.mark.parametrize('drop', [False, True])
def test_open_close_ctxmanager(testfile, nelems, seed, drop):

    with igzip._IndexedGzipFile(filename=testfile, drop_handles=drop) as f:

        element = np.random.randint(0, nelems, 1)[0]
        readval = read_element(f, element)

    assert readval == element
    assert f.closed


def test_open_mode():

    modes = [('r',  True),
             ('rb', True),
             (None, True),
             ('rt', False),
             ('w',  False),
             ('wt', False)]

    # open from file
    with tempdir():
        with gzip.open('f.gz', 'wb') as f:
            f.write(b'12345')
        for mode, expect in modes:
            if expect:
                gzf = igzip.IndexedGzipFile('f.gz', mode=mode)
                assert gzf.read() == b'12345'
            else:
                with pytest.raises(ValueError):
                    igzip.IndexedGzipFile('f.gz', mode=mode)

    # open from fileobj
    class BytesIOWithMode(BytesIO):
        pass

    # accept file-like without mode attribute
    modes.append(('del', True))

    for mode, expect in modes:

        if mode == 'del' and hasattr(BytesIOWithMode, 'mode'):
            delattr(BytesIOWithMode, 'mode')
        else:
            BytesIOWithMode.mode = mode

        fileobj = BytesIOWithMode()
        with gzip.GzipFile(fileobj=fileobj, mode='wb') as f:
            f.write(b'12345')

        print(mode, expect)

        if expect:
            assert igzip.IndexedGzipFile(fileobj=fileobj).read() == b'12345'
        else:
            with pytest.raises(ValueError):
                igzip.IndexedGzipFile(fileobj=fileobj).read()

@pytest.mark.parametrize('drop', [False, True])
def test_atts(testfile, drop):

    modes = [None, 'rb', 'r']

    for m in modes:
        with igzip._IndexedGzipFile(filename=testfile,
                                   mode=m,
                                   drop_handles=drop) as f:
            assert not f.closed
            assert     f.readable()
            assert     f.seekable()
            assert not f.writable()
            assert f.mode     == 'rb'
            assert f.tell()   == 0

            if not drop:
                assert f.fileobj() is not None
                assert f.fileno() == f.fileobj().fileno()
            else:
                with pytest.raises(igzip.NoHandleError):
                    f.fileobj()
                with pytest.raises(igzip.NoHandleError):
                    f.fileno()


@pytest.mark.parametrize('drop', [False, True])
def test_init_failure_cases(concat, drop):

    with tempdir() as td:
        testfile = op.join(td, 'test.gz')
        gen_test_data(testfile, 65536, concat)

        # No writing
        with pytest.raises(ValueError):
            gf = igzip._IndexedGzipFile(filename=testfile,
                                       mode='w',
                                       drop_handles=drop)
        with pytest.raises(ValueError):
            gf = igzip._IndexedGzipFile(filename=testfile,
                                       mode='wb',
                                       drop_handles=drop)

        # No writing
        f  = open(testfile, mode='wb')
        with pytest.raises(ValueError):
            gf = igzip._IndexedGzipFile(fileobj=f, drop_handles=drop)
        f.close()

        # No writing
        f  = open(testfile, mode='w')
        with pytest.raises(ValueError):
            gf = igzip._IndexedGzipFile(fileobj=f, drop_handles=drop)
        f.close()

        # Need a filename or fid
        with pytest.raises(ValueError):
            f = igzip._IndexedGzipFile(drop_handles=drop)

        # Doesn't exist
        with pytest.raises(FileNotFoundError):
            f = igzip._IndexedGzipFile(filename='no_file.gz',
                                       drop_handles=drop)

        # can only specify one of filename/fid
        with pytest.raises(ValueError):
            with open(testfile, mode='rb'):
                f = igzip._IndexedGzipFile(filename=testfile,
                                          fileobj=f,
                                          drop_handles=drop)


@pytest.mark.parametrize('drop', [False, True])
def test_init_success_cases(concat, drop):
    with tempdir() as td:
        testfile = op.join(td, 'test.gz')
        gen_test_data(testfile, 65536, concat)

        gf1 = igzip._IndexedGzipFile(filename=testfile,
                                    drop_handles=drop)
        gf2 = igzip._IndexedGzipFile(filename=testfile,
                                    mode='r',
                                    drop_handles=drop)
        gf3 = igzip._IndexedGzipFile(filename=testfile,
                                    mode='rb',
                                    drop_handles=drop)
        gf1.close()
        gf2.close()
        gf3.close()
        del gf1
        del gf2
        del gf3

@pytest.mark.parametrize('drop,file_like_object', [[False, False],
                                                   [False, True],
                                                   [True,  False],
                                                   [True,  True]])
def test_create_from_open_handle(testfile, nelems, seed, drop, file_like_object):

    f   = open(testfile, 'rb')
    if file_like_object:
        f = BytesIO(f.read())
    gzf = igzip._IndexedGzipFile(fileobj=f, drop_handles=drop)

    assert gzf.fileobj() is f
    assert not gzf.drop_handles

    element = np.random.randint(0, nelems, 1)[0]
    readval = read_element(gzf, element)

    gzf.close()

    try:
        assert readval == element
        assert gzf.closed
        assert not f.closed

    finally:
        f.close()
        del gzf
        del f


def test_accept_filename_or_fileobj(testfile, nelems):

    f    = None
    gzf1 = None
    gzf2 = None
    gzf3 = None

    try:
        f    = open(testfile, 'rb')
        gzf1 = igzip._IndexedGzipFile(testfile)
        gzf2 = igzip._IndexedGzipFile(f)
        gzf3 = igzip._IndexedGzipFile(fileobj=BytesIO(open(testfile, 'rb').read()))

        element  = np.random.randint(0, nelems, 1)[0]
        readval1 = read_element(gzf1, element)
        readval2 = read_element(gzf2, element)
        readval3 = read_element(gzf3, element)

        assert readval1 == element
        assert readval2 == element
        assert readval3 == element

    finally:
        if gzf3 is not None: gzf3.close()
        if gzf2 is not None: gzf2.close()
        if gzf1 is not None: gzf1.close()
        if f    is not None: f   .close()
        del f
        del gzf1
        del gzf2
        del gzf3


def test_prioritize_fd_over_f(testfile, nelems):
    """When a fileobj with an associated fileno is passed to IndexedGzipFile,
    the fileobj's file descriptor (fd) should be utilized by zran.c
    instead of the file-like object specified by fileobj (f).
    """
    if sys.version_info[0] < 3:
        # We can't set the .read attribute in Python 2
        # because it's read-only, so skip it.
        return

    f    = None
    gzf  = None

    try:
        f       = open(testfile, 'rb')
        f.read  = error_fn  # If the file-like object were directly used by zran.c, reading would raise an error.
        gzf     = igzip._IndexedGzipFile(fileobj=f)

        element  = np.random.randint(0, nelems, 1)[0]
        readval  = read_element(gzf, element)

        assert readval == element

    finally:
        if gzf is not None: gzf.close()
        if f   is not None: f  .close()
        del f
        del gzf


def test_handles_not_dropped(testfile, nelems, seed):

    # When drop_handles is False
    with igzip._IndexedGzipFile(filename=testfile, drop_handles=False) as f:
        fid = f.fileobj()

        assert fid is not None

        # Check that the file object
        # doesn't change across reads
        for i in range(5):

            element = np.random.randint(0, nelems, 1)[0]
            readval = read_element(f, element)

            assert readval == element
            assert f.fileobj() is fid

    # Also when given an open stream
    with open(testfile, 'rb') as f:
        with igzip._IndexedGzipFile(fileobj=f) as gzf:

            assert gzf.fileobj() is f

            for i in range(5):

                element = np.random.randint(0, nelems, 1)[0]
                readval = read_element(gzf, element)

                assert readval == element
                assert gzf.fileobj() is f


def test_manual_build():
    with tempdir() as td:
        nelems = 65536
        fname = op.join(td, 'test.gz')

        gen_test_data(fname, nelems, False)

        with igzip._IndexedGzipFile(fname, auto_build=False) as f:

            # Seeking to 0 should work, but
            # anywhere else should fail
            f.seek(0)
            for off in [1, 2, 20, 200]:
                with pytest.raises(igzip.NotCoveredError):
                    f.seek(off)

            # Reading from beginning should work
            readval = read_element(f, 0, seek=False)
            assert readval == 0

            # but subsequent reads should fail
            # (n.b. this might change in the future)
            with pytest.raises(igzip.NotCoveredError):
                readval = read_element(f, 1, seek=False)

            # Seek should still fail even after read
            with pytest.raises(igzip.NotCoveredError):
                f.seek(8)

            # But reading from beginning should still work
            f.seek(0)
            readval = read_element(f, 0, seek=False)
            assert readval == 0

            # But after building the index,
            # seeking and reading should work
            f.build_full_index()

            for i in range(5):
                element = np.random.randint(0, nelems, 1)[0]
                readval = read_element(f, element)
                assert readval == element


@pytest.mark.parametrize('drop', [False, True])
def test_read_all(testfile, nelems, use_mmap, drop):

    if use_mmap:
        pytest.skip('skipping test_read_all test as '
                    'it will require too much memory')

    with igzip._IndexedGzipFile(filename=testfile, drop_handles=drop) as f:
        data = f.read(nelems * 8)

    data = np.ndarray(shape=nelems, dtype=np.uint64, buffer=data)

    # Check that every value is valid
    assert check_data_valid(data, 0)


def test_simple_read_with_null_padding():


    fileobj = BytesIO()

    with gzip.GzipFile(fileobj=fileobj, mode='wb') as f:
        f.write(b"hello world")

    fileobj.write(b"\0" * 100)

    with igzip._IndexedGzipFile(fileobj=fileobj) as f:
        assert f.read() == b"hello world"
        f.seek(3)
        assert f.read() == b"lo world"
        f.seek(20)
        assert f.read() == b""


def test_read_with_null_padding(testfile, nelems, use_mmap):

    if use_mmap:
        pytest.skip('skipping test_read_with_null_padding test '
                    'as it will require too much memory')

    fileobj = BytesIO(open(testfile, "rb").read() + b"\0" * 100)

    with igzip._IndexedGzipFile(fileobj=fileobj) as f:
        data = f.read(nelems * 8)
        # Read a bit further so we reach the zero-padded area.
        # This line should not throw an exception.
        f.read(1)

    data = np.ndarray(shape=nelems, dtype=np.uint64, buffer=data)

    # Check that every value is valid
    assert check_data_valid(data, 0)


@pytest.mark.parametrize('drop', [False, True])
def test_read_beyond_end(concat, drop):
    with tempdir() as tdir:
        nelems   = 65536
        testfile = op.join(tdir, 'test.gz')

        gen_test_data(testfile, nelems, concat)

        with igzip._IndexedGzipFile(filename=testfile,
                                   readall_buf_size=1024,
                                   drop_handles=drop) as f:
            # Try with a specific number of bytes
            data1 = f.read(nelems * 8 + 10)

            # And also with unspecified numbytes
            f.seek(0)
            data2 = f.read()

        data1 = np.ndarray(shape=nelems, dtype=np.uint64, buffer=data1)
        data2 = np.ndarray(shape=nelems, dtype=np.uint64, buffer=data2)
        assert check_data_valid(data1, 0)
        assert check_data_valid(data2, 0)

        del f
        f = None
        os.remove(testfile)


def test_seek(concat):
    with tempdir() as tdir:
        nelems   = 262144 # == 2MB
        testfile = op.join(tdir, 'test.gz')
        gen_test_data(testfile, nelems, concat)

        results = []

        with igzip._IndexedGzipFile(testfile, spacing=131072) as f:

            results.append((f.read(8), 0))

            f.seek(24, SEEK_SET)
            results.append((f.read(8), 3))

            f.seek(-16, SEEK_CUR)
            results.append((f.read(8), 2))

            f.seek(16, SEEK_CUR)
            results.append((f.read(8), 5))

            # SEEK_END only works when index is built
            with pytest.raises(ValueError):
                f.seek(-100, SEEK_END)

            f.build_full_index()

            f.seek(-800, SEEK_END)
            results.append((f.read(8), 262044))

            f.seek(-3200, SEEK_END)
            results.append((f.read(8), 261744))

        for data, expected in results:
            val = np.frombuffer(data, dtype=np.uint64)
            assert val == expected


@pytest.mark.parametrize('drop', [False, True])
def test_seek_and_read(testfile, nelems, niters, seed, drop):

    with igzip._IndexedGzipFile(filename=testfile, drop_handles=drop) as f:

        # Pick some random elements and make
        # sure their values are all right
        seekelems = np.random.randint(0, nelems, niters)

        for i, testval in enumerate(seekelems):

            readval = read_element(f, testval)

            ft = f.tell()

            assert ft      == (testval + 1) * 8
            assert readval == testval


@pytest.mark.parametrize('drop', [False, True])
def test_seek_and_tell(testfile, nelems, niters, seed, drop):

    filesize = nelems * 8

    with igzip._IndexedGzipFile(filename=testfile, drop_handles=drop) as f:

        # Pick some random seek positions
        # and make sure that seek and tell
        # return their location correctly
        seeklocs = np.random.randint(0, filesize, niters)

        for seekloc in seeklocs:

            st = f.seek(seekloc)
            ft = f.tell()

            assert ft == seekloc
            assert st == seekloc

        # Also test that seeking beyond
        # EOF is clamped to EOF
        eofseeks = [filesize,
                    filesize + 1,
                    filesize + 2,
                    filesize + 3,
                    filesize + 4,
                    filesize + 1000,
                    filesize * 1000]

        for es in eofseeks:
            assert f.seek(es) == filesize
            assert f.tell()   == filesize


def test_pread():
    with tempdir() as td:
        nelems = 1024
        testfile = op.join(td, 'test.gz')
        gen_test_data(testfile, nelems, False)

        with igzip.IndexedGzipFile(testfile) as f:
            for i in range(20):
                off  = np.random.randint(0, nelems, 1)[0]
                data = f.pread(8, off * 8)
                val  = np.frombuffer(data, dtype=np.uint64)
                assert val[0] == off


@pytest.mark.parametrize('drop', [False, True])
def test_readinto(drop):
    lines = textwrap.dedent("""
    line 1
    line 2
    this is line 3
    line the fourth
    here is the fifth line
    """).strip().split('\n')


    def line_offset(idx):
        return sum([len(l) for l in lines[:idx]]) + idx


    with tempdir() as td:
        testfile = op.join(td, 'test.gz')
        write_text_to_gzip_file(testfile, lines)
        with igzip._IndexedGzipFile(filename=testfile, drop_handles=drop) as f:

            # read first line into a byte array
            buf = bytearray(len(lines[0]))
            f.seek(0)
            assert f.readinto(buf) == len(lines[0])
            assert buf.decode() == lines[0]

            # read first line into memoryvew
            buf = memoryview(bytearray(len(lines[0])))
            f.seek(0)
            assert f.readinto(buf) == len(lines[0])
            assert buf.tobytes().decode() == lines[0]

            # read an arbitrary line
            offset = line_offset(2)
            buf = bytearray(len(lines[2]))
            f.seek(offset)
            assert f.readinto(buf) == len(lines[2])
            assert buf.decode() == lines[2]

            # read the end line, sans-newline
            offset = line_offset(len(lines) - 1)
            buf = bytearray(len(lines[-1]))
            f.seek(offset)
            assert f.readinto(buf) == len(lines[-1])
            assert buf.decode() == lines[-1]

            # read the end line, with newline
            buf = bytearray(len(lines[-1]) + 1)
            f.seek(offset)
            assert f.readinto(buf) == len(lines[-1]) + 1
            assert buf.decode() == lines[-1] + '\n'

            # read the end line with a bigger buffer
            buf = bytearray(len(lines[-1]) + 10)
            f.seek(offset)
            assert f.readinto(buf) == len(lines[-1]) + 1
            assert buf.decode() == lines[-1] + '\n' + (b'\0' * 9).decode()

            # start at EOF, and try to read something
            filelen = sum([len(l) for l in lines]) + len(lines)
            f.seek(filelen)
            buf = bytearray([99 for i in range(len(buf))])
            assert f.readinto(buf) == 0
            assert all([b == chr(99) for b in buf.decode()])


@pytest.mark.parametrize('drop', [False, True])
def test_readline(drop):
    lines = textwrap.dedent("""
    this is
    some text
    split across
    several lines
    how creative
    """).strip().split('\n')

    with tempdir() as td:
        fname = op.join(td, 'test.gz')
        write_text_to_gzip_file(fname, lines)

        with igzip._IndexedGzipFile(fname, drop_handles=drop) as f:
            seekpos = 0
            for line in lines:

                assert f.readline() == (line + '\n').encode()
                seekpos += len(line) + 1
                assert f.tell() == seekpos

            # Should return empty string after EOF
            assert f.readline() == b''

            f.seek(0)
            assert f.readline(0) == b''


@pytest.mark.parametrize('drop', [False, True])
def test_readline_sizelimit(drop):

    lines = ['line one', 'line two']

    with tempdir() as td:
        fname = op.join(td, 'test.gz')
        write_text_to_gzip_file(fname, lines)

        with igzip._IndexedGzipFile(fname, drop_handles=drop) as f:

            # limit to one character before the end of the first line
            l = f.readline(len(lines[0]) - 1)
            assert l == (lines[0][:-1]).encode()

            # limit to the last character of the first line
            f.seek(0)
            l = f.readline(len(lines[0]) - 1)
            assert l == (lines[0][:-1]).encode()

            # limit to the newline at the end of the first line
            f.seek(0)
            l = f.readline(len(lines[0]) + 1)
            assert l == (lines[0] + '\n').encode()

            # limit to the first character after the first line
            f.seek(0)
            l = f.readline(len(lines[0]) + 2)
            assert l == (lines[0] + '\n').encode()


@pytest.mark.parametrize('drop', [False, True])
def test_readlines(drop):
    lines = textwrap.dedent("""
    this is
    some more text
    split across
    several lines
    super imaginative
    test data
    """).strip().split('\n')

    with tempdir() as td:
        fname = op.join(td, 'test.gz')
        write_text_to_gzip_file(fname, lines)

        with igzip._IndexedGzipFile(fname, drop_handles=drop) as f:

            gotlines = f.readlines()

            assert len(lines) == len(gotlines)

            for expl, gotl in zip(lines, gotlines):
                assert (expl + '\n').encode() == gotl

            assert f.read() == b''


@pytest.mark.parametrize('drop', [False, True])
def test_readlines_sizelimit(drop):

    lines = ['line one', 'line two']
    data  = '\n'.join(lines) + '\n'

    with tempdir() as td:
        fname = op.join(td, 'test.gz')
        write_text_to_gzip_file(fname, lines)

        limits = range(len(data) + 2)

        with igzip._IndexedGzipFile(fname, drop_handles=drop) as f:

            for lim in limits:
                f.seek(0)
                gotlines = f.readlines(lim)

                # Expect the first line
                if lim < len(lines[0]) + 1:
                    assert len(gotlines) == 1
                    assert gotlines[0] == (lines[0]  + '\n').encode()

                # Expect both lines
                else:
                    assert len(gotlines) == 2
                    assert gotlines[0] == (lines[0]  + '\n').encode()
                    assert gotlines[1] == (lines[1]  + '\n').encode()


@pytest.mark.parametrize('drop', [False, True])
def test_iter(drop):

    lines = textwrap.dedent("""
    this is
    even more text
    that is split
    across several lines
    the creativity
    involved in generating
    this test data is
    unparalleled
    """).strip().split('\n')

    with tempdir() as td:
        fname = op.join(td, 'test.gz')
        write_text_to_gzip_file(fname, lines)

        with igzip._IndexedGzipFile(fname, drop_handles=drop) as f:
            for i, gotline in enumerate(f):
                assert (lines[i] + '\n').encode() == gotline

            with pytest.raises(StopIteration):
                 next(f)


@pytest.mark.slow_test
def test_get_index_seek_points():

    with tempdir() as td:
        fname   = op.join(td, 'test.gz')
        spacing = 1048576

        # make a test file
        data = np.arange(spacing, dtype=np.uint64)
        with gzip.open(fname, 'wb') as f:
            f.write(data.tobytes())

        # check points before and after index creation
        with igzip._IndexedGzipFile(fname, spacing=spacing) as f:
            assert not list(f.seek_points())
            f.build_full_index()

            expected_number_of_seek_points = 1 + int(data.nbytes / spacing)
            seek_points = list(f.seek_points())

            assert len(seek_points) == expected_number_of_seek_points

            # check monotonic growth
            uncmp_offsets = [point[0] for point in seek_points]
            assert sorted(uncmp_offsets) == uncmp_offsets


def test_import_export_index():

    with tempdir() as td:
        fname    = op.join(td, 'test.gz')
        idxfname = op.join(td, 'test.gzidx')

        # make a test file
        data = np.arange(524288, dtype=np.uint64)
        with gzip.open(fname, 'wb') as f:
            f.write(data.tobytes())

        # generate an index file
        with igzip._IndexedGzipFile(fname, spacing=131072) as f:
            f.build_full_index()
            points = list(f.seek_points())
            f.export_index(idxfname)

        # Check that index file works via __init__
        with igzip._IndexedGzipFile(fname, index_file=idxfname) as f:
            f.seek(65535 * 8)
            val = np.frombuffer(f.read(8), dtype=np.uint64)
            assert val[0] == 65535
            assert points == list(f.seek_points())

        # Check that index file works via import_index
        with igzip._IndexedGzipFile(fname) as f:
            f.import_index(idxfname)
            f.seek(65535 * 8)
            val = np.frombuffer(f.read(8), dtype=np.uint64)
            assert val[0] == 65535
            assert points == list(f.seek_points())


def test_import_export_index_open_file():

    with tempdir() as td:
        fname    = op.join(td, 'test.gz')
        idxfname = op.join(td, 'test.gzidx')

        # make a test file
        data = np.arange(524288, dtype=np.uint64)
        with gzip.open(fname, 'wb') as f:
            f.write(data.tobytes())

        # generate an index file from open file handle
        with igzip._IndexedGzipFile(fname, spacing=131072) as f:
            f.build_full_index()
            points = list(f.seek_points())

            # Should raise if wrong permissions
            with pytest.raises(ValueError):
                touch(idxfname)
                with open(idxfname, 'rb') as idxf:
                    f.export_index(fileobj=idxf)

            with open(idxfname, 'wb') as idxf:
                f.export_index(fileobj=idxf)

        # Check that we can read it back
        with igzip._IndexedGzipFile(fname) as f:

            # Should raise if wrong permissions
            # (append, so existing contents are
            # not overwritten)
            with pytest.raises(ValueError):
                with open(idxfname, 'ab') as idxf:
                    f.import_index(fileobj=idxf)

            with open(idxfname, 'rb') as idxf:
                f.import_index(fileobj=idxf)
            f.seek(65535 * 8)
            val = np.frombuffer(f.read(8), dtype=np.uint64)
            assert val[0] == 65535
            assert points == list(f.seek_points())

        # Test exporting to / importing from a file-like object
        idxf = BytesIO()
        with igzip._IndexedGzipFile(fname, spacing=131072) as f:
            f.build_full_index()
            f.export_index(fileobj=idxf)
            points = list(f.seek_points())
        idxf.seek(0)
        with igzip._IndexedGzipFile(fname) as f:
            f.import_index(fileobj=idxf)
            f.seek(65535 * 8)
            val = np.frombuffer(f.read(8), dtype=np.uint64)
            assert val[0] == 65535
            assert points == list(f.seek_points())


def test_build_index_from_unseekable():
    with tempdir() as td:
        fname    = op.join(td, 'test.gz')
        idxfname = op.join(td, 'test.gzidx')

        # make a test file
        data = np.arange(524288, dtype=np.uint64)
        with gzip.open(fname, 'wb') as f:
            f.write(data.tobytes())

        # Test creating the index when file is unseekable,
        # then using the index when file is seekable.
        with open(fname, 'rb') as f:
            b = f.read()
            fileobj = BytesIO(b)

        def new_seek(*args, **kwargs):
            raise OSError()
        def new_tell(*args, **kwargs):
            raise OSError()
        old_seek = fileobj.seek
        old_tell = fileobj.tell
        fileobj.seekable = lambda: False
        fileobj.seek = new_seek
        fileobj.tell = new_tell
        # generate an index file
        with igzip._IndexedGzipFile(fileobj, spacing=131072) as f:
            f.build_full_index()
            f.export_index(idxfname)
            points = list(f.seek_points())
        fileobj.seek = old_seek
        fileobj.tell = old_tell
        fileobj.seekable = lambda: True
        fileobj.seek(0)
        # Check that index file works via __init__
        with igzip._IndexedGzipFile(fileobj, index_file=idxfname) as f:
            f.seek(65535 * 8)
            val = np.frombuffer(f.read(8), dtype=np.uint64)
            assert val[0] == 65535
            assert points == list(f.seek_points())


def test_wrapper_class():

    with tempdir() as td:
        fname    = op.join(td, 'test.gz')
        idxfname = op.join(td, 'test.gzidx')

        data = np.arange(65536, dtype=np.uint64)
        with gzip.open(fname, 'wb') as f:
            f.write(data.tobytes())

        with igzip.IndexedGzipFile(fname, drop_handles=False) as f:

            assert f.fileno() == f.fileobj().fileno()
            assert not f.drop_handles

            f.build_full_index()
            f.export_index(idxfname)

            f.import_index(idxfname)


def gcd(num):
    if num <= 3:
        return 1
    candidates = list(range(int(np.ceil(np.sqrt(num))), 2, -2))
    candidates.extend((2, 1))
    for divisor in candidates:
        if num % divisor == 0:
            return divisor
    return 1


def test_size_multiple_of_readbuf():

    fname = 'test.gz'

    with tempdir():

        while True:

            data = np.random.randint(1, 1000, 100000, dtype=np.uint32)
            with gzip.open(fname, 'wb') as f:
                f.write(data.tobytes())
            del f
            f = None

            # we need a file size that is divisible
            # by the minimum readbuf size
            fsize = op.getsize(fname)
            if gcd(fsize) >= 128:
                break

        # readbuf size == file size
        bufsz = fsize

        with igzip.IndexedGzipFile(fname, readbuf_size=bufsz) as f:
            assert f.seek(fsize) == fsize
        del f
        f = None

        with igzip.IndexedGzipFile(fname, readbuf_size=bufsz) as f:
            read = np.ndarray(shape=100000, dtype=np.uint32, buffer=f.read())
            assert np.all(read == data)
        del f
        f = None

        # Use a buf size that is a divisor of the file size
        bufsz = gcd(fsize)

        with igzip.IndexedGzipFile(fname, readbuf_size=bufsz) as f:
            assert f.seek(fsize) == fsize
        del f
        f = None

        with igzip.IndexedGzipFile(fname, readbuf_size=bufsz) as f:
            read = np.ndarray(shape=100000, dtype=np.uint32, buffer=f.read())
            assert np.all(read == data)
        del f
        f = None


@pytest.mark.slow_test
def test_picklable():

    # default behaviour is for drop_handles=True,
    # which means that an IndexedGzipFile object
    # should be picklable/serialisable
    fname = 'test.gz'

    with tempdir():
        data = np.random.randint(1, 1000, (10000, 10000), dtype=np.uint32)
        with open(fname+'.bin', 'wb') as f:
            f.write(data.tobytes())
        compress(fname+'.bin', fname)
        del f

        gzf        = igzip.IndexedGzipFile(fname)
        first50MB  = gzf.read(1048576 * 50)
        gzf.seek(gzf.tell())
        pickled    = pickle.dumps(gzf)
        second50MB = gzf.read(1048576 * 50)
        gzf.seek(gzf.tell())

        gzf.close()
        del gzf

        gzf = pickle.loads(pickled)
        assert gzf.tell() == 1048576 * 50
        assert gzf.read(1048576 * 50) == second50MB
        gzf.seek(0)
        assert gzf.read(1048576 * 50) == first50MB
        gzf.close()
        del gzf

    # if drop_handles=False, no pickle
    with tempdir():
        data = np.random.randint(1, 1000, 50000, dtype=np.uint32)
        with gzip.open(fname, 'wb') as f:
            f.write(data.tobytes())
        del f

        gzf = igzip.IndexedGzipFile(fname, drop_handles=False)

        with pytest.raises(pickle.PicklingError):
            pickled = pickle.dumps(gzf)
        gzf.close()
        del gzf


def test_copyable():
    fname = 'test.gz'

    with tempdir():
        data = np.random.randint(1, 1000, (10000, 10000), dtype=np.uint32)
        with open(fname+'.bin', 'wb') as f:
            f.write(data.tobytes())
        compress(fname+'.bin', fname)
        del f

        gzf        = igzip.IndexedGzipFile(fname)
        gzf_copy   = cp.deepcopy(gzf)
        first50MB  = gzf.read(1048576 * 50)
        gzf.seek(gzf.tell())
        gzf_copy2  = cp.deepcopy(gzf)
        second50MB = gzf.read(1048576 * 50)
        gzf.seek(gzf.tell())

        gzf.close()
        del gzf

        assert gzf_copy.tell() == 0
        assert gzf_copy2.tell() == 1048576 * 50
        assert gzf_copy.read(1048576 * 50) == first50MB
        assert gzf_copy2.read(1048576 * 50) == second50MB
        gzf_copy2.seek(0)
        assert gzf_copy2.read(1048576 * 50) == first50MB
        gzf_copy.close()
        gzf_copy2.close()
        del gzf_copy
        del gzf_copy2

    with tempdir():
        data = np.random.randint(1, 1000, 50000, dtype=np.uint32)
        with gzip.open(fname, 'wb') as f:
            f.write(data.tobytes())
        del f

        # if drop_handles=False, no copy
        gzf = igzip.IndexedGzipFile(fname, drop_handles=False)

        with pytest.raises(pickle.PicklingError):
            gzf_copy = cp.deepcopy(gzf)
        gzf.close()
        del gzf

        # If passed an open filehandle, no copy
        with open(fname, 'rb') as fobj:
            gzf = igzip.IndexedGzipFile(fileobj=fobj)
            with pytest.raises(pickle.PicklingError):
                gzf_copy = cp.deepcopy(gzf)
            gzf.close()
            del gzf
        del fobj


def _mpfunc(gzf, size, offset):
    gzf.seek(offset)
    bytes = gzf.read(size)
    val = np.ndarray(int(size / 4), np.uint32, buffer=bytes)
    gzf.close()
    del gzf
    return val.sum()


@pytest.mark.slowtest
def test_multiproc_serialise():
    fname = 'test.gz'
    with tempdir():

        data = np.arange(10000000, dtype=np.uint32)
        with gzip.open(fname, 'wb') as f:
            f.write(data.tobytes())
        del f

        gzf = igzip.IndexedGzipFile(fname)

        size    = int(len(data) / 16)
        offsets = np.arange(0, len(data), size)
        func    = ft.partial(_mpfunc, gzf, size * 4)

        pool = mp.Pool(8)
        results = pool.map(func, offsets * 4)
        pool.close()
        pool.join()
        gzf.close()
        del gzf
        del pool

        expected = [data[off:off+size].sum() for off in offsets]

        assert results == expected


@pytest.mark.slow_test
def test_32bit_overflow(niters, seed):
    with tempdir():

        block  = 2 ** 24     # 128MB
        nelems = block * 48  # 6GB

        data = np.ones(block, dtype=np.uint64).tobytes()

        with gzip.open('test.gz', 'wb') as f:
            for i in range(48):
                print('Generated to {}...'.format(block * i))
                f.write(data)

        with igzip._IndexedGzipFile(filename='test.gz') as f:

            seekelems = np.random.randint(0, nelems, niters)

            for i, testval in enumerate(seekelems):

                readval = read_element(f, testval)

                ft = f.tell()

                assert ft      == int(testval + 1) * 8
                assert readval == 1