/*
 * Copyright 2019 The GraphicsFuzz Project Authors
 *
 * 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
 *
 *     https://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 com.graphicsfuzz.reducer.util;

import com.graphicsfuzz.common.ast.TranslationUnit;
import com.graphicsfuzz.common.util.CompareAsts;
import com.graphicsfuzz.common.util.ParseHelper;
import org.junit.Ignore;
import org.junit.Test;

public class SimplifyTest {

  // TODO(110) - this test fails due to issue 110, and should be enabled once that issue is fixed.
  @Ignore
  @Test
  public void testIfParenthesesRemoved() throws Exception {
    final TranslationUnit tu = ParseHelper.parse("void main() {"
        + "  if (_GLF_DEAD(_GLF_FALSE(false, false))) {"
        + "  }"
        + "}");
    final String expected = "void main() {"
        + "  if (false) {"
        + "  }"
        + "}";
    final TranslationUnit simplifiedTu = Simplify.simplify(tu);
    CompareAsts.assertEqualAsts(expected, simplifiedTu);
  }

  @Ignore
  @Test
  public void testWhileParenthesesRemoved() throws Exception {
    final TranslationUnit tu = ParseHelper.parse("void main() {"
        + "  while (_GLF_DEAD(_GLF_FALSE(false, false))) {"
        + "  }"
        + "}");
    final String expected = "void main() {"
        + "  while (false) {"
        + "  }"
        + "}";
    final TranslationUnit simplifiedTu = Simplify.simplify(tu);
    CompareAsts.assertEqualAsts(expected, simplifiedTu);
  }

  @Ignore
  @Test
  public void testForParenthesesRemoved() throws Exception {
    final TranslationUnit tu = ParseHelper.parse("void main() {"
        + " for ("
        + "   int i = _GLF_ONE(1, _GLF_IDENTITY(1, 1));"
        + "   i > _GLF_IDENTITY(1, _GLF_FUZZED(1));"
        + "   i++)"
        + "   { }"
        + " }"
        + "}");
    final String expected = "void main() {"
        + " for (int i = 1; i > 1; i++)"
        + "   { }"
        + "}";
    final TranslationUnit simplifiedTu = Simplify.simplify(tu);
    CompareAsts.assertEqualAsts(expected, simplifiedTu);
  }

  @Ignore
  @Test
  public void testSwitchParenthesesRemoved() throws Exception {
    final TranslationUnit tu = ParseHelper.parse("void main() {"
        + " switch(_GLF_ZERO(0, _GLF_IDENTITY(0, 0)))"
        + "   {"
        + "   }"
        + "}");
    final String expected = "void main() {"
        + " switch(0)"
        + "   {"
        + "   }"
        + "}";
    final TranslationUnit simplifiedTu = Simplify.simplify(tu);
    CompareAsts.assertEqualAsts(expected, simplifiedTu);
  }

  @Ignore
  @Test
  public void testDoWhileParenthesesRemoved() throws Exception {
    final TranslationUnit tu = ParseHelper.parse("void main() {"
        + " do { } while (_GLF_DEAD(_GLF_FALSE(false, false)));"
        + "}");
    final String expected = "void main() {"
        + " do { } while (false);"
        + "}";
    final TranslationUnit simplifiedTu = Simplify.simplify(tu);
    CompareAsts.assertEqualAsts(expected, simplifiedTu);
  }

  @Ignore
  @Test
  public void testMacroBlockRemoved() throws Exception {
    final TranslationUnit tu = ParseHelper.parse("void main() {"
        + " _GLF_FUZZED(1);"
        + " _GLF_FUZZED(_GLF_IDENTITY(1, 1));"
        + " int a = _GLF_IDENTITY(_GLF_FUZZED(1), 1);"
        + "}"
    );
    final String expected = "void() {"
        + " 1;"
        + " 1;"
        + " int a = 1;"
        + "}";
    final TranslationUnit simplifiedTu = Simplify.simplify(tu);
    CompareAsts.assertEqualAsts(expected, simplifiedTu);
  }

}

@@ -1598,26 +1598,61 @@
   + %22  while
- 
 (
+_GLF_WRAPPED_LOOP(_GLF_FALSE(false ,
 _GLF_DEAD(_G
@@ -1667,32 +1667,34 @@
 (false, false)))
+))
  %7B%22%0A        + %22 
@@ -2129,16 +2129,20 @@
 int i = 
+int(
 _GLF_ONE
@@ -2143,16 +2143,18 @@
 LF_ONE(1
+.0
 , _GLF_I
@@ -2154,35 +2154,40 @@
  _GLF_IDENTITY(1
+.0
 , 1
+.0)
 ));%22%0A        + %22
@@ -2367,17 +2367,24 @@
 int i = 
-1
+int(1.0)
 ; i %3E 1;
@@ -2721,16 +2721,28 @@
 ch(_GLF_
+SWITCH(_GLF_
 ZERO(0, 
@@ -2762,16 +2762,17 @@
 (0, 0)))
+)
 %22%0A      
@@ -3230,16 +3230,34 @@
 %7D while 
+(_GLF_WRAPPED_LOOP
 (_GLF_DE
@@ -3285,16 +3285,17 @@
 false)))
+)
 ;%22%0A     
@@ -3817,16 +3817,16 @@
 %0A    );%0A
-
     fina
@@ -3850,16 +3850,21 @@
  = %22void
+ main
 () %7B%22%0A  

Add macro wrapper and fix space before comma (#500)