<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.pdfbox.pdmodel.common.function.type4;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
 * Tests the type 4 function parser.
 *
 */
public class TestParser
{

    /**
     * Test the very basics.
     * @throws Exception if an error occurs
     */
    @Test
    public void testParserBasics() throws Exception
    {
        Type4Tester.create("3 4 add 2 sub").pop(5).isEmpty();
    }

    /**
     * Test nested blocks.
     * @throws Exception if an error occurs
     */
    @Test
    public void testNested() throws Exception
    {
        Type4Tester.create("true { 2 1 add } { 2 1 sub } ifelse")
            .pop(3).isEmpty();

        Type4Tester.create("{ true }").pop(true).isEmpty();
    }

    /**
     * Tests parsing of real values.
     * @throws Exception if an error occurs
     */
    @Test
    public void testParseFloat() throws Exception
    {
        assertEquals(0, InstructionSequenceBuilder.parseReal("0"), 0.00001f);
        assertEquals(1, InstructionSequenceBuilder.parseReal("1"), 0.00001f);
        assertEquals(1, InstructionSequenceBuilder.parseReal("+1"), 0.00001f);
        assertEquals(-1, InstructionSequenceBuilder.parseReal("-1"), 0.00001f);
        assertEquals(3.14157, InstructionSequenceBuilder.parseReal("3.14157"), 0.00001f);
        assertEquals(-1.2, InstructionSequenceBuilder.parseReal("-1.2"), 0.00001f);

        assertEquals(1.0E-5, InstructionSequenceBuilder.parseReal("1.0E-5"), 0.00001f);
    }

    /**
     * Tests problematic functions from PDFBOX-804.
     * @throws Exception if an error occurs
     */
    @Test
    public void testJira804() throws Exception
    {
        //This is an example of a tint to CMYK function<commit_msg>PDFBOX-5017: Sonar fixes; change visibility<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.pdfbox.pdmodel.common.function.type4;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
 * Tests the type 4 function parser.
 *
 */
class TestParser
{

    /**
     * Test the very basics.
     * @throws Exception if an error occurs
     */
    @Test
    void testParserBasics() throws Exception
    {
        Type4Tester.create("3 4 add 2 sub").pop(5).isEmpty();
    }

    /**
     * Test nested blocks.
     * @throws Exception if an error occurs
     */
    @Test
    void testNested() throws Exception
    {
        Type4Tester.create("true { 2 1 add } { 2 1 sub } ifelse")
            .pop(3).isEmpty();

        Type4Tester.create("{ true }").pop(true).isEmpty();
    }

    /**
     * Tests parsing of real values.
     * @throws Exception if an error occurs
     */
    @Test
    void testParseFloat() throws Exception
    {
        assertEquals(0, InstructionSequenceBuilder.parseReal("0"), 0.00001f);
        assertEquals(1, InstructionSequenceBuilder.parseReal("1"), 0.00001f);
        assertEquals(1, InstructionSequenceBuilder.parseReal("+1"), 0.00001f);
        assertEquals(-1, InstructionSequenceBuilder.parseReal("-1"), 0.00001f);
        assertEquals(3.14157, InstructionSequenceBuilder.parseReal("3.14157"), 0.00001f);
        assertEquals(-1.2, InstructionSequenceBuilder.parseReal("-1.2"), 0.00001f);

        assertEquals(1.0E-5, InstructionSequenceBuilder.parseReal("1.0E-5"), 0.00001f);
    }

    /**
     * Tests problematic functions from PDFBOX-804.
     * @throws Exception if an error occurs
     */
    @Test
    void testJira804() throws Exception
    {
        //This is an example of a tint to CMYK function
{'fraction_non_alphanumeric': 0.10382301571899864, 'fraction_numerical': 0.03803609547836212, 'mean_word_length': 3.010894941634241, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5567140', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1390, 'n_words': 482}