<commit_before>package org.drools.base;

/*
 * Copyright 2005 JBoss Inc
 *
 * Licensed 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.
 */

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.io.ObjectOutput;
import java.io.IOException;
import java.io.ObjectInput;

import junit.framework.TestCase;

import org.drools.base.evaluators.EvaluatorRegistry;
import org.drools.common.InternalWorkingMemory;
import org.drools.rule.Declaration;
import org.drools.rule.VariableRestriction.BooleanVariableContextEntry;
import org.drools.rule.VariableRestriction.CharVariableContextEntry;
import org.drools.rule.VariableRestriction.DoubleVariableContextEntry;
import org.drools.rule.VariableRestriction.LongVariableContextEntry;
import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
import org.drools.rule.VariableRestriction.VariableContextEntry;
import org.drools.spi.Evaluator;
import org.drools.spi.Extractor;
import org.drools.spi.FieldExtractor;
import org.drools.spi.FieldValue;

/**
 * Some test coverage goodness for the evaluators.
 * Evaluator concrete instances are inside the factory at this time.
 * @author <NAME>
 */
public class EvaluatorFactoryTest extends TestCase {

    private EvaluatorRegistry registry = new EvaluatorRegistry();

    public void testObject() {

        final List list = new ArrayList();
        list.add( "foo" );
        list.add( null );

        Collection col = Arrays.asList( new String[] { "foo", "bar", null} );

        final Object[][] data = {
                                 {"foo", "==", "bar", Boolean.FALSE},
                                 {"foo", "==", "foo", Boolean.TRUE},
                                 {"foo", "!=", "bar", Boolean.TRUE},
                                 {list, "contains", "foo", Boolean.TRUE},
                                 {list, "contains", "bar", Boolean.FALSE},
                                 {list, "contains", null, Boolean.TRUE},
                                 {null, "contains", "bar", Boolean.FALSE},
                                 {null, "contains", null, Boolean.FALSE},
                                 {list, "==", null, Boolean.FALSE},
                                 {list, "!=", null, Boolean.TRUE},
                                 {null, "==", null, Boolean.TRUE},
                                 {null, "==", list, Boolean.FALSE},
                                 {null, "!=", list, Boolean.TRUE},
                                 {null, "<", new Integer( 43 ), Boolean.FALSE},
                                 {null, ">=", new Integer( -10 ), Boolean.FALSE},
                                 {null, ">", new Integer( -10 ), Boolean.FALSE},
                                 {null, "<=", new Integer( 42 ), Boolean.FALSE},
                                 {new BigDecimal( "42.42" ), "<", new BigDecimal( "43" ), Boolean.TRUE},
                                 {new BigDecimal( "42.42" ), ">", new BigDecimal( "43" ), Boolean.FALSE},
                                 {new BigDecimal( "42.42" ), "<=", new BigDecimal( "42.42" ), Boolean.TRUE},
                                 {new BigInteger( "42" ), ">=", new BigInteger( "43" ), Boolean.FALSE},
                                 {new BigInteger( "42" ), ">=", new BigInteger( "43" ), Boolean.FALSE},
                                 {list, "excludes", "baz", Boolean.TRUE},
                                 {list, "excludes", "foo", Boolean.FALSE},
                                 {"foo", "memberOf", col, Boolean.TRUE},
                                 {"xyz", "memberOf", col, Boolean.FALSE},
                                 {null, "memberOf", col, Boolean.TRUE},
                                 {"foo", "memberOf", null, Boolean.FALSE},
                                 {"foo", "not memberOf", col, Boolean.FALSE},
                                 {"xyz", "not memberOf", col, Boolean.TRUE},
                                 {null, "not memberOf", col, Boolean.FALSE},
                                 {"foo", "not memberOf", null, Boolean.FALSE},
                                 {Boolean.TRUE, "==", "xyz", Boolean.FALSE},
                                 {Boolean.TRUE, "==", "true", Boolean.TRUE},
                                 {Boolean.FALSE, "==", "xyz", Boolean.TRUE},
                                 {Boolean.FALSE, "==", "false", Boolean.TRUE},
                                 {Boolean.FALSE, "==", "true", Boolean.FALSE},
                                 {Boolean.TRUE, "!=", "xyz", Boolean.TRUE},
                                 {Boolean.TRUE, "!=", "true", Boolean.FALSE},
                                 {Boolean.FALSE, "!=", "xyz", Boolean.FALSE},
                                 {Boolean.FALSE, "!=", "true", Boolean.TRUE},
                                 {Boolean.FALSE, "!=", "false", Boolean.FALSE}

        };

        runEvaluatorTest( data,
                          ValueType.OBJECT_TYPE );

    }

    public void testArrayType() {

        final Object[] field = new Object[]{"foo", "bar"};

        final Object[][] data = {{field, "==", new Object[]{"foo"}, Boolean.FALSE},
                                 {field, "==", field, Boolean.TRUE},
                                 {field, "!=", new Object[]{"foo"}, Boolean.TRUE},
                                 {field, "contains", "foo", Boolean.TRUE},
                                 {field, "contains", "xyz", Boolean.FALSE},
                                 {field, "contains", null, Boolean.FALSE},
                                 {null, "contains", "foo", Boolean.FALSE},
                                 {field, "excludes", "foo", Boolean.FALSE},
                                 {field, "excludes", "xyz", Boolean.TRUE},
                                 {field, "excludes", null, Boolean.TRUE},
                                 {null, "excludes", "foo", Boolean.TRUE},
                                 {field, "!=", null, Boolean.TRUE},
                                 {field, "==", null, Boolean.FALSE},
                                 {null, "==", field, Boolean.FALSE},
                                 {null, "==", null, Boolean.TRUE},
                                 {null, "!=", field, Boolean.TRUE},
                                 {null, "!=", null, Boolean.FALSE},
                                 {"foo", "memberOf", field, Boolean.TRUE},
                                 {"xyz", "memberOf", field, Boolean.FALSE},
                                 {null, "memberOf", field, Boolean.FALSE},
                                 {"foo", "memberOf", null, Boolean.FALSE},
                                 {"foo", "not memberOf", field, Boolean.FALSE},
                                 {"xyz", "not memberOf", field, Boolean.TRUE},
                                 {null, "not memberOf", field, Boolean.TRUE},
                                 {"foo", "not memberOf", null, Boolean.TRUE}
                                 };

        runEvaluatorTest( data,
                          ValueType.ARRAY_TYPE );

    }

    public void testString() {

        Collection col = Arrays.asList( new String[] { "foo", "bar", null} );

        final Object[][] data = {{"foo", "==", "bar", Boolean.FALSE},
                                 {"foo", "==", "foo", Boolean.TRUE},
                                 {"foo", "!=", "bar", Boolean.TRUE},
                                 {"something foo", "matches", ".*foo", Boolean.TRUE},
                                 {"foo", "matches", ".*foo", Boolean.TRUE},
                                 {"foo", "matches", "bar", Boolean.FALSE},
                                 {null, "matches", ".*foo", Boolean.FALSE},
                                 {"foo", "==", null, Boolean.FALSE},
                                 {"foo", "!=", null, Boolean.TRUE},
                                 {null, "==", null, Boolean.TRUE},
                                 {"foo", "!=", null, Boolean.TRUE},
                                 {null, "!=", "foo", Boolean.TRUE},
                                 {null, "!=", null, Boolean.FALSE},
                                 {"foo", "memberOf", col, Boolean.TRUE},<commit_msg>JBRULES-1095 fix bad merge - fixed the lost handling of BigDecimal/BigInteger<commit_after>package org.drools.base;

/*
 * Copyright 2005 JBoss Inc
 *
 * Licensed 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.
 */

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.io.ObjectOutput;
import java.io.IOException;
import java.io.ObjectInput;

import junit.framework.TestCase;

import org.drools.base.evaluators.EvaluatorRegistry;
import org.drools.common.InternalWorkingMemory;
import org.drools.rule.Declaration;
import org.drools.rule.VariableRestriction.BooleanVariableContextEntry;
import org.drools.rule.VariableRestriction.CharVariableContextEntry;
import org.drools.rule.VariableRestriction.DoubleVariableContextEntry;
import org.drools.rule.VariableRestriction.LongVariableContextEntry;
import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
import org.drools.rule.VariableRestriction.VariableContextEntry;
import org.drools.spi.Evaluator;
import org.drools.spi.Extractor;
import org.drools.spi.FieldExtractor;
import org.drools.spi.FieldValue;

/**
 * Some test coverage goodness for the evaluators.
 * Evaluator concrete instances are inside the factory at this time.
 * @author <NAME>
 */
public class EvaluatorFactoryTest extends TestCase {

    private EvaluatorRegistry registry = new EvaluatorRegistry();

    public void testObject() {

        final List list = new ArrayList();
        list.add( "foo" );
        list.add( null );

        Collection col = Arrays.asList( new String[] { "foo", "bar", null} );

        final Object[][] data = {
                                 {"foo", "==", "bar", Boolean.FALSE},
                                 {"foo", "==", "foo", Boolean.TRUE},
                                 {"foo", "!=", "bar", Boolean.TRUE},
                                 {list, "contains", "foo", Boolean.TRUE},
                                 {list, "contains", "bar", Boolean.FALSE},
                                 {list, "contains", null, Boolean.TRUE},
                                 {null, "contains", "bar", Boolean.FALSE},
                                 {null, "contains", null, Boolean.FALSE},
                                 {list, "==", null, Boolean.FALSE},
                                 {list, "!=", null, Boolean.TRUE},
                                 {null, "==", null, Boolean.TRUE},
                                 {null, "==", list, Boolean.FALSE},
                                 {null, "!=", list, Boolean.TRUE},
                                 {null, "<", new Integer( 43 ), Boolean.FALSE},
                                 {null, ">=", new Integer( -10 ), Boolean.FALSE},
                                 {null, ">", new Integer( -10 ), Boolean.FALSE},
                                 {null, "<=", new Integer( 42 ), Boolean.FALSE},
                                 {new BigDecimal( "42.42" ), "<", new BigDecimal( "43" ), Boolean.TRUE},
                                 {new BigDecimal( "42.42" ), ">", new BigDecimal( "43" ), Boolean.FALSE},
                                 {new BigDecimal( "42.42" ), "<=", new BigDecimal( "42.42" ), Boolean.TRUE},
                                 {new BigInteger( "42" ), ">=", new BigInteger( "43" ), Boolean.FALSE},
                                 {new BigInteger( "42" ), ">=", new BigInteger( "43" ), Boolean.FALSE},
                                 {list, "excludes", "baz", Boolean.TRUE},
                                 {list, "excludes", "foo", Boolean.FALSE},
                                 {"foo", "memberOf", col, Boolean.TRUE},
                                 {"xyz", "memberOf", col, Boolean.FALSE},
                                 {null, "memberOf", col, Boolean.TRUE},
                                 {"foo", "memberOf", null, Boolean.FALSE},
                                 {"foo", "not memberOf", col, Boolean.FALSE},
                                 {"xyz", "not memberOf", col, Boolean.TRUE},
                                 {null, "not memberOf", col, Boolean.FALSE},
                                 {"foo", "not memberOf", null, Boolean.FALSE},
                                 {Boolean.TRUE, "==", "xyz", Boolean.FALSE},
                                 {Boolean.TRUE, "==", "true", Boolean.TRUE},
                                 {Boolean.FALSE, "==", "xyz", Boolean.TRUE},
                                 {Boolean.FALSE, "==", "false", Boolean.TRUE},
                                 {Boolean.FALSE, "==", "true", Boolean.FALSE},
                                 {Boolean.TRUE, "!=", "xyz", Boolean.TRUE},
                                 {Boolean.TRUE, "!=", "true", Boolean.FALSE},
                                 {Boolean.FALSE, "!=", "xyz", Boolean.FALSE},
                                 {Boolean.FALSE, "!=", "true", Boolean.TRUE},
                                 {Boolean.FALSE, "!=", "false", Boolean.FALSE}

        };

        runEvaluatorTest( data,
                          ValueType.OBJECT_TYPE );

    }

    public void testArrayType() {

        final Object[] field = new Object[]{"foo", "bar"};

        final Object[][] data = {{field, "==", new Object[]{"foo"}, Boolean.FALSE},
                                 {field, "==", field, Boolean.TRUE},
                                 {field, "!=", new Object[]{"foo"}, Boolean.TRUE},
                                 {field, "contains", "foo", Boolean.TRUE},
                                 {field, "contains", "xyz", Boolean.FALSE},
                                 {field, "contains", null, Boolean.FALSE},
                                 {null, "contains", "foo", Boolean.FALSE},
                                 {field, "excludes", "foo", Boolean.FALSE},
                                 {field, "excludes", "xyz", Boolean.TRUE},
                                 {field, "excludes", null, Boolean.TRUE},
                                 {null, "excludes", "foo", Boolean.TRUE},
                                 {field, "!=", null, Boolean.TRUE},
                                 {field, "==", null, Boolean.FALSE},
                                 {null, "==", field, Boolean.FALSE},
                                 {null, "==", null, Boolean.TRUE},
                                 {null, "!=", field, Boolean.TRUE},
                                 {null, "!=", null, Boolean.FALSE},
                                 {"foo", "memberOf", field, Boolean.TRUE},
                                 {"xyz", "memberOf", field, Boolean.FALSE},
                                 {null, "memberOf", field, Boolean.FALSE},
                                 {"foo", "memberOf", null, Boolean.FALSE},
                                 {"foo", "not memberOf", field, Boolean.FALSE},
                                 {"xyz", "not memberOf", field, Boolean.TRUE},
                                 {null, "not memberOf", field, Boolean.TRUE},
                                 {"foo", "not memberOf", null, Boolean.TRUE}
                                 };

        runEvaluatorTest( data,
                          ValueType.ARRAY_TYPE );

    }

    public void testString() {

        Collection col = Arrays.asList( new String[] { "foo", "bar", null} );

        final Object[][] data = {{"foo", "==", "bar", Boolean.FALSE},
                                 {"foo", "==", "foo", Boolean.TRUE},
                                 {"foo", "!=", "bar", Boolean.TRUE},
                                 {"something foo", "matches", ".*foo", Boolean.TRUE},
                                 {"foo", "matches", ".*foo", Boolean.TRUE},
                                 {"foo", "matches", "bar", Boolean.FALSE},
                                 {null, "matches", ".*foo", Boolean.FALSE},
                                 {"something", "matches", "something", Boolean.TRUE},
                                 {"something", "matches", "hello ;=", Boolean.FALSE},
                                 {"something", "not matches", "something", Boolean.FALSE},
                                 {"something", "not matches", "hello ;=", Boolean.TRUE},
                                 {"foo", "==", null, Boolean.FALSE},
                                 {"foo", "!=", null, Boolean.TRUE},
                                 {null, "==", null, Boolean.TRUE},
                                 {"foo", "!=", null, Boolean.TRUE},
                                 {null, "!=", "foo", Boolean.TRUE},
                                 {null, "!=", null, Boolean.FALSE},
                                 {"foo", "memberOf", col, Boolean.TRUE},
{'fraction_non_alphanumeric': 0.14417448923246826, 'fraction_numerical': 0.005080066261733848, 'mean_word_length': 1.3924702774108322, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 162, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5712168', 'n_tokens_mistral': 4473, 'n_tokens_neox': 4376, 'n_words': 1106}