# Path: pyfive/btree.py
# def _verify_fletcher32(chunk_buffer):
#     """ Verify a chunk with a fletcher32 checksum. """
#     # calculate checksums
#     if len(chunk_buffer) % 2:
#         arr = np.frombuffer(chunk_buffer[:-4]+b'\x00', '<u2')
#     else:
#         arr = np.frombuffer(chunk_buffer[:-4], '<u2')
#     sum1 = sum2 = 0
#     for i in arr:
#         sum1 = (sum1 + i) % 65535
#         sum2 = (sum2 + sum1) % 65535
# 
#     # extract stored checksums
#     ref_sum1, ref_sum2 = np.frombuffer(chunk_buffer[-4:], '>u2')
#     ref_sum1 = ref_sum1 % 65535
#     ref_sum2 = ref_sum2 % 65535
# 
#     # compare
#     if sum1 != ref_sum1 or sum2 != ref_sum2:
#         raise ValueError("fletcher32 checksum invalid")
#     return True


import os
import unittest
import numpy as np
import pyfive
from numpy.testing import assert_array_equal
from pyfive.btree import _verify_fletcher32



DIRNAME = os.path.dirname(__file__)
DATASET_FLETCHER_HDF5_FILE = os.path.join(DIRNAME, 'fletcher32.hdf5')


def test_fletcher32_datasets():

    with pyfive.File(DATASET_FLETCHER_HDF5_FILE) as hfile:

        # check data
        dset1 = hfile['dataset1']
        assert_array_equal(dset1[:], np.arange(4*4).reshape((4, 4)))
        assert dset1.chunks == (2, 2)

        # check data
        dset2 = hfile['dataset2']
        assert_array_equal(dset2[:], np.arange(3))
        assert dset2.chunks == (3, )

        # check attribute
        assert dset1.fletcher32


class TestChunkFletcher32(unittest.TestCase):

    def test_fletcher32_invalid(self):
        bad_chunk = b'\x00\x00\x00\x01'
        with self.assertRaises(ValueError) as context:

# Path: pyfive/btree.py
# def _verify_fletcher32(chunk_buffer):
#     """ Verify a chunk with a fletcher32 checksum. """
#     # calculate checksums
#     if len(chunk_buffer) % 2:
#         arr = np.frombuffer(chunk_buffer[:-4]+b'\x00', '<u2')
#     else:
#         arr = np.frombuffer(chunk_buffer[:-4], '<u2')
#     sum1 = sum2 = 0
#     for i in arr:
#         sum1 = (sum1 + i) % 65535
#         sum2 = (sum2 + sum1) % 65535
# 
#     # extract stored checksums
#     ref_sum1, ref_sum2 = np.frombuffer(chunk_buffer[-4:], '>u2')
#     ref_sum1 = ref_sum1 % 65535
#     ref_sum2 = ref_sum2 % 65535
# 
#     # compare
#     if sum1 != ref_sum1 or sum2 != ref_sum2:
#         raise ValueError("fletcher32 checksum invalid")
#     return True


# Path: tests/test_fletcher32.py
import os
import unittest
import numpy as np
import pyfive
from numpy.testing import assert_array_equal
from pyfive.btree import _verify_fletcher32




DIRNAME = os.path.dirname(__file__)
DATASET_FLETCHER_HDF5_FILE = os.path.join(DIRNAME, 'fletcher32.hdf5')


def test_fletcher32_datasets():

    with pyfive.File(DATASET_FLETCHER_HDF5_FILE) as hfile:

        # check data
        dset1 = hfile['dataset1']
        assert_array_equal(dset1[:], np.arange(4*4).reshape((4, 4)))
        assert dset1.chunks == (2, 2)

        # check data
        dset2 = hfile['dataset2']
        assert_array_equal(dset2[:], np.arange(3))
        assert dset2.chunks == (3, )

        # check attribute
        assert dset1.fletcher32


class TestChunkFletcher32(unittest.TestCase):

    def test_fletcher32_invalid(self):
        bad_chunk = b'\x00\x00\x00\x01'
        with self.assertRaises(ValueError) as context:

            _verify_fletcher32(bad_chunk)