<commit_before>#![feature(core)]

#[macro_use]
extern crate log;

extern crate opencl;

use opencl::hl::*;

macro_rules! expect (
    ($test: expr, $expected: expr) => ({
            let test     = $test;
            let expected = $expected;
            if test != expected {
                panic!(format!("Test failure in {}:", // " expected {}, got {}",
                              stringify!($test)/*,
                              expected, test*/))
            }
        }
    )
);

pub fn test_all_platforms_devices<F>(test: &mut F)
    where F: FnMut(&Device, &Context, &CommandQueue)
{
    let platforms = get_platforms();
    for p in platforms.iter() {
        let devices = p.get_devices();
        for d in devices.iter() {
            let context = d.create_context();
            let queue = context.create_command_queue(d);
            test(d, &context, &queue);
        }
    }
}

mod mem {
    use std::slice;
    use opencl::mem::{Read, Write};

    fn read_write<W: Write, R: Read>(src: &W, dst: &mut R)
    {
        // find the max size of the input buffer
        let mut max = 0;
        src.write(|off, _, len| {
            if max < off + len {
                max = off + len;
            }
        });
        let max = max as usize;

        let mut buffer: Vec<u8> = Vec::new();
        unsafe {
            buffer.reserve(max);
            buffer.set_len(max);
        }

        // copy from input into buffer
        src.write(|off, ptr, len| {
            let off = off as usize;
            let len = len as usize;
            assert!(buffer.len() >= (off + len) as usize);
            let target = &mut buffer[off .. off + len];
            unsafe {
                let ptr = ptr as *const u8;
                let src = slice::from_raw_buf(&ptr, len);
                slice::bytes::copy_memory(target, src);
            }
        });

        // copy from buffer into output
        dst.read(|off, ptr, len| {
            let off = off as usize;
            let len = len as usize;
            assert!(buffer.len() >= (off + len) as usize);
            let src = &buffer[off .. off + len];
            unsafe {
                let ptr = ptr as *mut u8;
                let dst = slice::from_raw_mut_buf(&ptr, len);
                slice::bytes::copy_memory(dst, src);
            }
        })
    }

    #[test]
    fn read_write_slice()
    {
        let input: &[isize] = &[0, 1, 2, 3, 4, 5, 6, 7];
        let mut output: &mut [isize] = &mut [0, 0, 0, 0, 0, 0, 0, 0];
        read_write(&input, &mut output);
        expect!(input, output);
    }

    #[test]
    fn read_write_int()
    {
        let input: isize      = 3141;
        let mut output: isize = 0;
        read_write(&input, &mut output);
        expect!(input, output);
    }

    #[test]
    fn read_write_uint()
    {
        let input : usize = 3141;
        let mut output : usize = 0;<commit_msg>Use from_raw_parts instead of from_raw_buf<commit_after>#![feature(core)]

#[macro_use]
extern crate log;

extern crate opencl;

use opencl::hl::*;

macro_rules! expect (
    ($test: expr, $expected: expr) => ({
            let test     = $test;
            let expected = $expected;
            if test != expected {
                panic!(format!("Test failure in {}:", // " expected {}, got {}",
                              stringify!($test)/*,
                              expected, test*/))
            }
        }
    )
);

pub fn test_all_platforms_devices<F>(test: &mut F)
    where F: FnMut(&Device, &Context, &CommandQueue)
{
    let platforms = get_platforms();
    for p in platforms.iter() {
        let devices = p.get_devices();
        for d in devices.iter() {
            let context = d.create_context();
            let queue = context.create_command_queue(d);
            test(d, &context, &queue);
        }
    }
}

mod mem {
    use std::slice;
    use opencl::mem::{Read, Write};

    fn read_write<W: Write, R: Read>(src: &W, dst: &mut R)
    {
        // find the max size of the input buffer
        let mut max = 0;
        src.write(|off, _, len| {
            if max < off + len {
                max = off + len;
            }
        });
        let max = max as usize;

        let mut buffer: Vec<u8> = Vec::new();
        unsafe {
            buffer.reserve(max);
            buffer.set_len(max);
        }

        // copy from input into buffer
        src.write(|off, ptr, len| {
            let off = off as usize;
            let len = len as usize;
            assert!(buffer.len() >= (off + len) as usize);
            let target = &mut buffer[off .. off + len];
            unsafe {
                let ptr = ptr as *const u8;
                let src = slice::from_raw_parts(ptr, len);
                slice::bytes::copy_memory(target, src);
            }
        });

        // copy from buffer into output
        dst.read(|off, ptr, len| {
            let off = off as usize;
            let len = len as usize;
            assert!(buffer.len() >= (off + len) as usize);
            let src = &buffer[off .. off + len];
            unsafe {
                let ptr = ptr as *mut u8;
                let dst = slice::from_raw_parts_mut(ptr, len);
                slice::bytes::copy_memory(dst, src);
            }
        })
    }

    #[test]
    fn read_write_slice()
    {
        let input: &[isize] = &[0, 1, 2, 3, 4, 5, 6, 7];
        let mut output: &mut [isize] = &mut [0, 0, 0, 0, 0, 0, 0, 0];
        read_write(&input, &mut output);
        expect!(input, output);
    }

    #[test]
    fn read_write_int()
    {
        let input: isize      = 3141;
        let mut output: isize = 0;
        read_write(&input, &mut output);
        expect!(input, output);
    }

    #[test]
    fn read_write_uint()
    {
        let input : usize = 3141;
        let mut output : usize = 0;
{'fraction_non_alphanumeric': 0.13574970080355617, 'fraction_numerical': 0.010258163788681825, 'mean_word_length': 1.496798975672215, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '1299904', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1787, 'n_words': 525}