import pytest
import os, sys
from creader import RawFileReader, DetectorType
from fixtures import data_path
import numpy as np

m03 = DetectorType.MOENCH_03
m04a= DetectorType.MOENCH_04_A
m04ad= DetectorType.MOENCH_04_AD

def test_references_on_read(data_path):
    fname= data_path/'Moench03_d0_f0_0.raw'
    r = RawFileReader(fname, m03)
    frames = r.read(10)
    assert sys.getrefcount(frames) == 2 #Over counts by one due to call by reference

def test_references_on_read_with_header(data_path):
    fname= data_path/'Moench03_d0_f0_0.raw'
    r = RawFileReader(fname, m03, header = True)
    frames, header = r.read(100)
    assert sys.getrefcount(frames) == 2 #Over counts by one due to call by reference
    assert sys.getrefcount(header) == 2 

def test_reading_frame_numbers(data_path):
    fname= data_path/'Moench03_d0_f0_0.raw'
    r = RawFileReader(fname, header = True, detector_type = m03)
    frames, header = r.read(1000)
    assert (header['Frame Number'] == np.arange(201,1201, dtype = np.uint64)).all()

def test_reading_more_files_than_available(data_path):
    fname= data_path/'Moench03_d0_f0_0.raw'
    r = RawFileReader(fname, m03, header = True)
    frames, header = r.read(1500)
    assert frames.shape == (1000,400, 400)
    assert header.size == 1000

def test_references_on_m04_a(data_path):
    fname= data_path/'Moench04_no_digital_d0_f0_0.raw'
    r = RawFileReader(fname, m04a)
    frames = r.read(3)
    assert sys.getrefcount(frames) == 2 #Over counts by one due to call by reference
    assert frames.shape == (3,400,400)

def test_references_on_m04_a_with_heaer(data_path):
    fname= data_path/'Moench04_no_digital_d0_f0_0.raw'
    r = RawFileReader(fname, m04a, header = True)
    frames, header = r.read(7)
    assert sys.getrefcount(frames) == 2 #Over counts by one due to call by reference
    assert frames.shape == (7,400,400)

    assert sys.getrefcount(header) == 2 
    assert header.size == 7

def test_references_on_m04_ad(data_path):
    fname= data_path/'Moench04_digital_d0_f0_0.raw'
    r = RawFileReader(fname, m04ad)
    analog, digital = r.read(3)
    assert sys.getrefcount(analog) == 2 #Over counts by one due to call by reference
    assert analog.shape == (3,400,400)
    assert sys.getrefcount(digital) == 2

def test_references_on_m04_ad_with_header(data_path):
    fname= data_path/'Moench04_digital_d0_f0_0.raw'
    r = RawFileReader(fname, m04ad, header = True)
    analog, digital, header = r.read(3)
    assert sys.getrefcount(analog) == 2 #Over counts by one due to call by reference
    assert analog.shape == (3,400,400)
    assert sys.getrefcount(digital) == 2
    assert sys.getrefcount(header) == 2 

def test_read_too_many_m04_frames(data_path):
    fname= data_path/'Moench04_digital_d0_f0_0.raw'
    r = RawFileReader(fname, m04ad, header = True)
    analog, digital, header = r.read(150)
    assert analog.shape == (100,400,400)
    assert digital.shape == (100, 400, 400)
    assert header.size == 100
    assert sys.getrefcount(digital) == 2