aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/scala/async/run/ifelse0/WhileSpec.scala
blob: 9ba0b69471e2b219167e0a964a578256b620f7de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2012-2014 Lightbend Inc. <http://www.lightbend.com>
 */

package scala.async
package run
package ifelse0

import org.junit.Test
import scala.async.internal.AsyncId

class WhileSpec {

  @Test
  def whiling1() {
    import AsyncId._

    val result = async {
      var xxx: Int = 0
      var y = 0
      while (xxx < 3) {
        y = await(xxx)
        xxx = xxx + 1
      }
      y
    }
    result mustBe (2)
  }

  @Test
  def whiling2() {
    import AsyncId._

    val result = async {
      var xxx: Int = 0
      var y = 0
      while (false) {
        y = await(xxx)
        xxx = xxx + 1
      }
      y
    }
    result mustBe (0)
  }

  @Test
  def nestedWhile() {
    import AsyncId._

    val result = async {
      var sum = 0
      var i = 0
      while (i < 5) {
        var j = 0
        while (j < 5) {
          sum += await(i) * await(j)
          j += 1
        }
        i += 1
      }
      sum
    }
    result mustBe (100)
  }

  @Test
  def whileExpr() {
    import AsyncId._

    val result = async {
      var cond = true
      while (cond) {
        cond = false
        await { 22 }
      }
    }
    result mustBe ()
  }

  @Test def doWhile() {
    import AsyncId._
    val result = async {
      var b = 0
      var x = ""
      await(do {
        x += "1"
        x += await("2")
        x += "3"
        b += await(1)
      } while (b < 2))
      await(x)
    }
    result mustBe "123123"
  }

  @Test def whileAwaitCondition() {
    import AsyncId._
    val result = async {
      var b = true
      while(await(b)) {
        b = false
      }
      await(b)
    }
    result mustBe false
  }

  @Test def doWhileAwaitCondition() {
    import AsyncId._
    val result = async {
      var b = true
      do {
        b = false
      } while(await(b))
      b
    }
    result mustBe false
  }
}