Example #1
0
        public void running_a_scan_when_any_pointers_are_referenced_keeps_the_arena()
        {
            // scan takes a list of known references, and assumes anything
            // that's not in the list in not referenced. Any arena with nothing
            // referenced is reset.

            var mem     = new MemorySimulator(Mega.Bytes(1));
            var bump    = (Allocator.ArenaSize / 4) + 1; // three fit in each arena, with some spare
            var subject = new Allocator(512, Mega.Bytes(1), mem);

            var x1  = subject.Alloc(bump).Value;
            var ar1 = subject.CurrentArena();

            subject.Alloc(bump); subject.Alloc(bump); // fill up first arena

            var x2  = subject.Alloc(bump).Value;
            var ar2 = subject.CurrentArena();

            Assert.That(ar1, Is.Not.EqualTo(ar2), "Failed to trigger a new arena");

            // Check that both arenas are non-empty:
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Not.Zero);
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);

            // Run the scan (note we don't need all pointers, just one from each arena)
            var list = new Vector <long>(subject, mem);

            list.Push(x1);
            list.Push(x2);
            subject.ScanAndSweep(list);

            // Check nothing has been cleared
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Not.Zero);
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);
        }
        private MegaUser Login(bool save, string userAccountFile)
        {
            MegaUser u;

            if ((u = Mega.LoadAccount(userAccountFile)) == null)
            {
                save = true;
            }

            Status = ConnectivityStatus.LogginIn;

            Mega.Init(u, (m) =>
            {
                api = m;
                if (save)
                {
                    SaveAccount(userAccountFile, "user.anon.dat");
                }

                Status = ConnectivityStatus.AccountLoaded;

                if (api.User.Status == MegaUserStatus.Anonymous)
                {
                    Title = TITLE + " - anonymous account";
                }
                else
                {
                    Title = TITLE + " - " + m.User.Email;
                }
            }, (e) => { MessageBox.Show("Error while loading account: " + e); Application.Current.Shutdown(); });
            return(u);
        }
Example #3
0
    public static void Main(string[] args)
    {
        int i, bolas = 10;

        Console.WriteLine($"\nBingo de {bolas} bolas\n-----------------------------");
        Bingo b = new Bingo();

        b.Iniciar(bolas);
        for (i = 0; i < bolas; i++)
        {
            Console.WriteLine($"Sorteando ... {b.Proximo()}");
        }

        Console.WriteLine("\nBolas sorteadas\n--------------------");
        foreach (int bola in b.Sorteados())
        {
            Console.WriteLine($"{bola}\t");
        }

        Console.WriteLine($"\nMega Sena\n-----------------------------");
        Mega ms = new Mega();

        for (i = 0; i < 6; i++)
        {
            Console.WriteLine($"Sorteando ... {ms.Proximo()}");
        }

        Console.WriteLine("\nBolas sorteadas\n--------------------");
        foreach (int bola in ms.Sorteados())
        {
            Console.WriteLine($"{bola}\t");
        }

        return;
    }
Example #4
0
        public void deallocating_the_hash_map_releases_memory()
        {
            var mem     = new MemorySimulator(Mega.Bytes(20));
            var alloc   = new Allocator(Mega.Bytes(10), Mega.Bytes(20), mem);
            var subject = new TaggedHashMap(256, alloc, mem);

            for (ulong i = 0; i < 128; i++)
            {
                subject.Add(i, i * 2);
            }


            // Check that memory is used...
            alloc.GetState(out var allocatedBytes, out var unallocatedBytes, out var occupiedArenas, out var emptyArenas, out var totalReferenceCount, out var largestContiguous);
            Assert.That(allocatedBytes, Is.GreaterThanOrEqualTo(6000), "Allocated bytes looks too small");
            //Assert.That(unallocatedBytes, Is.LessThan(Mega.Bytes(10)), "Unallocated bytes looks too big: "+unallocatedBytes);
            Assert.That(occupiedArenas, Is.EqualTo(1), "Occupied arenas looks wrong");
            Assert.That(emptyArenas, Is.GreaterThanOrEqualTo(100), "Empty arenas looks wrong");
            Assert.That(totalReferenceCount, Is.GreaterThan(2), "Reference count looks wrong");
            Assert.That(largestContiguous, Is.EqualTo(Allocator.ArenaSize), "Should not have exhausted memory!");

            // Release the hash map
            subject.Deallocate();

            // Check that everything is released
            alloc.GetState(out allocatedBytes, out unallocatedBytes, out occupiedArenas, out emptyArenas, out totalReferenceCount, out largestContiguous);
            Assert.That(allocatedBytes, Is.EqualTo(0), "Memory was not freed");
            Assert.That(unallocatedBytes, Is.GreaterThan(Mega.Bytes(9)), "Unallocated bytes not correct");
            Assert.That(occupiedArenas, Is.EqualTo(0), "Some arenas still occupied");
            Assert.That(emptyArenas, Is.GreaterThanOrEqualTo(100), "Empty arenas looks wrong");
            Assert.That(totalReferenceCount, Is.EqualTo(0), "Some references still dangling");
            Assert.That(largestContiguous, Is.EqualTo(Allocator.ArenaSize), "Should not have exhausted memory!");
        }
Example #5
0
    public static void Main(string[] args)
    {
        Bingo b = new Bingo();

        b.Iniciar(10);
        for (int i = 0; i < 10; i++)
        {
            b.Proximo();
        }
        Console.WriteLine("Numeros do bingo:");
        foreach (int q in b.Sorteados())
        {
            Console.WriteLine(q);
        }

        Mega m = new Mega();

        m.Iniciar();
        for (int i = 0; i < 6; i++)
        {
            m.Proximo();
        }
        Console.WriteLine("Numeros da loteria:");
        foreach (int a in m.Sorteados())
        {
            Console.WriteLine(a);
        }
    }
Example #6
0
        public void Vector__vs__LinkedList()
        {
            var sara_time   = new Stopwatch();
            var dotnet_time = new Stopwatch();

            var mem    = new MemorySimulator(Mega.Bytes(50));
            var sara   = new Vector <int>(new Allocator(0, Mega.Bytes(50), mem), mem);
            var dotnet = new LinkedList <int>();

            sara_time.Start();
            for (int i = 0; i < 2500; i++)
            {
                sara.Push(i);
            }
            for (int i = 0; i < 2500; i++)
            {
                sara.Pop();
            }
            sara_time.Stop();


            dotnet_time.Start();
            for (int i = 0; i < 2500; i++)
            {
                dotnet.AddLast(i);
            }
            for (int i = 0; i < 2500; i++)
            {
                dotnet.RemoveLast();
            }
            dotnet_time.Stop();

            Assert.Pass("SArA: " + sara_time.Elapsed + " ; dotnet: " + dotnet_time.Elapsed);
        }
Example #7
0
        public void trying_to_write_past_the_end_of_a_sibling_chain_will_fail()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Tree <SampleElement>(new Allocator(0, Mega.Bytes(1), mem), mem);

            //##### BUILD #####
            subject.SetRootValue(Sample[0]);

            // First child of root
            var ptrRes = subject.AddChild(subject.Root, Sample[1]); if (!ptrRes.Success)

            {
                Assert.Fail("Failed to add child 1");
            }

            // ( 1 )

            // Second child of root
            ptrRes = subject.AddChild(subject.Root, Sample[2]); if (!ptrRes.Success)
            {
                Assert.Fail("Failed to add child 2");
            }
            // ( 1, 2 )

            //##### ACTION #####
            // Try to write past the last index
            ptrRes = subject.InsertChild(parent: subject.Root, targetIndex: 3, element: Sample[3]);

            Assert.That(ptrRes.Success, Is.False, "Wrote pass the last valid index");
        }
Example #8
0
        public void freeing_the_list_frees_all_chunks()
        {
            // Setup, and allocate a load of entries
            var memsize = Mega.Bytes(1);
            var mem     = new MemorySimulator(memsize);
            var alloc   = new Allocator(0, memsize, mem);
            var subject = new Vector <SampleElement>(alloc, mem);

            int full = (int)(Allocator.ArenaSize / sizeof(long)) * 2;

            var expected = (full * 8) + ((full / 32) * 8);

            Console.WriteLine("Expecting to allocate " + expected);
            if (expected >= memsize)
            {
                Assert.Fail("memsize is not big enough for the test");
            }

            for (int i = 0; i < full; i++)
            {
                subject.Push(Sample(i));
            }

            // Now free the whole list at once (including base ptr)
            subject.Deallocate();

            // check we've emptied memory
            alloc.GetState(out var allocatedBytes, out _, out var post_occupied, out _, out var post_refCount, out _);
            Assert.That(allocatedBytes, Is.Zero, "Expected no bytes allocated");
            Assert.That(post_occupied, Is.Zero, "Expected no arenas occupied");
            Assert.That(post_refCount, Is.Zero, "Expected no references");
        }
Example #9
0
        public void stress_test()
        {
            var rnd = new Random();
            // we deliberately use a small initial size to stress the scaling.
            // if you can afford to oversize the map, that will make things a lot faster
            var mem     = new MemorySimulator(Mega.Bytes(10));
            var subject = new TaggedHashMap(100, new Allocator(0, Mega.Bytes(10), mem), mem);

            subject.Add(0, 1);
            for (int i = 0; i < /*100000*/ 25_000; i++) // 25'000 should be under a second
            {
                if (!subject.Put((ulong)rnd.Next(1, 1_000_000), (ulong)i, true))
                {
                    Assert.Fail("Put rejected the change");
                }
                subject.Remove((ulong)rnd.Next(1, 1_000_000));
            }

            Assert.That(subject.Count, Is.GreaterThan(1000)); // there will probably be key collisions

            var ok = subject.Get(0, out var val);

            Assert.That(ok, Is.True);
            Assert.That(val, Is.EqualTo(1));
        }
Example #10
0
        public void can_create_a_vector_larger_than_the_arena_limit()
        {
            var memsize = Mega.Bytes(1);
            var mem     = new MemorySimulator(memsize);
            var subject = new Vector <SampleElement>(new Allocator(0, memsize, mem), mem);

            uint full = (uint)(Allocator.ArenaSize / sizeof(long)) * 2;

            var expected = (full * 8) + ((full / 32) * 8);

            Console.WriteLine("Expecting to allocate " + expected);
            if (expected >= memsize)
            {
                Assert.Fail("memsize is not big enough for the test");
            }

            for (int i = 0; i < full; i++)
            {
                subject.Push(Sample(i));
            }

            var res = subject.Get(full - 1);

            Assert.That(res.Value.a, Is.EqualTo(full - 1));
        }
Example #11
0
    public static void Main(string[] args)
    {
        //Criar instĂ¢ncia
        Bingo b = new Bingo();

        b.Iniciar(10);

        //'Proximo' sortea um nĂºmero ainda nĂ£o sorteado
        for (int i = 0; i < 10; i++)
        {
            //Escrever nĂºmero sorteado
            Console.WriteLine(b.Proximo());
        }

        //MEGA
        Console.WriteLine("Mega:");

        //Criar instĂ¢ncia
        Mega m = new Mega();


        for (int i = 0; i < 10; i++)
        {
            m.Proximo();
        }

        foreach (int i in m.Sorteados())
        {
            Console.WriteLine(i);
        }
    }
Example #12
0
        public Form1()
        {
            InitializeComponent();

            System.Net.ServicePointManager.DefaultConnectionLimit = 50;
            notifyIcon1.Icon   = Properties.Resources.min;
            notifyIcon1.Click += notifyIcon1_MouseClick;

            if (!string.IsNullOrEmpty(Properties.Settings.Default.folderPath))
            {
                textBoxFolder.Text = Properties.Settings.Default.folderPath;
            }
            else
            {
                textBoxFolder.Text = Path.Combine(
                    Environment.GetEnvironmentVariable("USERPROFILE"),
                    "MegaDesktop");
            }

            auth = Mega.LoadAccount(GetUserKeyFilePath());
            if (auth != null)
            {
                textBoxEmail.Text    = auth.Email;
                textBoxPassword.Text = "password";
            }
        }
Example #13
0
        public void running_a_scan_when_no_pointers_are_referenced_resets_the_arena()
        {
            var bump    = (Allocator.ArenaSize / 4) + 1; // three fit in each arena, with some spare
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(512, Mega.Bytes(1), mem);

            subject.Alloc(bump);
            var ar1 = subject.CurrentArena();

            subject.Alloc(bump); subject.Alloc(bump); // fill up first arena

            var x2  = subject.Alloc(bump).Value;
            var ar2 = subject.CurrentArena();

            Assert.That(ar1, Is.Not.EqualTo(ar2), "Failed to trigger a new arena");

            // Check that both arenas are non-empty:
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Not.Zero);
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);

            // Run the scan (only including the second arena)
            var list = new Vector <long>(subject, mem);

            list.Push(x2);
            subject.ScanAndSweep(list);

            // Check nothing has been cleared
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Zero, "Unreferenced arena was not reset");
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);
        }
Example #14
0
        public void can_remove_values()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new TaggedHashMap(64, new Allocator(0, Mega.Bytes(1), mem), mem);

            subject.Add(50000000, 123);
            subject.Add(1, 456);

            var ok1 = subject.Get(1, out _);
            var ok2 = subject.Get(50000000, out _);
            var ok3 = subject.Get(50, out _);

            Assert.That(ok1, Is.True);
            Assert.That(ok2, Is.True);
            Assert.That(ok3, Is.False);

            // remove one value at beginning
            subject.Remove(1);

            ok1 = subject.Get(1, out _);
            ok2 = subject.Get(50000000, out _);
            ok3 = subject.Get(50, out _);

            Assert.That(ok1, Is.False);
            Assert.That(ok2, Is.True);
            Assert.That(ok3, Is.False);
        }
Example #15
0
        public void building_and_walking_a_tree()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Tree <SampleElement>(new Allocator(0, Mega.Bytes(1), mem), mem);

            //##### BUILD #####
            subject.SetRootValue(Sample[0]);

            // First child of root
            var ptrRes = subject.AddChild(subject.Root, Sample[1]);

            if (!ptrRes.Success)
            {
                Assert.Fail("Failed to add child");
            }

            // Second child of root
            ptrRes = subject.AddChild(subject.Root, Sample[2]);
            if (!ptrRes.Success)
            {
                Assert.Fail("Failed to add child");
            }
            var rc2 = ptrRes.Value;

            // Child of second child
            ptrRes = subject.AddChild(rc2, Sample[3]);
            if (!ptrRes.Success)
            {
                Assert.Fail("Failed to add child");
            }

            //##### WALK #####
            var body1 = subject.ReadBody(subject.Root);

            Assert.That(body1, Is.EqualTo(Sample[0]));

            var child1Res = subject.Child(subject.Root);

            Assert.IsTrue(child1Res.Success);

            var child1childRes = subject.Child(child1Res.Value);

            Assert.IsFalse(child1childRes.Success);

            var child2Res = subject.Sibling(child1Res.Value);

            Assert.IsTrue(child2Res.Success);

            var child3Res = subject.Sibling(child2Res.Value);

            Assert.IsFalse(child3Res.Success);

            var child2childRes = subject.Child(child2Res.Value);

            Assert.IsTrue(child2childRes.Success);
            var body_c2c1 = subject.ReadBody(child2childRes.Value);

            Assert.That(body_c2c1, Is.EqualTo(Sample[3]));
        }
Example #16
0
        private void MegaConfigureTab(bool tryLogin)
        {
            Color OkColor  = Color.Green;
            Color NokColor = Color.DarkRed;

            tpMega.Enabled = false;

            atcMegaAccountType.AccountTypeChanged -= atcMegaAccountType_AccountTypeChanged;
            atcMegaAccountType.SelectedAccountType = Config.MegaAnonymousLogin ? AccountType.Anonymous : AccountType.User;
            atcMegaAccountType.AccountTypeChanged += atcMegaAccountType_AccountTypeChanged;

            pnlMegaLogin.Enabled = !Config.MegaAnonymousLogin;

            if (Config.MegaAuthInfos != null)
            {
                txtMegaEmail.Text = Config.MegaAuthInfos.Email;
            }

            if (Config.MegaAnonymousLogin)
            {
                lblMegaStatus.Text      = "Configured (anonymous)";
                lblMegaStatus.ForeColor = OkColor;
            }
            else if (Config.MegaAuthInfos == null)
            {
                lblMegaStatus.Text      = "Not configured";
                lblMegaStatus.ForeColor = NokColor;
            }
            else
            {
                cbMegaFolder.Items.Clear();

                Mega mega = new Mega(Config.MegaAuthInfos);
                if (!tryLogin || mega.TryLogin())
                {
                    lblMegaStatus.Text      = "Configured";
                    lblMegaStatus.ForeColor = OkColor;

                    if (tryLogin)
                    {
                        Mega.DisplayNode[] nodes = mega.GetDisplayNodes().ToArray();
                        cbMegaFolder.Items.AddRange(nodes);
                        cbMegaFolder.SelectedItem = nodes.FirstOrDefault(n => n.Node != null && n.Node.Id == Config.MegaParentNodeId) ?? Mega.DisplayNode.EmptyNode;
                    }
                    else
                    {
                        cbMegaFolder.Items.Add("[Click refresh folders]");
                        cbMegaFolder.SelectedIndex = 0;
                    }
                }
                else
                {
                    lblMegaStatus.Text      = "Invalid authentication";
                    lblMegaStatus.ForeColor = NokColor;
                }
            }

            tpMega.Enabled = true;
        }
Example #17
0
        public void can_allocate_memory_from_a_pool_and_get_a_pointer()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(10), mem);

            long ptr = subject.Alloc(byteCount: Kilo.Bytes(1)).Value;

            Assert.That(ptr, Is.GreaterThanOrEqualTo(100));
        }
Example #18
0
        static void Main(string[] args)
        {
            int opt = 0;

            Console.WriteLine("Escolha o tipo de sorteio:");
            Console.WriteLine("1 - Megasena");
            Console.WriteLine("2 - Quina");
            Console.WriteLine("3 - Lotomania");
            Console.WriteLine("4 - Lotofacil");

            var k = Console.ReadLine();

            int.TryParse(k, out opt);

            Lottery lottery = null;
            int     bids    = 0;

            switch (opt)
            {
            case 1:
            {
                lottery = new Mega();
                bids    = 6;
                break;
            }

            case 2:
            {
                lottery = new Quina();
                bids    = 5;
                break;
            }

            case 3:
            {
                lottery = new Lotomania();
                bids    = 50;
                break;
            }

            case 4:
            {
                lottery = new Lotofacil();
                bids    = 15;
                break;
            }
            }

            while (true)
            {
                var output = string.Join("\t", lottery.Drawn(bids));
                File.AppendAllText("output.txt", output + "\n");
                Console.WriteLine(output);
                Console.ReadKey();
            }
        }
Example #19
0
        [Test] // The vector class can be used to store larger chunks of data
        public void requesting_a_block_larger_than_a_single_area_fails()
        {
            // Doing this to keep things very simple
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(512, Mega.Bytes(1), mem);

            var result = subject.Alloc(Allocator.ArenaSize * 2);

            Assert.That(result.Success, Is.False);
        }
Example #20
0
        public void a_second_allocation_returns_different_memory()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(10), mem);

            long ptr1 = subject.Alloc(byteCount: 256).Value;
            long ptr2 = subject.Alloc(byteCount: 256).Value;

            Assert.That(ptr1, Is.Not.EqualTo(ptr2));
        }
Example #21
0
        public void InBetweenScalingIsBasedOnLargerOfBothOperands()
        {
            var largerOperand   = new Mega();
            var inBetweenPrefix = new Hecto();
            var expectedPrefix  = new Mega();
            var multiplication  = largerOperand.Exponent + inBetweenPrefix.Exponent;
            var operation       = Normalisers.Get(multiplication);

            Assert.Equal(expectedPrefix.Exponent, operation.Exponent);
        }
Example #22
0
        public void can_check_for_presence_of_a_key()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new TaggedHashMap(64, new Allocator(0, Mega.Bytes(1), mem), mem);

            subject.Add(123, 0);
            subject.Add(1, 456);

            Assert.That(subject.ContainsKey(123), Is.True);
            Assert.That(subject.ContainsKey(321), Is.False);
        }
Example #23
0
        private static void Sortear()
        {
            var geradorNumeros = new GeradorMegaSena();

            if (Mega == null)
            {
                Mega = new Mega(geradorNumeros);
            }
            Mega.Sortear();
            MensagemConsole("O nĂºmero da Mega Sena sorteado foi: " + Mega.NumerosSorteados.ToString() + ". Pressione ENTER para retornar ao menu anterior.");
        }
Example #24
0
        public void can_remove_last_child_by_an_index()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Tree <SampleElement>(new Allocator(0, Mega.Bytes(1), mem), mem);

            //##### BUILD #####
            subject.SetRootValue(Sample[0]);

            // First child of root
            var ptrRes = subject.AddChild(subject.Root, Sample[1]); if (!ptrRes.Success)

            {
                Assert.Fail("Failed to add child 1");
            }

            // ( 1 )

            // Second child of root
            ptrRes = subject.AddChild(subject.Root, Sample[2]); if (!ptrRes.Success)
            {
                Assert.Fail("Failed to add child 2");
            }
            // ( 1, 2 )

            // Third child of root
            ptrRes = subject.AddChild(subject.Root, Sample[3]); if (!ptrRes.Success)
            {
                Assert.Fail("Failed to add child 3");
            }
            // ( 1, 2, 3 )

            //##### ACTION #####
            // Delete first item
            subject.RemoveChild(parent: subject.Root, targetIndex: 2);
            // ( 1, 2 )


            //##### WALK #####
            var wres1 = subject.Child(subject.Root);
            var wres2 = subject.SiblingR(wres1);

            Assert.That(wres2.Success, Is.True, "Failed to get full chain");

            Assert.That(subject.SiblingR(wres2).Success, Is.False, "Resulting chain was too long");

            var newChild1 = subject.ReadBody(wres1.Value);

            Assert.That(newChild1, Is.EqualTo(Sample[1]), "Incorrect data (1)");

            var newChild2 = subject.ReadBody(wres2.Value);

            Assert.That(newChild2, Is.EqualTo(Sample[2]), "Incorrect data (2)");
        }
Example #25
0
        public void put_can_replace_an_existing_value()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new TaggedHashMap(64, new Allocator(0, Mega.Bytes(1), mem), mem);

            subject.Put(1, 1, true);
            subject.Put(1, 2, true);  // overwrite
            subject.Put(1, 3, false); // silently abort

            subject.Get(1, out var result);
            Assert.That(result, Is.EqualTo(2));
        }
Example #26
0
        public Sync(string localFolder, Mega api, string remoteFolderName)
        {
            this.localFolder = localFolder;
            var metadataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MegaDesktop");

            Directory.CreateDirectory(metadataFolder);
            metadataFile = Path.Combine(metadataFolder, "files.metadata");

            remoteStore = new NodeStore(api, "sync");

            InitTimer();
            InitWatcher(localFolder);
        }
Example #27
0
        public void allocating_enough_memory_changes_arena()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(1), mem);

            int  first  = subject.CurrentArena();
            long ptr1   = subject.Alloc(Allocator.ArenaSize).Value;
            long ptr2   = subject.Alloc(Kilo.Bytes(1)).Value;
            int  second = subject.CurrentArena();

            Assert.That(ptr1, Is.Not.EqualTo(ptr2));
            Assert.That(first, Is.Not.EqualTo(second));
        }
Example #28
0
        public void memory_exhaustion_results_in_an_error_code()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(10, Mega.Bytes(1), mem);

            Result <long> result = default;

            for (int i = 0; i < 17; i++)
            {
                result = subject.Alloc(Allocator.ArenaSize - 1);
            }

            Assert.That(result.Success, Is.False);
        }
Example #29
0
        public void can_directly_deallocate_a_pointer()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(10), mem);

            long ptr = subject.Alloc(byteCount: 256).Value;

            subject.Deref(ptr);

            var ar   = subject.CurrentArena();
            var refs = subject.ArenaRefCount(ar);

            Assert.That(refs.Value, Is.Zero);
        }
Example #30
0
        public void deallocating_an_old_allocation_does_nothing()
        {
            // Older items just hang around until the entire arena is abandoned
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(10), mem);

            long ptr1 = subject.Alloc(byteCount: 256).Value;
            long ptr2 = subject.Alloc(byteCount: 256).Value;

            subject.Deref(ptr1);
            long ptr3 = subject.Alloc(byteCount: 512).Value;

            Assert.That(ptr3, Is.GreaterThan(ptr2));
        }