<commit_before>package io.usethesource.vallang.basic;

import java.net.URISyntaxException;

import io.usethesource.vallang.IDateTime;
import io.usethesource.vallang.IInteger;
import io.usethesource.vallang.INumber;
import io.usethesource.vallang.IReal;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.Setup;
import io.usethesource.vallang.type.TypeFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

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

@RunWith(Parameterized.class)
public final class BasicValueSmokeTest {

  @Parameterized.Parameters
  public static Iterable<? extends Object> data() {
    return Setup.valueFactories();
  }

  private final IValueFactory vf;

  public BasicValueSmokeTest(final IValueFactory vf) {
    this.vf = vf;
  }

  protected TypeFactory tf = TypeFactory.getInstance();

  protected void assertEqual(IValue l, IValue r) {
    assertTrue("Expected " + l + " got " + r, l.isEqual(r));
  }

  @Test
  public void testRationalToReal() {
    assertTrue(vf.rational(1, 4).toReal(3).isEqual(vf.real(0.25)));
  }

  @Test
  public void testStringRepresentation() {
    assertTrue(vf.string("\uD83C\uDF5D").isEqual(vf.string("🍝")));
    assertTrue(vf.string(new String(Character.toChars(0x1F35D))).isEqual(vf.string("🍝")));
  }

  @Test
  public void testStringLength() {
    assertTrue(vf.string("\uD83C\uDF5D").length() == 1);
    assertTrue(vf.string("\uD83C\uDF5D\uD83C\uDF5D").length() == 2);
    assertTrue(vf.string("🍝").length() == 1);
    assertTrue(vf.string("🍝🍝").length() == 2);
    assertTrue(vf.string("é").length() == 1);
    assertTrue(vf.string("").length() == 0);
  }

  @Test
  public void testStringReverse() {
    assertTrue(vf.string("").reverse().isEqual(vf.string("")));
    assertTrue(vf.string("🍝").reverse().isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝🍝").reverse().isEqual(vf.string("🍝🍝")));
    assertTrue(vf.string("🍝x🍝").reverse().isEqual(vf.string("🍝x🍝")));
    assertTrue(vf.string("🍝🍞").reverse().getValue().equals("🍞🍝"));
  }

  @Test
  public void testStringSubString() {
    assertTrue(vf.string("").substring(0, 0).isEqual(vf.string("")));
    assertTrue(vf.string("🍝").substring(0, 1).isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝🍝").substring(0, 1).isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝x🍝").substring(1, 2).isEqual(vf.string("x")));
    assertTrue(vf.string("🍝x🍝").substring(1, 3).isEqual(vf.string("x🍝")));
  }

  @Test
  public void testStringCharAt() {
    assertTrue(vf.string("🍝").charAt(0) == 0x1F35D);
    assertTrue(vf.string("🍝🍞").charAt(1) == 0x1F35E);
    assertTrue(vf.string("🍝x🍝").charAt(1) == 'x');
    assertTrue(vf.string("🍝x🍞").charAt(2) == 0x1F35E);
  }

  @Test
  public void testStringConcat() {
    assertTrue(vf.string("").concat(vf.string("")).isEqual(vf.string("")));
    assertTrue(vf.string("x").concat(vf.string("y")).isEqual(vf.string("xy")));
    assertTrue(vf.string("🍝").concat(vf.string("y")).isEqual(vf.string("🍝y")));
    assertTrue(vf.string("x").concat(vf.string("🍝")).isEqual(vf.string("x🍝")));
    assertTrue(vf.string("🍝").concat(vf.string("🍝")).isEqual(vf.string("🍝🍝")));
  }

  @Test
  public void testStringReplace() {
    assertTrue(vf.string("").replace(0, 1, 0, vf.string("x")).isEqual(vf.string("x")));
    assertTrue(vf.string("x").replace(0, 1, 0, vf.string("")).isEqual(vf.string("x")));
    assertTrue(vf.string("xy").replace(0, 1, 1, vf.string("p")).isEqual(vf.string("py")));
    assertTrue(vf.string("xy").replace(1, 1, 0, vf.string("p")).isEqual(vf.string("xp")));
    assertTrue(vf.string("xy").replace(0, 1, 1, vf.string("pq")).isEqual(vf.string("pqy")));
    assertTrue(vf.string("xy").replace(1, 1, 0, vf.string("pq")).isEqual(vf.string("xqp")));
    assertTrue(vf.string("xy").replace(0, 1, 0, vf.string("pq")).isEqual(vf.string("pqxy")));
    assertTrue(vf.string("xy").replace(1, 1, 1, vf.string("pq")).isEqual(vf.string("xpqy")));

    assertTrue(vf.string("🍝y").replace(0, 1, 1, vf.string("p")).isEqual(vf.string("py")));
    assertTrue(vf.string("🍝y").replace(1, 1, 0, vf.string("p")).isEqual(vf.string("🍝p")));
    assertTrue(vf.string("xy").replace(0, 1, 1, vf.string("🍝")).isEqual(vf.string("🍝y")));
    assertTrue(vf.string("").replace(0, 1, 0, vf.string("🍝")).isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝").replace(0, 1, 0, vf.string("")).isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝y").replace(0, 1, 1, vf.string("p")).isEqual(vf.string("py")));
    assertTrue(vf.string("🍝y").replace(1, 1, 0, vf.string("p")).isEqual(vf.string("🍝p")));
    assertTrue(vf.string("x🍝").replace(0, 1, 1, vf.string("p")).isEqual(vf.string("p🍝")));
    assertTrue(vf.string("x🍝").replace(1, 1, 0, vf.string("p")).isEqual(vf.string("xp")));
    assertTrue(vf.string("🍝y").replace(0, 1, 1, vf.string("p🍝")).isEqual(vf.string("p🍝y")));
    assertTrue(vf.string("🍝y").replace(1, 1, 0, vf.string("p🍝")).isEqual(vf.string("🍝🍝p")));
    assertTrue(vf.string("🍝y").replace(0, 1, 0, vf.string("🍝q")).isEqual(vf.string("🍝q🍝y")));
    assertTrue(vf.string("x🍝").replace(1, 1, 1, vf.string("🍝q")).isEqual(vf.string("x🍝q🍝")));
    assertTrue(vf.string("🍝y🍝").replace(1, 1, 2, vf.string("🍝")).isEqual(vf.string("🍝🍝🍝")));
  }

  @Test
  public void testIntAddition() {
    assertTrue(vf.integer(1).add(vf.integer(1)).isEqual(vf.integer(2)));
  }

  @Test
  public void testReal() {
    assertTrue(vf.real("1.5").floor().isEqual(vf.real("1")));
    assertTrue(vf.real("1.5").round().isEqual(vf.real("2")));
  }

  @Test
  public void testNumberSubTypes() {
    assertTrue(tf.integerType().isSubtypeOf(tf.numberType()));
    assertFalse(tf.numberType().isSubtypeOf(tf.integerType()));
    assertTrue(tf.realType().isSubtypeOf(tf.numberType()));
    assertFalse(tf.numberType().isSubtypeOf(tf.realType()));
    assertTrue(tf.rationalType().isSubtypeOf(tf.numberType()));
    assertFalse(tf.numberType().isSubtypeOf(tf.rationalType()));

    assertTrue(tf.integerType().lub(tf.realType()).equivalent(tf.numberType()));
    assertTrue(tf.integerType().lub(tf.rationalType()).equivalent(tf.numberType()));
    assertTrue(tf.integerType().lub(tf.numberType()).equivalent(tf.numberType()));
    assertTrue(tf.realType().lub(tf.numberType()).equivalent(tf.numberType()));
    assertTrue(tf.rationalType().lub(tf.integerType()).equivalent(tf.numberType()));
    assertTrue(tf.rationalType().lub(tf.realType()).equivalent(tf.numberType()));
    assertTrue(tf.rationalType().lub(tf.numberType()).equivalent(tf.numberType()));
  }

  @Test<commit_msg>Added random tests around the indent feature<commit_after>package io.usethesource.vallang.basic;

import java.net.URISyntaxException;
import java.util.Random;

import io.usethesource.vallang.IDateTime;
import io.usethesource.vallang.IInteger;
import io.usethesource.vallang.INumber;
import io.usethesource.vallang.IReal;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.Setup;
import io.usethesource.vallang.random.util.RandomUtil;
import io.usethesource.vallang.type.TypeFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

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

@RunWith(Parameterized.class)
public final class BasicValueSmokeTest {

  @Parameterized.Parameters
  public static Iterable<? extends Object> data() {
    return Setup.valueFactories();
  }

  private final IValueFactory vf;

  public BasicValueSmokeTest(final IValueFactory vf) {
    this.vf = vf;
  }

  protected TypeFactory tf = TypeFactory.getInstance();

  protected void assertEqual(IValue l, IValue r) {
    assertTrue("Expected " + l + " got " + r, l.isEqual(r));
  }

  @Test
  public void testRationalToReal() {
    assertTrue(vf.rational(1, 4).toReal(3).isEqual(vf.real(0.25)));
  }

  @Test
  public void testStringRepresentation() {
    assertTrue(vf.string("\uD83C\uDF5D").isEqual(vf.string("🍝")));
    assertTrue(vf.string(new String(Character.toChars(0x1F35D))).isEqual(vf.string("🍝")));
  }

  @Test
  public void testStringLength() {
    assertTrue(vf.string("\uD83C\uDF5D").length() == 1);
    assertTrue(vf.string("\uD83C\uDF5D\uD83C\uDF5D").length() == 2);
    assertTrue(vf.string("🍝").length() == 1);
    assertTrue(vf.string("🍝🍝").length() == 2);
    assertTrue(vf.string("é").length() == 1);
    assertTrue(vf.string("").length() == 0);
  }

  @Test
  public void testStringReverse() {
    assertTrue(vf.string("").reverse().isEqual(vf.string("")));
    assertTrue(vf.string("🍝").reverse().isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝🍝").reverse().isEqual(vf.string("🍝🍝")));
    assertTrue(vf.string("🍝x🍝").reverse().isEqual(vf.string("🍝x🍝")));
    assertTrue(vf.string("🍝🍞").reverse().getValue().equals("🍞🍝"));
  }

  @Test
  public void testStringSubString() {
    assertTrue(vf.string("").substring(0, 0).isEqual(vf.string("")));
    assertTrue(vf.string("🍝").substring(0, 1).isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝🍝").substring(0, 1).isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝x🍝").substring(1, 2).isEqual(vf.string("x")));
    assertTrue(vf.string("🍝x🍝").substring(1, 3).isEqual(vf.string("x🍝")));
  }

  @Test
  public void testStringCharAt() {
    assertTrue(vf.string("🍝").charAt(0) == 0x1F35D);
    assertTrue(vf.string("🍝🍞").charAt(1) == 0x1F35E);
    assertTrue(vf.string("🍝x🍝").charAt(1) == 'x');
    assertTrue(vf.string("🍝x🍞").charAt(2) == 0x1F35E);
  }

  @Test
  public void testStringConcat() {
    assertTrue(vf.string("").concat(vf.string("")).isEqual(vf.string("")));
    assertTrue(vf.string("x").concat(vf.string("y")).isEqual(vf.string("xy")));
    assertTrue(vf.string("🍝").concat(vf.string("y")).isEqual(vf.string("🍝y")));
    assertTrue(vf.string("x").concat(vf.string("🍝")).isEqual(vf.string("x🍝")));
    assertTrue(vf.string("🍝").concat(vf.string("🍝")).isEqual(vf.string("🍝🍝")));
  }

  @Test
  public void testStringReplace() {
    assertTrue(vf.string("").replace(0, 1, 0, vf.string("x")).isEqual(vf.string("x")));
    assertTrue(vf.string("x").replace(0, 1, 0, vf.string("")).isEqual(vf.string("x")));
    assertTrue(vf.string("xy").replace(0, 1, 1, vf.string("p")).isEqual(vf.string("py")));
    assertTrue(vf.string("xy").replace(1, 1, 0, vf.string("p")).isEqual(vf.string("xp")));
    assertTrue(vf.string("xy").replace(0, 1, 1, vf.string("pq")).isEqual(vf.string("pqy")));
    assertTrue(vf.string("xy").replace(1, 1, 0, vf.string("pq")).isEqual(vf.string("xqp")));
    assertTrue(vf.string("xy").replace(0, 1, 0, vf.string("pq")).isEqual(vf.string("pqxy")));
    assertTrue(vf.string("xy").replace(1, 1, 1, vf.string("pq")).isEqual(vf.string("xpqy")));

    assertTrue(vf.string("🍝y").replace(0, 1, 1, vf.string("p")).isEqual(vf.string("py")));
    assertTrue(vf.string("🍝y").replace(1, 1, 0, vf.string("p")).isEqual(vf.string("🍝p")));
    assertTrue(vf.string("xy").replace(0, 1, 1, vf.string("🍝")).isEqual(vf.string("🍝y")));
    assertTrue(vf.string("").replace(0, 1, 0, vf.string("🍝")).isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝").replace(0, 1, 0, vf.string("")).isEqual(vf.string("🍝")));
    assertTrue(vf.string("🍝y").replace(0, 1, 1, vf.string("p")).isEqual(vf.string("py")));
    assertTrue(vf.string("🍝y").replace(1, 1, 0, vf.string("p")).isEqual(vf.string("🍝p")));
    assertTrue(vf.string("x🍝").replace(0, 1, 1, vf.string("p")).isEqual(vf.string("p🍝")));
    assertTrue(vf.string("x🍝").replace(1, 1, 0, vf.string("p")).isEqual(vf.string("xp")));
    assertTrue(vf.string("🍝y").replace(0, 1, 1, vf.string("p🍝")).isEqual(vf.string("p🍝y")));
    assertTrue(vf.string("🍝y").replace(1, 1, 0, vf.string("p🍝")).isEqual(vf.string("🍝🍝p")));
    assertTrue(vf.string("🍝y").replace(0, 1, 0, vf.string("🍝q")).isEqual(vf.string("🍝q🍝y")));
    assertTrue(vf.string("x🍝").replace(1, 1, 1, vf.string("🍝q")).isEqual(vf.string("x🍝q🍝")));
    assertTrue(vf.string("🍝y🍝").replace(1, 1, 2, vf.string("🍝")).isEqual(vf.string("🍝🍝🍝")));
  }
  
  private static final String[] commonNewlines = new String[] { "\r\n", "\n"};
  
  
  private void checkIndent(String indent, String newline, String... lines) {
	  StringBuilder unindented = new StringBuilder();
	  StringBuilder indented = new StringBuilder();
	  IString concatTree = vf.string("");
	  for (String l : lines) {
		  unindented.append(l);
		  unindented.append(newline);

		  concatTree = concatTree.concat(vf.string(l));
		  concatTree = concatTree.concat(vf.string(newline));
		  
		  indented.append(indent);
		  indented.append(newline);
		  indented.append(newline);
	  }
	  
	  String expected = indented.toString();
	  
	  IString indentedDirect = vf.string(unindented.toString()).indent(vf.string(indent));
	  IString indentedConcatTree = concatTree.indent(vf.string(indent));

	  assertEquals(expected, indentedDirect.toString());
	  assertEquals(expected, indentedConcatTree.toString());
	  assertEqual(indentedDirect, indentedConcatTree);
	  assertEquals(indentedDirect.hashCode(), indentedConcatTree.hashCode());
  }
  
  

  public void testStringIndent() {
	  Random rnd = new Random();
	  for (String nl: commonNewlines) {
		  checkIndent(" ", nl, "a", "b", "c");
		  checkIndent("\t", nl, "a", "b", "c");
		  checkIndent("\t", nl, "a", "", "c");
		  checkIndent("   ", nl, "a", "b", "c");
		  checkIndent(" ", nl, " abcdef", " bcdefg", " cdefgh");
		  checkIndent(" ", nl, "🍝", " b", " c");
		  
		  String[] randomLines = new String[10];
		  for (int n = 0; n < randomLines.length; n++) {
		     String newString = RandomUtil.string(rnd, rnd.nextInt(2000));
		     for (String newL : commonNewlines) {
		    	 newString = newString.replaceAll(newL, "_");
		     }
		     randomLines[n] = newString;
		  }
		  
		  checkIndent("  ", nl, randomLines);
	  }
  }

  @Test
  public void testIntAddition() {
    assertTrue(vf.integer(1).add(vf.integer(1)).isEqual(vf.integer(2)));
  }

  @Test
  public void testReal() {
    assertTrue(vf.real("1.5").floor().isEqual(vf.real("1")));
    assertTrue(vf.real("1.5").round().isEqual(vf.real("2")));
  }

  @Test
  public void testNumberSubTypes() {
    assertTrue(tf.integerType().isSubtypeOf(tf.numberType()));
    assertFalse(tf.numberType().isSubtypeOf(tf.integerType()));
    assertTrue(tf.realType().isSubtypeOf(tf.numberType()));
    assertFalse(tf.numberType().isSubtypeOf(tf.realType()));
    assertTrue(tf.rationalType().isSubtypeOf(tf.numberType()));
    assertFalse(tf.numberType().isSubtypeOf(tf.rationalType()));

    assertTrue(tf.integerType().lub(tf.realType()).equivalent(tf.numberType()));
    assertTrue(tf.integerType().lub(tf.rationalType()).equivalent(tf.numberType()));
    assertTrue(tf.integerType().lub(tf.numberType()).equivalent(tf.numberType()));
    assertTrue(tf.realType().lub(tf.numberType()).equivalent(tf.numberType()));
    assertTrue(tf.rationalType().lub(tf.integerType()).equivalent(tf.numberType()));
    assertTrue(tf.rationalType().lub(tf.realType()).equivalent(tf.numberType()));
    assertTrue(tf.rationalType().lub(tf.numberType()).equivalent(tf.numberType()));
  }

  @Test
{'fraction_non_alphanumeric': 0.22863247863247863, 'fraction_numerical': 0.017158767158767158, 'mean_word_length': 5.163208300079808, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3316667', 'n_tokens_mistral': 6224, 'n_tokens_neox': 5983, 'n_words': 675}