// Copyright 2010-2018 Google LLC
// 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 com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.IntVar;
import static org.junit.Assert.*;

/** Tests the CP-SAT java interface. */
public class TestSat {
  static {
    System.loadLibrary("jniortools");
  }

  static void testCrashInPresolve() {
    CpModel model = new CpModel();

    // Create decision variables
    IntVar x = model.newIntVar(0, 5, "x");
    IntVar y = model.newIntVar(0, 5, "y");

    // Create a linear constraint which enforces that only x or y can be greater
    // than 0.
    model.addLinearSum(new IntVar[] { x, y }, 0, 1);

    // Create the objective variable
    IntVar obj = model.newIntVar(0, 3, "obj");

    // Cut the domain of the objective variable
    model.addGreaterOrEqual(obj, 2);

    // Set a constraint that makes the problem infeasible
    model.addMaxEquality(obj, new IntVar[] { x, y });

    // Optimize objective
    model.minimize(obj);

    // Create a solver and solve the model.
    CpSolver solver = new CpSolver();
    com.google.ortools.sat.CpSolverStatus status = solver.solve(model);

    assertEquals(status, CpSolverStatus.FEASIBLE);
  }

  public static void main(String[] args) throws Exception {
    testCrashInPresolve();
  }
}
@@ -757,33 +757,24 @@
 ort 
-static org.junit.Assert.*
+java.util.Random
 ;%0A%0A/
@@ -927,16 +927,63 @@
 lve() %7B%0A
+    System.out.println(%22testCrashInPresolve%22);%0A
     CpMo
@@ -1779,52 +1779,1275 @@
     
-assertEquals(status, CpSolverStatus.FEASIBLE
+if (status != CpSolverStatus.INFEASIBLE) %7B%0A      throw new RuntimeException(%22Wrong status in testCrashInPresolve%22);%0A    %7D else %7B%0A      System.out.println(%22  ... test OK%22);%0A    %7D%0A  %7D%0A%0A  private static IntVar%5B%5D entitiesOne;%0A  private static void testCrashInSolveWithAllowedAssignment() %7B%0A    System.out.println(%22testCrashInSolveWithAllowedAssignment%22);%0A    final CpModel model = new CpModel();%0A    final int numEntityOne = 50000;%0A    final int numEntityTwo = 100;%0A    entitiesOne = new IntVar%5BnumEntityOne%5D;%0A    for (int i = 0; i %3C entitiesOne.length; i++) %7B%0A      entitiesOne%5Bi%5D = model.newIntVar(1, numEntityTwo, %22E%22 + i);%0A    %7D%0A    final int%5B%5D%5B%5D allAllowedValues = new int%5BnumEntityTwo%5D%5BentitiesOne.length%5D;%0A    for (int i = 0; i %3C numEntityTwo; i++) %7B%0A      for (int j = 0; j %3C entitiesOne.length; j++) %7B%0A        allAllowedValues%5Bi%5D%5Bj%5D = i;%0A      %7D%0A    %7D%0A    try %7B%0A      model.addAllowedAssignments(entitiesOne, allAllowedValues);%0A    %7D catch (final Exception e) %7B%0A      e.printStackTrace();%0A    %7D%0A    final Random r = new Random();%0A    for (int i = 0; i %3C entitiesOne.length; i++) %7B%0A      model.addEquality(entitiesOne%5Bi%5D, r.nextInt((numEntityTwo)));%0A    %7D%0A    final CpSolver solver = new CpSolver();%0A    solver.solve(model);%0A    System.out.println(%22  ... test OK%22
 );%0A 
@@ -3141,9 +3141,56 @@
 ();%0A
+    testCrashInSolveWithAllowedAssignment();%0A%0A
   %7D%0A
-
 %7D
+%0A

add test for #1015