/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/expressions/unaryoperations/UnaryExprTest.java

https://github.com/ballerina-platform/ballerina-lang · Java · 188 lines · 124 code · 45 blank · 19 comment · 0 complexity · 279b94971b36285a9662a415d7fc7a8e MD5 · raw file

  1. /*
  2. * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
  3. *
  4. * WSO2 Inc. licenses this file to you under the Apache License,
  5. * Version 2.0 (the "License"); you may not use this file except
  6. * in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,
  11. * software distributed under the License is distributed on an
  12. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. * KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations
  15. * under the License.
  16. */
  17. package org.ballerinalang.test.expressions.unaryoperations;
  18. import org.ballerinalang.model.values.BBoolean;
  19. import org.ballerinalang.model.values.BFloat;
  20. import org.ballerinalang.model.values.BInteger;
  21. import org.ballerinalang.model.values.BValue;
  22. import org.ballerinalang.test.util.BAssertUtil;
  23. import org.ballerinalang.test.util.BCompileUtil;
  24. import org.ballerinalang.test.util.BRunUtil;
  25. import org.ballerinalang.test.util.CompileResult;
  26. import org.testng.Assert;
  27. import org.testng.annotations.BeforeClass;
  28. import org.testng.annotations.Test;
  29. /**
  30. * Class to test functionality of unary operator.
  31. */
  32. public class UnaryExprTest {
  33. CompileResult result;
  34. CompileResult resultNegative;
  35. @BeforeClass
  36. public void setup() {
  37. result = BCompileUtil.compile("test-src/expressions/unaryoperations/unary-operation.bal");
  38. resultNegative = BCompileUtil.compile(
  39. "test-src/expressions/unaryoperations/unary-operation-negative.bal");
  40. }
  41. @Test(description = "Test unary negative expression")
  42. public void integerUnaryExprTest() {
  43. BValue[] args = {};
  44. BValue[] returns = BRunUtil.invoke(result, "negativeIntTest", args);
  45. Assert.assertEquals(returns.length, 2);
  46. BInteger x = (BInteger) returns[0];
  47. Assert.assertSame(x.getClass(), BInteger.class, "Invalid class type returned.");
  48. Assert.assertEquals(x.intValue(), (-5), "Invalid value returned.");
  49. BInteger y = (BInteger) returns[1];
  50. Assert.assertSame(y.getClass(), BInteger.class, "Invalid class type returned.");
  51. Assert.assertEquals(y.intValue(), 5, "Invalid value returned.");
  52. }
  53. @Test(description = "Test int positive unary expression")
  54. public void positiveIntegerUnaryExprTest() {
  55. BValue[] args = {};
  56. BValue[] returns = BRunUtil.invoke(result, "positiveIntTest", args);
  57. Assert.assertEquals(returns.length, 2);
  58. BInteger x = (BInteger) returns[0];
  59. Assert.assertSame(x.getClass(), BInteger.class, "Invalid class type returned.");
  60. Assert.assertEquals(x.intValue(), (+5), "Invalid value returned.");
  61. BInteger y = (BInteger) returns[1];
  62. Assert.assertSame(y.getClass(), BInteger.class, "Invalid class type returned.");
  63. Assert.assertEquals(y.intValue(), +5, "Invalid value returned.");
  64. }
  65. @Test(description = "Test float unary negative expression")
  66. public void floatUnaryExprTest() {
  67. BValue[] args = {};
  68. BValue[] returns = BRunUtil.invoke(result, "negativeFloatTest", args);
  69. Assert.assertEquals(returns.length, 2);
  70. BFloat x = (BFloat) returns[0];
  71. Assert.assertSame(x.getClass(), BFloat.class, "Invalid class type returned.");
  72. Assert.assertEquals(x.floatValue(), -5.0D, "Invalid value returned.");
  73. BFloat y = (BFloat) returns[1];
  74. Assert.assertSame(y.getClass(), BFloat.class, "Invalid class type returned.");
  75. Assert.assertEquals(y.floatValue(), 5.0D, "Invalid value returned.");
  76. }
  77. @Test(description = "Test float positive unary expression")
  78. public void positiveFloatUnaryExprTest() {
  79. BValue[] args = {};
  80. BValue[] returns = BRunUtil.invoke(result, "positiveFloatTest", args);
  81. Assert.assertEquals(returns.length, 2);
  82. BFloat x = (BFloat) returns[0];
  83. Assert.assertSame(x.getClass(), BFloat.class, "Invalid class type returned.");
  84. Assert.assertEquals(x.floatValue(), +5D, "Invalid value returned.");
  85. BFloat y = (BFloat) returns[1];
  86. Assert.assertSame(y.getClass(), BFloat.class, "Invalid class type returned.");
  87. Assert.assertEquals(y.floatValue(), +5D, "Invalid value returned.");
  88. }
  89. @Test(description = "Test unary boolean not expression")
  90. public void booleanUnaryExprTest() {
  91. BValue[] args = {};
  92. BValue[] returns = BRunUtil.invoke(result, "booleanNotTest", args);
  93. Assert.assertEquals(returns.length, 3);
  94. BBoolean x = (BBoolean) returns[0];
  95. Assert.assertSame(x.getClass(), BBoolean.class, "Invalid class type returned.");
  96. Assert.assertFalse(x.booleanValue(), "Invalid value returned.");
  97. BBoolean y = (BBoolean) returns[1];
  98. Assert.assertSame(y.getClass(), BBoolean.class, "Invalid class type returned.");
  99. Assert.assertTrue(y.booleanValue(), "Invalid value returned.");
  100. BBoolean z = (BBoolean) returns[2];
  101. Assert.assertSame(z.getClass(), BBoolean.class, "Invalid class type returned.");
  102. Assert.assertTrue(z.booleanValue(), "Invalid value returned.");
  103. }
  104. @Test(description = "Test unary boolean not expression in if else")
  105. public void unaryExprInIfConditionTest() {
  106. BValue[] args = {};
  107. BValue[] returns = BRunUtil.invoke(result, "unaryExprInIfConditionTest", args);
  108. Assert.assertEquals(returns.length, 1);
  109. BBoolean x = (BBoolean) returns[0];
  110. Assert.assertSame(x.getClass(), BBoolean.class, "Invalid class type returned.");
  111. Assert.assertTrue(x.booleanValue(), "Invalid value returned.");
  112. }
  113. @Test(description = "Test unary negation expression")
  114. public void unaryNegationTest() {
  115. long a = 3;
  116. long b = 2;
  117. long expectedResult = a - -b;
  118. BValue[] args = {new BInteger(a), new BInteger(b)};
  119. BValue[] returns = BRunUtil.invoke(result, "unaryNegationTest", args);
  120. Assert.assertEquals(returns.length, 1);
  121. Assert.assertSame(returns[0].getClass(), BInteger.class, "Invalid class type returned.");
  122. long actualResult = ((BInteger) returns[0]).intValue();
  123. Assert.assertEquals(actualResult, expectedResult);
  124. }
  125. @Test(description = "Test unary positive negation expression")
  126. public void unaryPositiveNegationTest() {
  127. long a = 3;
  128. long expectedResult = +-a;
  129. BValue[] args = {new BInteger(a)};
  130. BValue[] returns = BRunUtil.invoke(result, "unaryPositiveNegationTest", args);
  131. Assert.assertEquals(returns.length, 1);
  132. Assert.assertSame(returns[0].getClass(), BInteger.class, "Invalid class type returned.");
  133. long actualResult = ((BInteger) returns[0]).intValue();
  134. Assert.assertEquals(actualResult, expectedResult);
  135. }
  136. @Test(description = "Test uanry statement with errors")
  137. public void testUnaryStmtNegativeCases() {
  138. Assert.assertEquals(resultNegative.getErrorCount(), 3);
  139. BAssertUtil.validateError(resultNegative, 0, "operator '+' not defined for 'json'", 5, 10);
  140. BAssertUtil.validateError(resultNegative, 1, "operator '-' not defined for 'json'", 14, 10);
  141. BAssertUtil.validateError(resultNegative, 2, "operator '!' not defined for 'json'", 23, 10);
  142. }
  143. }