public unsafe void BuildTreeAndOverlapTasks([Values(2, 10, 33, 100)] int elementCount)
        {
            const int threadCount = 8;

            elementCount *= 2;

            var tree = new Broadphase.Tree(elementCount);

            var points      = new NativeArray <PointAndIndex>(elementCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            var aabbs       = new NativeArray <Aabb>(elementCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            var branchCount = new NativeArray <int>(1, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

            InitInputWithCopyArrays(points, aabbs, tree.BodyFilters);

            // Override filter data with default filters.
            for (int i = 0; i < tree.BodyFilters.Length; i++)
            {
                tree.BodyFilters[i] = CollisionFilter.Default;
            }

            for (int i = 0; i < tree.NodeFilters.Length; i++)
            {
                tree.NodeFilters[i] = CollisionFilter.Default;
            }

            var branchNodeOffset = new NativeArray <int>(Constants.MaxNumTreeBranches, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            var shouldDoWork     = new NativeArray <int>(1, Allocator.Persistent);

            shouldDoWork[0] = 1;
            NativeArray <int> oldBranchCount = new NativeArray <int>(1, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

            JobHandle handle = new BuildFirstNLevelsJob
            {
                Points            = points,
                Nodes             = (Node *)tree.Nodes.GetUnsafePtr(),
                Ranges            = tree.Ranges,
                BranchNodeOffsets = branchNodeOffset,
                BranchCount       = branchCount,
                OldBranchCount    = oldBranchCount,
                ThreadCount       = threadCount,
                ShouldDoWork      = shouldDoWork
            }.Schedule();

            handle = new BuildBranchesJob
            {
                Points            = points,
                Aabbs             = aabbs,
                BodyFilters       = tree.BodyFilters,
                Nodes             = (Node *)tree.Nodes.GetUnsafePtr(),
                Ranges            = tree.Ranges,
                BranchNodeOffsets = branchNodeOffset,
                BranchCount       = branchCount
            }.ScheduleUnsafeIndex0(branchCount, 1, handle);

            new FinalizeTreeJob
            {
                Aabbs             = aabbs,
                LeafFilters       = tree.BodyFilters,
                Nodes             = (Node *)tree.Nodes.GetUnsafePtr(),
                BranchNodeOffsets = branchNodeOffset,
                NumNodes          = tree.Nodes.Length,
                BranchCount       = branchCount,
                ShouldDoWork      = shouldDoWork,
                OldBranchCount    = oldBranchCount
            }.Schedule(handle).Complete();

            int numBranchOverlapPairs = branchCount[0] * (branchCount[0] + 1) / 2;
            var nodePairIndices       = new NativeList <int2>(Allocator.TempJob);

            nodePairIndices.ResizeUninitialized(numBranchOverlapPairs);
            var collisionPairs = new NativeStream(numBranchOverlapPairs, Allocator.TempJob);

            handle = new Broadphase.DynamicVsDynamicBuildBranchNodePairsJob
            {
                Ranges          = tree.Ranges,
                NumBranches     = branchCount,
                NodePairIndices = nodePairIndices
            }.Schedule();

            handle = new Broadphase.DynamicVsDynamicFindOverlappingPairsJob
            {
                DynamicTree     = tree,
                NodePairIndices = nodePairIndices,
                PairWriter      = collisionPairs.AsWriter()
            }.Schedule(nodePairIndices, numBranchOverlapPairs, handle);

            handle.Complete();

            int numPairs = collisionPairs.Count();

            Assert.AreEqual(elementCount / 2, numPairs);
            //Debug.Log($"Num colliding pairs: {numPairs}");

            tree.BoundingVolumeHierarchy.CheckIntegrity();

            nodePairIndices.Dispose();
            tree.Dispose();
            collisionPairs.Dispose();
            branchCount.Dispose();
            shouldDoWork.Dispose();
        }
 static void ExpectedCount(ref NativeStream container, int expected)
 {
     Assert.AreEqual(expected == 0, container.IsEmpty());
     Assert.AreEqual(expected, container.Count());
 }