51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2553|回复: 10
打印 上一主题 下一主题

WR基本函数扩展-1

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2007-8-24 18:11:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#########################################################
#
# This compiled module contains miscellanous auxiliary functions
#
# LIST of FUNCTIONS:
#
#   General:
#
#     min, max
#
#   String functions:
#
#     abs
#     rindex
#     ltrim, rtrim
#     repl
#     count_str
#     isalpha, isdigit
#     gen_str
#
#   File Operations:
#
#     gen_file
#     dirname
#     f_tmp_name
#
#########################################################



# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function returns the lesser of two numbers passed to
#          the function as parameters.
#
#       PARAMETERS:     in a and in b - two numbers to get the lesser of them
#
#       RETURN CODE:    Lesser of the two parameters passed to the function .
#
public function min(in a, in b)
{
  if (a > b)
return(b);
  else
return(a);
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function returns the greater of two numbers passed to
#          the function as parameters.
#
#       PARAMETERS:     in a and in b - two numbers to get the greater of them
#
#       RETURN CODE:    Greater of the two parameters passed to the function .
#
public function max(in a, in b)
{
   if (a > b)
return(a);
   else
return(b);
}

# -------------------------------------------------------------------------------
#       DESCRIPTION:    This function performs an absolute value on a number.
#
#       PARAMETERS:     in number - a number.
#
#       RETURN CODE:   Returns the abs number.
#
public function abs(in number)
{
    return number <0 ? -number : number;
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function returns a position of the last occurrence of the
#          string s2 in the string s1.
#
#       PARAMETERS:     in s1 and in s2 - character strings.
#
#       RETURN CODE:    Position of the last occurrence of the string s2 in the string s1.
#
public function rindex(in s1, in s2)
{
   auto ptr, lpos, src;
   auto s2_len = length(s2);

   src = s1;
   lpos = 0;

   while ((ptr = index(src, s2)) != 0) {
lpos += ptr + (s2_len - 1);
src = substr(src, ptr + s2_len);
   }

   if (lpos > 0)
lpos -= s2_len - 1;

   return(lpos);
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function removes leading spaces and tabs in the string s.
#
#       PARAMETERS:     in s - character string.
#
#       RETURN CODE:    A string that is the copy of s where leading spaces and tabs are
#           removed .
#
public function ltrim(in s)
{
   auto i;
   auto src = s;

   while ((index(src, " ") == 1) || (index(src, "\t") == 1))
src = substr(src, 2);

   return(src);
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function removes trailing spaces and tabs in the string s.
#
#       PARAMETERS:     in s - character string.
#
#       RETURN CODE:    A string that is the copy of s where trailing spaces and tabs are
#           removed.
#
public function rtrim(in s)
{
   auto i;
   auto src = s;

   while ((rindex(src, " ") == length(src)) || (rindex(src, "\t") == length(src))) {
src = substr(src, 1, length(src) - 1);
if (length(src) == 0)
  break;
   }

   return(src);
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function replaces all occurrences of the substring sub1 in the
#          string s by the substring sub2.
#
#       PARAMETERS:     in s, in sub1, in sub2 - character strings.
#
#       RETURN CODE:    A string that is the copy of s where all occurrences of the substring
#          sub1 in the string s are replaced by the substring sub2.
#
public function repl_str(in s, in sub1, in sub2)
{
   auto ptr;
   auto src         = s;
   auto sub1_len = length(sub1);

   while (ptr = index(src, sub1))
src = substr(src, 1, ptr - 1) & sub2 & substr(src, ptr + sub1_len);

   return(src);
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function returns the number of occurrences of the substring s2
#          in the string s1.
#
#       PARAMETERS:     in s1, in s2 - character strings.
#
#       RETURN CODE:    A number of occurrences of the substring s2 in the string s1.
#
public function count_str(in s1, in s2)
{
   auto src = s1;
   auto s2_len = length(s2);
   auto sub_cnt, ptr;

   for (sub_cnt = 0; ptr = index(src, s2); sub_cnt++)
src = substr(src, ptr + s2_len);

   return(sub_cnt);
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function checks if c is a letter.
#
#       PARAMETERS:     in c - a character.
#
#       RETURN CODE:    1 - if c is a letter, otherwise - 0.
#
public function isalpha(c)
{
   auto ch = ascii(substr(c, 1, 1));

   if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
return(1);
   else
return(0);
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function checks if c is a digit.
#
#       PARAMETERS:     in c - a character.
#
#       RETURN CODE:    1 - if c is a digit, otherwise - 0.
#
public function isdigit(c)
{
   auto ch = ascii(substr(c, 1, 1));

   if (ch >= 48 && ch <= 57)
return(1);
   else
return(0);
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function generates a random string (containing characters
#         which ASCII is in the range 32 - 126).
#
#       PARAMETERS:     in slen - length of the string to generate,
#          in max_wlen - maximal length of a word in the string (length of
#      a word is random restricted from the top by
#      max_wlen).
#
#       RETURN CODE:    The generated random string.
#
public function gen_str(in slen, in max_wlen)
{
   auto rand_ch, wlen, word, i;
   auto str = "";

   # If max_wlen is not defined use the default.
   #
   if (nargs() < 2)
max_wlen = min(slen, 7);

    srand();

   # Generate random string.
   #
   while (length(str) < slen) {
    # Generate random word.
    #
word = "";
    wlen = min(int(rand() * max_wlen) + 1, slen - length(str));

    for (i = 0; i < wlen; i++) {
  # Generate random character.
  rand_ch = sprintf("%c", int(rand() * 95) + 32);
  word = word & rand_ch;
    }

    str = str & word;
    if (length(str) < slen)
  str = str & " ";
   }

   return(str);
}



######## File Operations #####################
#
#

# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function generates a file consisting of a random string
#         (containing characters which ASCII is in the range 32 - 126).
#
#       PARAMETERS:    in fname - the name of the file to generate,
#          in slen - length of the string to generate,
#   in scnt - number of lines in the file.
#
#       RETURN CODE:    None.
#
public function gen_file(in fname, in slen, in scnt)
{
   auto i;

   file_open(fname, FO_MODE_WRITE);
   for (i = 1; i <= scnt; i++)
file_printf(fname,"%s\r\n", gen_str(slen));

   file_close(fname);
}


# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function returns all but the last level of the pathname in fp.
#
#       PARAMETERS:    in fp - the pathname to proceed.
#
#       RETURN CODE:    Pathname without the last level.
#
public function dirname(in fp)
{
   auto file_name, i, d, dirs[], st;

   if( nargs() < 1)
call_chain_get_attr("testname", 1, file_name);
   else
file_name = fp;

   if (!index(file_name, FILE_SEP)) # If file_name is only name of a file.
if( file_name = "." )
  return "..\\.";
else
  return "";

   do
file_name = substr(file_name,1,length(file_name)-1);
   while (substr(file_name,length(file_name)) != FILE_SEP);

   if (file_name == "." )
file_name = "..\\.";

   return file_name;
}

# -------------------------------------------------------------------------------
#       DESCRIPTION:    The function generates a random temporary file name.
#
#       PARAMETERS:
#
#       RETURN CODE:    Returns random file name.
#
public function f_tmp_name() {
   auto c_time;

   c_time = get_time();
   return getenv("TMP") & "\\tst" & substr(c_time, length(c_time) - 4);
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2007-8-24 18:11:44 | 只看该作者
这是wr自带的一些函数
只是没有写在help文件里
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2007-8-24 21:43:59 | 只看该作者
非常感谢lz的分享,都是很有用的函数!
记得以前看过一个WR的拓展函数库,等我找到后也上传sdlkfj3
回复 支持 反对

使用道具 举报

该用户从未签到

4#
发表于 2007-8-26 21:47:05 | 只看该作者
很早以前下载的WR拓展函数库,没怎么仔细看过这两个文件的内容sdlkfj1 ,有需要的拿下吧,如果有分数不够的找我MSN要

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
回复 支持 反对

使用道具 举报

该用户从未签到

5#
发表于 2007-8-27 09:08:06 | 只看该作者
非常感谢LZ的分享!支持一下!!!
回复 支持 反对

使用道具 举报

该用户从未签到

6#
发表于 2007-8-27 15:23:39 | 只看该作者
支持先先
回复 支持 反对

使用道具 举报

该用户从未签到

7#
发表于 2007-8-28 08:31:22 | 只看该作者
谢谢了,我已经下了
回复 支持 反对

使用道具 举报

该用户从未签到

8#
发表于 2007-8-28 08:32:22 | 只看该作者
dionysus版主 的分享
回复 支持 反对

使用道具 举报

该用户从未签到

9#
发表于 2007-9-14 15:13:54 | 只看该作者
谢了
回复 支持 反对

使用道具 举报

该用户从未签到

10#
发表于 2007-9-24 11:46:45 | 只看该作者
谢谢!非常感谢!
回复 支持 反对

使用道具 举报

该用户从未签到

11#
发表于 2007-9-24 15:18:08 | 只看该作者
qqq
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-14 14:36 , Processed in 0.074212 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表