SCV:制約付ランダムデータ生成(その2)

今回は、SCVを使った制約付きランダム生成(その2)です。
前回のデータを元に更に制約を加えてみたいと思います。

  • サンプルコード
#include <scv.h>

// Valid values for a are 10-20, 50-60, 90-100
class slower : public scv_constraint_base {
public:
  scv_smart_ptr <int> a;
  SCV_CONSTRAINT_CTOR(slower) {
    SCV_CONSTRAINT(
       (a() >= 10 && a() <= 100) &&
      !(a() >= 21 && a() <= 49) &&
      !(a() >= 61 && a() <= 89)
    );
  }
};

// Valid values for a are 55-60, 90-100
class new_slower : public slower {
  SCV_CONSTRAINT_CTOR(new_slower) {
    SCV_BASE_CONSTRAINT(slower);
    SCV_CONSTRAINT(
      ( a() >= 55)
    );
  }
};

int sc_main( int argc, char* argv[] ) {

  slower     s_hoge("slower");
  new_slower n_hoge("new_slower");

  for (int i=0; i<10; i++) {
    s_hoge.next();
    printf( "[SCV] slower data = %d\n", s_hoge.a->read() );
  }
  printf("\n");
  for (int i=0; i<20; i++) {
    n_hoge.next();
    printf( "[SCV] new slower data = %d\n", n_hoge.a->read() );
  }

  return 0;
}
  • 実行結果
$> ./main 

             SystemC 2.2.0 --- Jan 15 2012 14:19:56
        Copyright (c) 1996-2006 by all Contributors
                    ALL RIGHTS RESERVED
[SCV] slower data = 94
[SCV] slower data = 99
[SCV] slower data = 55
[SCV] slower data = 94
[SCV] slower data = 99
[SCV] slower data = 55
[SCV] slower data = 15
[SCV] slower data = 99
[SCV] slower data = 91
[SCV] slower data = 100

[SCV] new slower data = 56
[SCV] new slower data = 92
[SCV] new slower data = 55
[SCV] new slower data = 93
[SCV] new slower data = 55
[SCV] new slower data = 91
[SCV] new slower data = 93
[SCV] new slower data = 96
[SCV] new slower data = 60
[SCV] new slower data = 93
[SCV] new slower data = 99
[SCV] new slower data = 96
[SCV] new slower data = 96
[SCV] new slower data = 94
[SCV] new slower data = 60
[SCV] new slower data = 55
[SCV] new slower data = 100
[SCV] new slower data = 58
[SCV] new slower data = 93
[SCV] new slower data = 58

新しく制約を追加したものは、制約通りに出力されています。
ちなみに、 SCV_BASE_CONSTRAINT(slower); を記載しないと、
ベースクラス(slower)の制約はなかったものとなります。