Skip to content

Commit

Permalink
add testing unit, fix issues found in the testing unit
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Jun 9, 2020
1 parent ddb0623 commit e2451e1
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 38 deletions.
69 changes: 37 additions & 32 deletions savejson.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
% FileName [''|string]: a file name to save the output JSON data
% FloatFormat ['%.10g'|string]: format to show each numeric element
% of a 1D/2D array;
% IntFormat ['%d'|string]: format to display integer elements
% IntFormat ['%.0f'|string]: format to display integer elements
% of a 1D/2D array;
% ArrayIndent [1|0]: if 1, output explicit data array with
% precedent indentation; if 0, no indentation
Expand Down Expand Up @@ -147,7 +147,7 @@
opt.formatversion=jsonopt('FormatVersion',2,opt);
opt.compressarraysize=jsonopt('CompressArraySize',100,opt);
opt.compressstringsize=jsonopt('CompressStringSize',opt.compressarraysize*4,opt);
opt.intformat=jsonopt('IntFormat','%d',opt);
opt.intformat=jsonopt('IntFormat','%.0f',opt);
opt.floatformat=jsonopt('FloatFormat','%.10g',opt);
opt.unpackhex=jsonopt('UnpackHex',1,opt);
opt.arraytostruct=jsonopt('ArrayToStruct',0,opt);
Expand Down Expand Up @@ -288,6 +288,9 @@
if(~iscell(item) && ~isa(item,'string'))
error('input is not a cell or string array');
end
if(isa(item,'string'))
level=level-1;
end
isnum2cell=varargin{1}.num2cell_;
if(isnum2cell)
item=squeeze(item);
Expand All @@ -310,14 +313,16 @@
nl=ws.newline;
bracketlevel=~varargin{1}.singletcell;
if(len>bracketlevel)
if(~isempty(name))
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'": [', nl}; name='';
else
txt={padding0, '[', nl};
if(~isa(item,'string'))
if(~isempty(name))
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":[', nl}; name='';
else
txt={padding0, '[', nl};
end
end
elseif(len==0)
if(~isempty(name))
txt={padding0, '"' decodevarname(name,varargin{1}.unpackhex) '": []'}; name='';
txt={padding0, '"' decodevarname(name,varargin{1}.unpackhex) '":[]'}; name='';
else
txt={padding0, '[]'};
end
Expand All @@ -340,7 +345,7 @@
end
%if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end
end
if(len>bracketlevel)
if(len>bracketlevel && ~isa(item,'string'))
txt(end+1:end+3)={nl,padding0,']'};
end
txt = sprintf('%s',txt{:});
Expand Down Expand Up @@ -369,7 +374,7 @@

if(isempty(item))
if(~isempty(name))
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'": []'};
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":[]'};
else
txt={padding0, '[]'};
end
Expand All @@ -378,7 +383,7 @@
end
if(~isempty(name))
if(forcearray)
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'": [', nl};
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":[', nl};
end
else
if(forcearray)
Expand All @@ -392,7 +397,7 @@
for i=1:dim(1)
names = fieldnames(item(i,j));
if(~isempty(name) && len==1 && ~forcearray)
txt(end+1:end+5)={padding1, '"', decodevarname(name,varargin{1}.unpackhex),'": {', nl};
txt(end+1:end+5)={padding1, '"', decodevarname(name,varargin{1}.unpackhex),'":{', nl};
else
txt(end+1:end+3)={padding1, '{', nl};
end
Expand Down Expand Up @@ -466,7 +471,7 @@

if(isempty(item))
if(~isempty(name))
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'": []'};
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":[]'};
else
txt={padding0, '[]'};
end
Expand All @@ -475,7 +480,7 @@
end
if(~isempty(name))
if(forcearray)
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'": {', nl};
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":{', nl};
end
else
if(forcearray)
Expand Down Expand Up @@ -516,7 +521,7 @@

if(~isempty(name))
if(len>1)
txt={padding1, '"', decodevarname(name,varargin{1}.unpackhex),'": [', nl};
txt={padding1, '"', decodevarname(name,varargin{1}.unpackhex),'":[', nl};
end
else
if(len>1)
Expand All @@ -530,7 +535,7 @@
val=item(e,:);
end
if(len==1)
obj=['"' decodevarname(name,varargin{1}.unpackhex) '": ' '"',val,'"'];
obj=['"' decodevarname(name,varargin{1}.unpackhex) '":' '"',val,'"'];
if(isempty(name))
obj=['"',val,'"'];
end
Expand Down Expand Up @@ -567,10 +572,10 @@
if(~varargin{1}.nosubstruct_ && ( ((isnest==0) && length(size(item))>2) || issparse(item) || ~isreal(item) || ...
(isempty(item) && any(size(item))) || varargin{1}.arraytostruct || (~isempty(dozip) && numel(item)>zipsize)))
if(isempty(name))
txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',...
txt=sprintf('%s{%s%s"_ArrayType_":"%s",%s%s"_ArraySize_":%s,%s',...
padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl);
else
txt=sprintf('%s"%s": {%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',...
txt=sprintf('%s"%s":{%s%s"_ArrayType_":"%s",%s%s"_ArraySize_":%s,%s',...
padding1,decodevarname(name,varargin{1}.unpackhex),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl);
end
else
Expand All @@ -579,9 +584,9 @@
txt=sprintf('%s%s',padding1,numtxt);
else
if(numel(item)==1 && varargin{1}.singletarray==0)
txt=sprintf('%s"%s": %s',padding1,decodevarname(name,varargin{1}.unpackhex),numtxt);
txt=sprintf('%s"%s":%s',padding1,decodevarname(name,varargin{1}.unpackhex),numtxt);
else
txt=sprintf('%s"%s": %s',padding1,decodevarname(name,varargin{1}.unpackhex),numtxt);
txt=sprintf('%s"%s":%s',padding1,decodevarname(name,varargin{1}.unpackhex),numtxt);
end
end
return;
Expand All @@ -599,9 +604,9 @@
% (Necessary for complex row vector handling below.)
data=data';
end
txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','true', sep);
txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_":','true', sep);
end
txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_": ','true', sep);
txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_":','true', sep);
if(~isempty(dozip) && numel(data*2)>zipsize)
if(size(item,1)==1)
% Row vector, store only column indices.
Expand All @@ -613,10 +618,10 @@
% General case, store row and column indices.
fulldata=[ix,iy,data];
end
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipSize_": ',regexprep(mat2str(size(fulldata)),'\s+',','), sep);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipType_": "',dozip, ['"' sep]);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipSize_":',regexprep(mat2str(size(fulldata)),'\s+',','), sep);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipType_":"',dozip, ['"' sep]);
compfun=str2func([dozip 'encode']);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipData_": "',base64encode(compfun(typecast(fulldata(:),'uint8'))),['"' nl]);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipData_":"',base64encode(compfun(typecast(fulldata(:),'uint8'))),['"' nl]);
else
if(size(item,1)==1)
% Row vector, store only column indices.
Expand All @@ -628,7 +633,7 @@
% General case, store row and column indices.
fulldata=[ix,iy,data];
end
txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',...
txt=sprintf(dataformat,txt,padding0,'"_ArrayData_":',...
matdata2json(fulldata',level+2,varargin{:}), nl);
end
else
Expand All @@ -642,20 +647,20 @@
fulldata=uint8(fulldata);
end
else
txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','true', sep);
txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_":','true', sep);
fulldata=[real(item(:)) imag(item(:))]';
end
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipSize_": ',regexprep(mat2str(size(fulldata)),'\s+',','), sep);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipType_": "',dozip, ['"' sep]);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipSize_":',regexprep(mat2str(size(fulldata)),'\s+',','), sep);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipType_":"',dozip, ['"' sep]);
compfun=str2func([dozip 'encode']);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipData_": "',char(base64encode(compfun(typecast(fulldata(:),'uint8')))),['"' nl]);
txt=sprintf(dataformat,txt,padding0,'"_ArrayZipData_":"',char(base64encode(compfun(typecast(fulldata(:),'uint8')))),['"' nl]);
else
if(isreal(item))
txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',...
txt=sprintf(dataformat,txt,padding0,'"_ArrayData_":',...
matdata2json(item(:)',level+2,varargin{:}), nl);
else
txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','true', sep);
txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',...
txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_":','true', sep);
txt=sprintf(dataformat,txt,padding0,'"_ArrayData_":',...
matdata2json([real(item(:)) imag(item(:))]',level+2,varargin{:}), nl);
end
end
Expand Down
18 changes: 12 additions & 6 deletions saveubjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
%%-------------------------------------------------------------------------
function txt=cell2ubjson(name,item,level,varargin)
txt='';
if(~iscell(item))
if(~iscell(item) && ~isa(item,'string'))
error('input is not a cell');
end
isnum2cell=varargin{1}.num2cell_;
Expand Down Expand Up @@ -766,7 +766,11 @@
end
end
if(numel(mat)==1)
txt=I_(int64(mat),varargin{:});
if(mat<0)
txt=I_(int64(mat),varargin{:});
else
txt=I_(uint64(mat),varargin{:});
end
else
txt=I_a(mat(:),type,size(mat),varargin{:});
end
Expand Down Expand Up @@ -848,7 +852,7 @@
isdebug=varargin{1}.debug;
if(isfield(varargin{1},'inttype_'))
if(isdebug)
val=[Imarker(varargin{1}.inttype_) sprintf('<%d>',num)];
val=[Imarker(varargin{1}.inttype_) sprintf('<%.0f>',num)];
else
val=[Imarker(varargin{1}.inttype_) data2byte(swapbytes(cast(num,cid{varargin{1}.inttype_})),'uint8')];
end
Expand All @@ -872,15 +876,17 @@
for i=1:length(cid)
if(num==cast(num,cid{i}))
if(isdebug)
val=[key(i) sprintf('<%d>',num)];
val=[key(i) sprintf('<%.0f>',num)];
else
val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')];
end
return;
end
end
error('unsupported integer');

val=S_(sprintf('%.0f',num),varargin{:});
if(Imarker(1)=='U')
val(1)='H';
end
%%-------------------------------------------------------------------------
function val=D_(num, varargin)
if(~isfloat(num))
Expand Down
32 changes: 32 additions & 0 deletions test/run_save_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function run_save_test(testname,fhandle,input,expected,varargin)
res=fhandle('',input,varargin{:});
if(~isequal(strtrim(res),expected))
warning('%s: failed: expected ''%s'', obtained ''%s''',testname,expected,res);
else
fprintf(1,'%s: ok\n\toutput:''%s''\n',testname,strtrim(res));
if(regexp(res,'^[\[\{]'))
handleinfo=functions(fhandle);
loadfunname=regexprep(handleinfo.function,'^save','load');
loadfun=str2func(loadfunname);
if(strcmp(loadfunname,'loadubjson'))
newres=loadfun(fhandle('',input,varargin{:},'debug',0),varargin{:});
else
newres=loadfun(res,varargin{:});
end
if(exist('isequaln'))
try
if(isequaln(newres,input))
fprintf(1,'\t%s successfully restored the input\n',loadfunname);
end
catch
end
else
try
if(newres==input)
fprintf(1,'\t%s successfully restored the input\n',loadfunname);
end
catch
end
end
end
end
Loading

0 comments on commit e2451e1

Please sign in to comment.