<commit_before>﻿using System;
using System.Runtime.InteropServices;

using Kirkin.Memory;

using NUnit.Framework;

namespace Kirkin.Tests.Memory
{
    public unsafe class RawCopyTests
    {
        [Test]
        public void RawCopy_Int32()
        {
            int a = -1;
            int b = 0;

            RawCopy.CopyBytes(&a, &b, sizeof(int));

            Assert.AreEqual(-1, b);
        }

        [Test]
        public void RawCopy_Bool()
        {
            bool value = true;
            int result = 0;

            RawCopy.CopyBytes(&value, 0, &result, 0, sizeof(bool));

            Assert.AreEqual(1, result);

            RawCopy.CopyBytes(&value, 0, &result, 1, sizeof(bool));

            Assert.AreEqual(257, result);
        }

        [Test]
        public void ReadWrite_Int8()
        {
            Block8 value = new Block8();

            RawCopy.RefInt32(&value) = 1;

            Assert.AreEqual(1, RawCopy.RefInt32(&value));
        }

        [Test]
        public void ReadWrite_Int32()
        {
            Block32 value = new Block32();

            Assert.AreEqual(0, RawCopy.RefInt32(&value));

            RawCopy.WriteBytes(&value, new byte[] { 255, 255, 255, 255 });

            Assert.AreEqual(-1, RawCopy.RefInt32(&value));
        }

        [Test]
        public void ReadWrite_Int64()
        {
            Block64 value = new Block64();

            Assert.AreEqual(0, RawCopy.RefInt64(&value));

            RawCopy.RefInt64(&value) = long.MaxValue;

            Assert.AreEqual(-1, RawCopy.RefInt32(&value));
            Assert.AreEqual(int.MaxValue, RawCopy.RefInt32(&value, 4));

            RawCopy.RefInt32(&value, 4) = -1;

            Assert.AreEqual(-1, RawCopy.RefInt64(&value));
        }

        [Test]
        public void ReadWrite_Bytes()
        {
            byte[] bytes;
            int value = 0;

            bytes = RawCopy.ReadBytes(&value, sizeof(int));

            Assert.AreEqual(0, bytes[0]);
            Assert.AreEqual(0, bytes[1]);
            Assert.AreEqual(0, bytes[2]);
            Assert.AreEqual(0, bytes[3]);

            RawCopy.WriteBytes(&value, new byte[] { 255, 255, 255, 255 });

            Assert.AreEqual(-1, RawCopy.RefInt32(&value));

            bytes = RawCopy.ReadBytes(&value, sizeof(int));

            Assert.AreEqual(255, bytes[0]);
            Assert.AreEqual(255, bytes[1]);
            Assert.AreEqual(255, bytes[2]);
            Assert.AreEqual(255, bytes[3]);
        }

        [Test]
        public void RawCopy_BoxedInt32()
        {
            object a = -1;
            object b = 0;

            using (Pinned pinnedA = new Pinned(a))
            using (Pinned pinnedB = new Pinned(b)) {
                RawCopy.CopyBytes(pinnedA, pinnedB, sizeof(int));
            }

            Assert.AreEqual(-1, b);
            Assert.AreNotSame(a, b);
        }

        [Test]
        public void MutateString()
        {
            string s1 = "Hello world";
            string s2 = "zzz";

            fixed (char* c1 = s1, c2 = s2) {
                RawCopy.CopyBytes(c2, c1, 3 * sizeof(char));
            }

            Assert.AreEqual("zzzlo world", s1);
        }

        [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
        public static extern void* memcpy(void* dest, void* src, int count);

        [Test]
        public void Memcpy_Int32Copy()
        {
            int a = -1;
            int b = 0;

            memcpy(&b, &a, sizeof(int));

            Assert.AreEqual(-1, b);
        }

        [Test]
        public void Benchmark_RawCopy()
        {
            int a = -1;
            int b = 0;

            for (int i = 0; i < 10000000; i++) {
                RawCopy.CopyBytes(&a, &b, sizeof(int));
            }
        }
<commit_msg>RawCopyTests: SuppressUnmanagedCodeSecurityAttribute on memcpy for a fairer comparison<commit_after>﻿using System;
using System.Runtime.InteropServices;
using System.Security;

using Kirkin.Memory;

using NUnit.Framework;

namespace Kirkin.Tests.Memory
{
    public unsafe class RawCopyTests
    {
        [Test]
        public void RawCopy_Int32()
        {
            int a = -1;
            int b = 0;

            RawCopy.CopyBytes(&a, &b, sizeof(int));

            Assert.AreEqual(-1, b);
        }

        [Test]
        public void RawCopy_Bool()
        {
            bool value = true;
            int result = 0;

            RawCopy.CopyBytes(&value, 0, &result, 0, sizeof(bool));

            Assert.AreEqual(1, result);

            RawCopy.CopyBytes(&value, 0, &result, 1, sizeof(bool));

            Assert.AreEqual(257, result);
        }

        [Test]
        public void ReadWrite_Int8()
        {
            Block8 value = new Block8();

            RawCopy.RefInt32(&value) = 1;

            Assert.AreEqual(1, RawCopy.RefInt32(&value));
        }

        [Test]
        public void ReadWrite_Int32()
        {
            Block32 value = new Block32();

            Assert.AreEqual(0, RawCopy.RefInt32(&value));

            RawCopy.WriteBytes(&value, new byte[] { 255, 255, 255, 255 });

            Assert.AreEqual(-1, RawCopy.RefInt32(&value));
        }

        [Test]
        public void ReadWrite_Int64()
        {
            Block64 value = new Block64();

            Assert.AreEqual(0, RawCopy.RefInt64(&value));

            RawCopy.RefInt64(&value) = long.MaxValue;

            Assert.AreEqual(-1, RawCopy.RefInt32(&value));
            Assert.AreEqual(int.MaxValue, RawCopy.RefInt32(&value, 4));

            RawCopy.RefInt32(&value, 4) = -1;

            Assert.AreEqual(-1, RawCopy.RefInt64(&value));
        }

        [Test]
        public void ReadWrite_Bytes()
        {
            byte[] bytes;
            int value = 0;

            bytes = RawCopy.ReadBytes(&value, sizeof(int));

            Assert.AreEqual(0, bytes[0]);
            Assert.AreEqual(0, bytes[1]);
            Assert.AreEqual(0, bytes[2]);
            Assert.AreEqual(0, bytes[3]);

            RawCopy.WriteBytes(&value, new byte[] { 255, 255, 255, 255 });

            Assert.AreEqual(-1, RawCopy.RefInt32(&value));

            bytes = RawCopy.ReadBytes(&value, sizeof(int));

            Assert.AreEqual(255, bytes[0]);
            Assert.AreEqual(255, bytes[1]);
            Assert.AreEqual(255, bytes[2]);
            Assert.AreEqual(255, bytes[3]);
        }

        [Test]
        public void RawCopy_BoxedInt32()
        {
            object a = -1;
            object b = 0;

            using (Pinned pinnedA = new Pinned(a))
            using (Pinned pinnedB = new Pinned(b)) {
                RawCopy.CopyBytes(pinnedA, pinnedB, sizeof(int));
            }

            Assert.AreEqual(-1, b);
            Assert.AreNotSame(a, b);
        }

        [Test]
        public void MutateString()
        {
            string s1 = "Hello world";
            string s2 = "zzz";

            fixed (char* c1 = s1, c2 = s2) {
                RawCopy.CopyBytes(c2, c1, 3 * sizeof(char));
            }

            Assert.AreEqual("zzzlo world", s1);
        }

        [SuppressUnmanagedCodeSecurity]
        [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
        public static extern void* memcpy(void* dest, void* src, int count);

        [Test]
        public void Memcpy_Int32Copy()
        {
            int a = -1;
            int b = 0;

            memcpy(&b, &a, sizeof(int));

            Assert.AreEqual(-1, b);
        }

        [Test]
        public void Benchmark_RawCopy()
        {
            int a = -1;
            int b = 0;

            for (int i = 0; i < 10000000; i++) {
                RawCopy.CopyBytes(&a, &b, sizeof(int));
            }
        }

{'fraction_non_alphanumeric': 0.12560819462227912, 'fraction_numerical': 0.03636363636363636, 'mean_word_length': 1.5727931488801055, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1545984', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2292, 'n_words': 508}