/*
 * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package org.openjdk.strings.equals;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jol.info.GraphLayout;
import org.openjdk.strings.Util;

import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(5)
public class EqualsBench {

    @Param({"1", "64", "4096"})
    int size;

    private String cmp1_1;
    private String cmp1_2;
    private String cmp1_3;
    private String cmp2_1;
    private String cmp2_2;
    private String cmp2_3;

    @Setup
    public void setup() {
        long seed = Long.getLong("seed", 1234567890L);

        String base = Util.newString(seed, size - 1, 1.0);
        cmp1_1 = new String(base + (char)0x0042);
        cmp1_2 = new String(base + (char)0x0042);
        char[] array1 = cmp1_2.toCharArray();
        array1[size/2] = (char)((int)array1[size/2]+1);
        cmp1_3 = new String(array1);
        
        cmp2_1 = new String(base + (char)0x4242);
        cmp2_2 = new String(base + (char)0x4242);
        char[] array2 = cmp2_2.toCharArray();
        array2[size/2] = (char)((int)array2[size/2]+1);
        cmp2_3 = new String(array2);
    }

    @Benchmark
    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    public boolean equals_byte() {
        return cmp1_1.equals(cmp1_2);
    }

    @Benchmark
    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    public boolean unequal_byte() {
        return cmp1_1.equals(cmp1_3);
    }

    @Benchmark
    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    public boolean unequal_char() {
        return cmp2_1.equals(cmp2_3);
    }

    @Benchmark
    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    public boolean equals_char() {
        return cmp2_1.equals(cmp2_2);
    }

}
