<commit_before>import os

import numpy as np
import pytest

from atrcopy import DefaultSegment, SegmentData, get_xex, interleave_segments, user_bit_mask, diff_bit_mask


def get_indexed(segment, num, scale):
    indexes = np.arange(num) * scale
    raw = segment.rawdata.get_indexed(indexes)
    s = DefaultSegment(raw, segment.start_addr + indexes[0])
    return s, indexes

class TestSegment1(object):
    def setup(self):
        self.segments = []
        for i in range(8):
            data = np.arange(1024, dtype=np.uint8) * i
            r = SegmentData(data)
            self.segments.append(DefaultSegment(r, i * 1024))

    def test_xex(self):
        items = [
            [(0, 1, 2), 0],
            ]
        
        for indexes, stuff in items:
            s = [self.segments[i] for i in indexes]
            s[1].style[0:500] = diff_bit_mask
            s[1].set_comment_at(0, "comment 0")
            s[1].set_comment_at(10, "comment 10")
            s[1].set_comment_at(100, "comment 100")
            print list(s[1].iter_comments_in_segment())
            seg, subseg = get_xex(s, 0xbeef)
            assert tuple(seg.data[0:2]) == (0xff, 0xff)
            # 2 bytes for the ffff
            # 4 bytes per segment for start, end address
            # An extra segment has been inserted for the run address!
            size = reduce(lambda a, b:a + len(b), subseg, 0)
            assert len(seg) == 2 + size
            print id(s[1]), list(s[1].iter_comments_in_segment())
            print id(subseg[2]), list(subseg[2].iter_comments_in_segment())
            for i, c in s[1].iter_comments_in_segment():
                assert c == subseg[2].get_comment(i + 4)
            assert np.all(s[1].style[:] == subseg[2].style[4:])

    def test_copy(self):
        for s in self.segments:
            d = s.rawdata
            print "orig:", d.data.shape, d.is_indexed, d.data, id(d.data)
            c = d.copy()
            print "copy", c.data.shape, c.is_indexed, c.data, id(c.data)
            assert c.data.shape == s.data.shape
            assert id(c) != id(s)
            assert np.all((c.data[:] - s.data[:]) == 0)
            c.data[0:100] = 1
            print d.data
            print c.data
            assert not np.all((c.data[:] - s.data[:]) == 0)


class TestIndexed(object):
    def setup(self):
        data = np.arange(4096, dtype=np.uint8)
        data[1::2] = np.repeat(np.arange(16, dtype=np.uint8), 128)<commit_msg>Fixed test for segment start address<commit_after>import os

import numpy as np
import pytest

from atrcopy import DefaultSegment, SegmentData, get_xex, interleave_segments, user_bit_mask, diff_bit_mask
from atrcopy.errors import *


def get_indexed(segment, num, scale):
    indexes = np.arange(num) * scale
    raw = segment.rawdata.get_indexed(indexes)
    s = DefaultSegment(raw, segment.start_addr + indexes[0])
    return s, indexes

class TestSegment1(object):
    def setup(self):
        self.segments = []
        for i in range(8):
            data = np.arange(1024, dtype=np.uint8) * i
            r = SegmentData(data)
            self.segments.append(DefaultSegment(r, i * 1024))

    def test_xex(self):
        items = [
            [(0, 1, 2), 0],
            ]
        
        for indexes, stuff in items:
            s = [self.segments[i] for i in indexes]
            s[1].style[0:500] = diff_bit_mask
            s[1].set_comment_at(0, "comment 0")
            s[1].set_comment_at(10, "comment 10")
            s[1].set_comment_at(100, "comment 100")
            print list(s[1].iter_comments_in_segment())
            with pytest.raises(InvalidBinaryFile):
                seg, subseg = get_xex(s, 0xbeef)
            seg, subseg = get_xex(s)
            assert tuple(seg.data[0:2]) == (0xff, 0xff)
            # 2 bytes for the ffff
            # 4 bytes per segment for start, end address
            # An extra segment has been inserted for the run address!
            size = reduce(lambda a, b:a + len(b), subseg, 0)
            assert len(seg) == 2 + size
            print id(s[1]), list(s[1].iter_comments_in_segment())
            print id(subseg[2]), list(subseg[2].iter_comments_in_segment())
            for i, c in s[1].iter_comments_in_segment():
                assert c == subseg[2].get_comment(i + 4)
            assert np.all(s[1].style[:] == subseg[2].style[4:])

    def test_copy(self):
        for s in self.segments:
            d = s.rawdata
            print "orig:", d.data.shape, d.is_indexed, d.data, id(d.data)
            c = d.copy()
            print "copy", c.data.shape, c.is_indexed, c.data, id(c.data)
            assert c.data.shape == s.data.shape
            assert id(c) != id(s)
            assert np.all((c.data[:] - s.data[:]) == 0)
            c.data[0:100] = 1
            print d.data
            print c.data
            assert not np.all((c.data[:] - s.data[:]) == 0)


class TestIndexed(object):
    def setup(self):
        data = np.arange(4096, dtype=np.uint8)
        data[1::2] = np.repeat(np.arange(16, dtype=np.uint8), 128)
{'fraction_non_alphanumeric': 0.13359683794466404, 'fraction_numerical': 0.030039525691699605, 'mean_word_length': 2.045126353790614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4361729', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1685, 'n_words': 404}