MatrixBerryCore
EqualCallCounter.m
Go to the documentation of this file.
1 classdef EqualCallCounter<mxberry.core.obj.StaticPropStorage
2  methods (Static)
3  function [propVal,isThere]=getProp(propName,varargin)
4  branchName=mfilename('class');
5  [propVal,isThere]=mxberry.core.obj.StaticPropStorage.getPropInternal(...
6  branchName,propName,varargin{:});
7  end
8  function setProp(propName,propVal)
9  branchName=mfilename('class');
11  branchName,propName,propVal);
12  end
13  function flush()
14  branchName=mfilename('class');
15  mxberry.core.obj.StaticPropStorage.flushInternal(branchName);
16  end
17  function val=getEqCounter()
18  import mxberry.core.test.aux.EqualCallCounter;
19  [val,isThere]=EqualCallCounter.getProp('isEqCallCount',true);
20  if ~isThere
21  val=0;
22  end
23  end
24  function setEqCounter(val)
25  mxberry.core.test.aux.EqualCallCounter.setProp(...
26  'isEqCallCount',val);
27  end
28  function incEqCounter(valInc)
29  import mxberry.core.test.aux.EqualCallCounter;
30  val=EqualCallCounter.getEqCounter();
31  val=val+valInc;
32  EqualCallCounter.setEqCounter(val);
33  end
34  %
35  function checkNCallsEquality(fList)
36  nFunc=numel(fList);
37  callNumVec=nan(1,nFunc);
38  for iFunc=1:nFunc
39  callNumVec(iFunc)=getNCalls(fList{iFunc});
40  end
41  checkEqCalls(callNumVec(1:end-1),callNumVec(2:end));
42  end
43  %
44  function checkNotSortableCalls(objVec)
47  @()mxberry.core.ismemberjoint({objVec},{objVec(2:end)}),...
48  @()ismember(objVec,objVec(2:end))});
49  EqualCallCounter.checkNCallsEquality({...
50  @()mxberry.core.ismemberjoint({objVec},{objVec}),...
51  @()ismember(objVec,objVec)});
52  EqualCallCounter.checkNCallsEquality({...
53  @()unique(objVec),...
54  @()mxberry.core.uniquejoint({objVec})});
55  end
56  %
57  function checkCalls(objVec,isBuiltInsChecked)
59  if nargin<2
60  isBuiltInsChecked=true;
61  end
62  nRels=numel(objVec);
63  %
64  check(@()(isequal(objVec(1),objVec(2))),1);
65  check(@()(isequaln(objVec(1),objVec(2))),1);
66  check(@()(isequaln(objVec(1),objVec(2))),1);
67  %
68  nSortCalls=getNCalls(@()sort(objVec));
69  sortedObjVec=sort(objVec);
70  nDoubleSortCalls=getNCalls(@()sort([sortedObjVec,sortedObjVec]));
71  doubleSortedObjVec=sort([sortedObjVec,sortedObjVec]);
72  nHandleComparisons=sum(eq(doubleSortedObjVec(1:end-1),...
73  doubleSortedObjVec(2:end),'asHandle',true));
74  fUniqCplx=@(x)(x-1+nSortCalls);
75  fIsMembCplx=@(x)(2*fUniqCplx(x)+2*x-1-nHandleComparisons+nDoubleSortCalls);
76  %
77  nCallsForBuiltInUniq=fUniqCplx(nRels);
78  if ismethod(objVec,'sort')
79  nCallsForUniqJoint=nCallsForBuiltInUniq;
80  else
81  nCallsForUniqJoint=nRels*(nRels-1)*0.5;
82  end
83  %
84  if isBuiltInsChecked
85  check(@()unique(objVec),nCallsForBuiltInUniq);
86  end
87  check(@()mxberry.core.uniquejoint({objVec}),nCallsForUniqJoint);
88  %
89  nEqCallsForBuiltInIsMember=fIsMembCplx(nRels);
90  if isBuiltInsChecked
91  check(@()ismember(objVec,objVec),nEqCallsForBuiltInIsMember);
92  end
93  check(@()mxberry.core.ismemberjoint({objVec},{objVec}),...
94  nEqCallsForBuiltInIsMember);
95  check(@()mxberry.core.ismember(objVec,objVec),...
96  nEqCallsForBuiltInIsMember);
97  %
98  function check(fHandle,nExpCalls)
99  nCalls=getNCalls(fHandle);
100  checkEqCalls(nExpCalls,nCalls);
101  end
102  end
103  function nCalls=getNCalls(fHandle)
104  nCalls=getNCalls(fHandle);
105  end
106  end
107 end
108 function checkEqCalls(nExpCallVec,nCallVec)
109 isOk=isequal(nExpCallVec,nCallVec);
110 if ~isOk
111  mxberry.core.throwerror('wrongState',...
112  sprintf('Expected %s calls, Called %s times',...
113  mat2str(nExpCallVec),mat2str(nCallVec)));
114 end
115 end
116 function nCalls=getNCalls(fHandle)
118 %
119 feval(fHandle);
120 %
122 
123 end
static function setPropInternal(in branchName, in propName, in propVal)
function uniquejoint(in inpCArr, in varargin)
UNIQUEJOINT perform joint unique operation for cell arrays.
function unique(in inpVec)
UNIQUE for arrays of any type.
static function checkNCallsEquality(in fList)
function ismember(in leftVec, in rightVec, in varargin)
ISMEMBER - ismember implementation for arrays of any type.
static function getPropInternal(in branchName, in propName, in isPresenceChecked)