vector.com|Contact
Search tips

apple banana
Find rows that contain at least one of the two words.

+apple +juice
Find rows that contain both words.

+apple macintosh
Find rows that contain the word 'apple', but rank rows higher if they also contain 'macintosh'.

+apple -macintosh
Find rows that contain the word 'apple' but not 'macintosh'.

+apple ~macintosh
Find rows that contain the word 'apple', but if the row also contains the word 'macintosh', rate it lower than if row does not. This is "softer" than a search for '+apple -macintosh', for which the presence of 'macintosh' causes the row not to be returned at all.

+apple +(>turnover >strudel)
Find rows that contain the words 'apple' and 'turnover', or 'apple' and 'strudel' (in any order), but rank 'apple turnover' higher than 'apple strudel'.

apple*
Find rows that contain words such as 'apple', 'apples', 'applesauce', or 'applet'.

"some words"
Find rows that contain the exact phrase 'some words' (for example, rows that contain 'some words of wisdom' but not "some noise words").

By continuing to use this site you agree to the use of cookies. For more information and to find out how to change this click here. Accept Cookies
Please enable cookies in your browser for this website.
Search:
Advanced search

Stubbing Operator New in Environment

Last updated: 2019-11-11
Question:
I would like to use the new operator in order to test an exception in my code. How do I force the 'new' operator to be stubbed in VectorCAST?
Answer:

There is a way to force operator new to be stubbed. It involves pre-declaring it in unit prefix user code (Environment | User Code | Edit). However, it is probably easier to simply define it yourself, and have it fail whenever a particular condition is met (which you can control). For instance, try this:
 

  • add the following code to User Globals User Code:

VCAST_USER_GLOBALS_EXTERN int num_calls_to_new;
VCAST_USER_GLOBALS_EXTERN int new_fails_on_call;

 

  • add the following code to Environment User Code - Data:
     

  #include <stdlib.h>
  #include <new>
  extern int num_calls_to_new;
  extern int new_fails_on_call;


  void *operator new(size_t sz, const std::nothrow_t&) throw ()
  {
    if ( ++num_calls_to_new == new_fails_on_call )
    {
      return 0;
    }
    return malloc(sz);
  }


  void *operator new[](size_t sz, const std::nothrow_t&) throw ()
  {
    if ( ++num_calls_to_new == new_fails_on_call )
    {
      return 0;
    }
    return malloc(sz);
  }


  void *operator new(size_t sz) throw (std::bad_alloc)
  {
    if ( ++num_calls_to_new == new_fails_on_call )
    {
      throw std::bad_alloc();
    }
    return malloc(sz);
  }


  void *operator new[](size_t sz) throw (std::bad_alloc)
  {
    if ( ++num_calls_to_new == new_fails_on_call )
    {
      throw std::bad_alloc();
    }
    return malloc(sz);
  }


  void operator delete(void* ptr) throw ()
  {
    if ( ptr )
      free(ptr);
  }


  void operator delete[](void* ptr) throw ()
  {
    if ( ptr )
      free(ptr);
  }

 

  • save the changes
  • rebuild your environment
  • in your test case, you will now be able to set new_fails_on_call to the Nth call to new which you want to fail. For instance, if the call to new which you want to fail is the 3rd call, you would set new_fails_on_call to 3 in the parameter tree.

Keep in mind that the VectorCAST-generated code may call new before your function is called or when stubs are called, or your function may call your own functions which call new. So the number of calls to new may not be obvious. In the parameter tree you can set the expected value for num_calls_to_new to <<ANY>> to have VectorCAST show its value in the report, which would help you determine how many hidden calls you need to account for.
 

 
Article Options
2019-11-11
Views: 9