<commit_before>/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */
package org.apache.directory.server.core.avltree;

 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Comparator;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * TestCase for AvlTreeMarshaller.
 *
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev$, $Date$
 */
public class AvlTreeMarshallerTest
{
    private static final byte[] SERIALIZED_AVLTREE = 
    { 
        0, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 
        86, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 
        0, 0, 0, 0, 26, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, -122, 
        0, 0, 0, 5, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 0, 
        0, 0, 0, 122, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, -74, 
        0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0 
    };

    AvlTree<Integer> tree;
    Comparator<Integer> comparator;
    AvlTreeMarshaller<Integer> treeMarshaller;
    
    static AvlTree<Integer> savedTree;
    
    File treeFile = new File( System.getProperty( "java.io.tmpdir" ) + File.separator + "avl.tree");
    
    private static final Logger LOG = LoggerFactory.getLogger( AvlTreeMarshallerTest.class.getSimpleName() );

    
    @Before
    public void createTree()
    {
        comparator = new Comparator<Integer>() 
        {
            public int compare( Integer i1, Integer i2 )
            {
                return i1.compareTo( i2 );
            }
        };
        
      
        tree = new AvlTree<Integer>( comparator );
        treeMarshaller = new AvlTreeMarshaller<Integer>( comparator, new IntegerKeyMarshaller() );
    }

    
    @Test
    public void testBadTree() throws IOException
    {
        Comparator<Long> comparator = new Comparator<Long>() 
        {
            public int compare( Long i1, Long i2 )
            {
                return i1.compareTo( i2 );
            }
        };
        
      
        AvlTreeMarshaller<Long> treeMarshaller = new AvlTreeMarshaller<Long>( comparator, new LongMarshaller() );
        treeMarshaller.deserialize( SERIALIZED_AVLTREE );
    }
    

    @Test
    public void testMarshalEmptyTree() throws IOException
    {
        byte[] bites = treeMarshaller.serialize( new AvlTree<Integer>( comparator ) );<commit_msg>commiting new test to show how AvlTree.remove is failing by removing more keys than it should<commit_after>/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */
package org.apache.directory.server.core.avltree;

 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Comparator;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * TestCase for AvlTreeMarshaller.
 *
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev$, $Date$
 */
public class AvlTreeMarshallerTest
{
    private static final long[] AVLTREE_KEYS_PRE_REMOVE =
    {
        2, 14, 26, 86, 110, 122, 134, 182
    };
        
    private static final long[] AVLTREE_EXPECTED_KEYS_POST_REMOVE =
    {
        2, 14, 26, 86, 122, 134, 182
    };
        
    private static final byte[] SERIALIZED_AVLTREE_PRE_REMOVE =
    {
        0, 0, 0, 0, 8, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 
        110, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 
        0, 0, 0, 0, 26, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 
        8, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 
        14, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 86, 0, 0, 0, 
        3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 
        8, 0, 0, 0, 0, 0, 0, 0, -122, 0, 0, 0, 6, 0, 0, 0, 
        2, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 122, 0, 0, 0, 
        5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 
        8, 0, 0, 0, 0, 0, 0, 0, -74, 0, 0, 0, 7, 0, 0, 0, 
        0, 0, 0, 0, 0 
    };
    
//    private static final byte[] SERIALIZED_AVLTREE_POST_REMOVE = 
//    { 
//        0, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 
//        86, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 
//        0, 0, 0, 0, 26, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 
//        0, 0, 0, 0, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, -122, 
//        0, 0, 0, 5, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 0, 
//        0, 0, 0, 122, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 
//        0, 0, 0, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, -74, 
//        0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0 
//    };

    AvlTree<Integer> tree;
    Comparator<Integer> comparator;
    AvlTreeMarshaller<Integer> treeMarshaller;
    
    static AvlTree<Integer> savedTree;
    
    File treeFile = new File( System.getProperty( "java.io.tmpdir" ) + File.separator + "avl.tree");
    
    private static final Logger LOG = LoggerFactory.getLogger( AvlTreeMarshallerTest.class.getSimpleName() );

    
    @Before
    public void createTree()
    {
        comparator = new Comparator<Integer>() 
        {
            public int compare( Integer i1, Integer i2 )
            {
                return i1.compareTo( i2 );
            }
        };
        
      
        tree = new AvlTree<Integer>( comparator );
        treeMarshaller = new AvlTreeMarshaller<Integer>( comparator, new IntegerKeyMarshaller() );
    }

    
    /**
     * TODO Fix me.
     *
     * @throws IOException
     */
    @Test
    @Ignore ( "Remove operation is deleting more keys than it should." )
    public void testRemoveBug() throws IOException
    {
        Comparator<Long> comparator = new Comparator<Long>() 
        {
            public int compare( Long i1, Long i2 )
            {
                return i1.compareTo( i2 );
            }
        };

        /*
         * This deserializes the state of the AvlTree before the remove 
         * operation and it should work checking to make sure that all
         * the pre-remove keys are present.
         */

        AvlTreeMarshaller<Long> treeMarshaller = new AvlTreeMarshaller<Long>( comparator, new LongMarshaller() );
        AvlTree<Long> tree = treeMarshaller.deserialize( SERIALIZED_AVLTREE_PRE_REMOVE );
        
        for ( long key : AVLTREE_KEYS_PRE_REMOVE )
        {
            assertNotNull( "Should find " + key, tree.find( key ) );
        }
        
        /*
         * Now we remove the key 110 and this should show that we don't have 
         * the expected keys.  We will be missing 134, 2 and 14.
         */
        
        tree.remove( 110L );

        for ( long key : AVLTREE_EXPECTED_KEYS_POST_REMOVE )
        {
            assertNotNull( "Should find " + key, tree.find( key ) );
        }
    }
    

    @Test
    public void testMarshalEmptyTree() throws IOException
    {
        byte[] bites = treeMarshaller.serialize( new AvlTree<Integer>( comparator ) );
{'fraction_non_alphanumeric': 0.1222464558342421, 'fraction_numerical': 0.059541984732824425, 'mean_word_length': 2.2590618336886994, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6350638', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3113, 'n_words': 1104}