Skip to content

Commit ce76a41

Browse files
committed
обработка параметра Количество (совместимо)
1 parent 15b12eb commit ce76a41

2 files changed

Lines changed: 54 additions & 8 deletions

File tree

src/OneScript.StandardLibrary/Hash/HashImpl.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This Source Code Form is subject to the terms of the
1313
using ScriptEngine.Machine.Contexts;
1414
using System;
1515
using System.IO;
16+
using System.Runtime.Intrinsics.X86;
1617
using System.Security.Cryptography;
1718
using System.Text;
1819

@@ -104,6 +105,12 @@ private void AppendStream(Stream stream)
104105

105106
private void AppendStream(Stream stream, int count)
106107
{
108+
if (count <= 0)
109+
{
110+
AppendStream(stream);
111+
return;
112+
}
113+
107114
int bufSize = Math.Min(BUFFER_SIZE, count);
108115
var buffer = new byte[bufSize];
109116
int toRead = count;
@@ -119,23 +126,42 @@ private void AppendStream(Stream stream, int count)
119126
}
120127

121128
[ContextMethod("Добавить", "Append")]
122-
public void Append(BslValue toAdd, int count = 0)
129+
public void Append(BslValue toAdd, BslValue count = null)
123130
{
124131
switch (toAdd)
125132
{
126133
case BslStringValue s:
127134
AppendData(Encoding.UTF8.GetBytes((string)s));
128135
break;
129-
case IStreamWrapper wrapper:
130-
var stream = wrapper.GetUnderlyingStream();
131-
if (count <= 0)
132-
AppendStream(stream);
133-
else
134-
AppendStream(stream, count);
135-
break;
136136
case BinaryDataContext binaryData:
137137
AppendStream(binaryData.GetStream());
138138
break;
139+
140+
case IStreamWrapper wrapper:
141+
var stream = wrapper.GetUnderlyingStream();
142+
if (count == null)
143+
{
144+
AppendStream(stream);
145+
}
146+
else
147+
{
148+
int cnt;
149+
try
150+
{
151+
cnt = (int)count;
152+
}
153+
catch
154+
{
155+
if (count is BslStringValue)
156+
throw RuntimeException.InvalidNthArgumentValue(2);
157+
else
158+
throw RuntimeException.InvalidNthArgumentType(2);
159+
}
160+
161+
AppendStream(stream, cnt);
162+
}
163+
break;
164+
139165
default:
140166
throw RuntimeException.InvalidNthArgumentType(1);
141167
}

tests/hash.os

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
ВсеТесты.Добавить("ТестДолжен_ПроверитьХешиПустойСтроки");
1919
ВсеТесты.Добавить("ТестДолжен_ПроверитьХешиПустогоПотока");
2020
ВсеТесты.Добавить("ТестДолжен_ПроверитьИнкрементальность");
21+
ВсеТесты.Добавить("ТестДолжен_ПроверитьИгнорированиеВторогоПараметра");
2122

2223
Возврат ВсеТесты;
2324
КонецФункции
@@ -208,3 +209,22 @@
208209

209210
КонецПроцедуры
210211

212+
Процедура ТестДолжен_ПроверитьИгнорированиеВторогоПараметра() Экспорт
213+
Хеш = 158520161;
214+
215+
Провайдер = Новый ХешированиеДанных(ХешФункция.CRC32);
216+
Провайдер.Добавить("123456",);
217+
юТест.ПроверитьРавенство(Хеш, Провайдер.ХешСумма);
218+
219+
Провайдер = Новый ХешированиеДанных(ХешФункция.CRC32);
220+
Провайдер.Добавить("123456", 3);
221+
юТест.ПроверитьРавенство(Хеш, Провайдер.ХешСумма);
222+
223+
Провайдер = Новый ХешированиеДанных(ХешФункция.CRC32);
224+
Провайдер.Добавить("123456", '0001-01-01');
225+
юТест.ПроверитьРавенство(Хеш, Провайдер.ХешСумма);
226+
227+
Провайдер = Новый ХешированиеДанных(ХешФункция.CRC32);
228+
Провайдер.Добавить("123456", Новый Массив);
229+
юТест.ПроверитьРавенство(Хеш, Провайдер.ХешСумма);
230+
КонецПроцедуры

0 commit comments

Comments
 (0)